Skip to content

Commit 4107c0b

Browse files
deveshidwivedigitster
authored andcommitted
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>
1 parent 795c338 commit 4107c0b

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ static int add_worktree(const char *path, const char *refname,
539539

540540
strbuf_reset(&sb);
541541
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);
543543
strbuf_reset(&sb);
544544
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
545545
write_file(sb.buf, "../..");

worktree.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void update_worktree_location(struct worktree *wt, const char *path_,
445445
strbuf_realpath(&path, path_, 1);
446446
strbuf_addf(&dotgit, "%s/.git", path.buf);
447447
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);
449449

450450
free(wt->path);
451451
wt->path = strbuf_detach(&path, NULL);
@@ -684,7 +684,7 @@ static void repair_gitfile(struct worktree *wt,
684684

685685
if (repair) {
686686
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);
688688
}
689689

690690
done:
@@ -742,7 +742,7 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
742742
if (!file_exists(dotgit.buf))
743743
goto done;
744744

745-
write_worktree_linking_files(dotgit, gitdir, is_relative_path);
745+
write_worktree_linking_files(dotgit.buf, gitdir.buf, is_relative_path);
746746
done:
747747
strbuf_release(&gitdir);
748748
strbuf_release(&dotgit);
@@ -913,7 +913,7 @@ void repair_worktree_at_path(const char *path,
913913

914914
if (repair) {
915915
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);
917917
}
918918
done:
919919
free(dotgit_contents);
@@ -1087,17 +1087,17 @@ int init_worktree_config(struct repository *r)
10871087
return res;
10881088
}
10891089

1090-
void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
1090+
void write_worktree_linking_files(const char *dotgit, const char *gitdir,
10911091
int use_relative_paths)
10921092
{
10931093
struct strbuf path = STRBUF_INIT;
10941094
struct strbuf repo = STRBUF_INIT;
10951095
struct strbuf tmp = STRBUF_INIT;
10961096

1097-
strbuf_addbuf(&path, &dotgit);
1097+
strbuf_addstr(&path, dotgit);
10981098
strbuf_strip_suffix(&path, "/.git");
10991099
strbuf_realpath(&path, path.buf, 1);
1100-
strbuf_addbuf(&repo, &gitdir);
1100+
strbuf_addstr(&repo, gitdir);
11011101
strbuf_strip_suffix(&repo, "/gitdir");
11021102
strbuf_realpath(&repo, repo.buf, 1);
11031103

@@ -1110,11 +1110,11 @@ void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
11101110
}
11111111

11121112
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));
11151115
} 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);
11181118
}
11191119

11201120
strbuf_release(&path);

worktree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int init_worktree_config(struct repository *r);
240240
* dotgit: "/path/to/foo/.git"
241241
* gitdir: "/path/to/repo/worktrees/foo/gitdir"
242242
*/
243-
void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
243+
void write_worktree_linking_files(const char *dotgit, const char *gitdir,
244244
int use_relative_paths);
245245

246246
#endif

0 commit comments

Comments
 (0)