worktree: do not pass strbuf by value
write_worktree_linking_files() takes two struct strbuf parameters by value, even though it only reads path strings from them. Passing a strbuf by value is misleading and dangerous. The structure carries a pointer to its underlying character array; caller and callee end up sharing that storage. If the callee ever causes the strbuf to be reallocated, the caller's copy becomes a dangling pointer, which results in a double-free when the caller does strbuf_release(). The function only needs the string values, not the strbuf machinery. Switch it to take const char * and update all callers to pass .buf. Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Deveshi Dwivedi committed
Mar 11, 2026 at 17:33 UTC
4107c0bb3455905aeacdba3be09b20e62b310eaa
3 files changed
+13
-13
builtin/worktree.c
+1
-1
@@ -539,7 +539,7 @@ static int add_worktree(const char *path, const char *refname,
539
540
strbuf_reset(&sb);
541
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
542
- write_worktree_linking_files(sb_git, sb, opts->relative_paths);
542
+ write_worktree_linking_files(sb_git.buf, sb.buf, opts->relative_paths);
543
strbuf_reset(&sb);
544
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
545
write_file(sb.buf, "../..");
worktree.c
+11
-11
@@ -445,7 +445,7 @@ void update_worktree_location(struct worktree *wt, const char *path_,
445
strbuf_realpath(&path, path_, 1);
446
strbuf_addf(&dotgit, "%s/.git", path.buf);
447
if (fspathcmp(wt->path, path.buf)) {
448
- write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
448
+ write_worktree_linking_files(dotgit.buf, gitdir.buf, use_relative_paths);
449
450
free(wt->path);
451
wt->path = strbuf_detach(&path, NULL);
@@ -684,7 +684,7 @@ static void repair_gitfile(struct worktree *wt,
684
685
if (repair) {
686
fn(0, wt->path, repair, cb_data);
687
- write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
687
+ write_worktree_linking_files(dotgit.buf, gitdir.buf, use_relative_paths);
688
}
689
690
done:
@@ -742,7 +742,7 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
742
if (!file_exists(dotgit.buf))
743
goto done;
744
745
- write_worktree_linking_files(dotgit, gitdir, is_relative_path);
745
+ write_worktree_linking_files(dotgit.buf, gitdir.buf, is_relative_path);
746
done:
747
strbuf_release(&gitdir);
748
strbuf_release(&dotgit);
@@ -913,7 +913,7 @@ void repair_worktree_at_path(const char *path,
913
914
if (repair) {
915
fn(0, gitdir.buf, repair, cb_data);
916
- write_worktree_linking_files(dotgit, gitdir, use_relative_paths);
916
+ write_worktree_linking_files(dotgit.buf, gitdir.buf, use_relative_paths);
917
}
918
done:
919
free(dotgit_contents);
@@ -1087,17 +1087,17 @@ cleanup:
1087
return res;
1088
}
1089
1090
-void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
1090
+void write_worktree_linking_files(const char *dotgit, const char *gitdir,
1091
int use_relative_paths)
1092
{
1093
struct strbuf path = STRBUF_INIT;
1094
struct strbuf repo = STRBUF_INIT;
1095
struct strbuf tmp = STRBUF_INIT;
1096
1097
- strbuf_addbuf(&path, &dotgit);
1097
+ strbuf_addstr(&path, dotgit);
1098
strbuf_strip_suffix(&path, "/.git");
1099
strbuf_realpath(&path, path.buf, 1);
1100
- strbuf_addbuf(&repo, &gitdir);
1100
+ strbuf_addstr(&repo, gitdir);
1101
strbuf_strip_suffix(&repo, "/gitdir");
1102
strbuf_realpath(&repo, repo.buf, 1);
1103
@@ -1110,11 +1110,11 @@ void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
1110
}
1111
1112
if (use_relative_paths) {
1113
- write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
1114
- write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
1113
+ write_file(gitdir, "%s/.git", relative_path(path.buf, repo.buf, &tmp));
1114
+ write_file(dotgit, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp));
1115
} else {
1116
- write_file(gitdir.buf, "%s/.git", path.buf);
1117
- write_file(dotgit.buf, "gitdir: %s", repo.buf);
1116
+ write_file(gitdir, "%s/.git", path.buf);
1117
+ write_file(dotgit, "gitdir: %s", repo.buf);
1118
}
1119
1120
strbuf_release(&path);
worktree.h
+1
-1
@@ -240,7 +240,7 @@ int init_worktree_config(struct repository *r);
240
* dotgit: "/path/to/foo/.git"
241
* gitdir: "/path/to/repo/worktrees/foo/gitdir"
242
*/
243
-void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
243
+void write_worktree_linking_files(const char *dotgit, const char *gitdir,
244
int use_relative_paths);
245
246
#endif