Skip to content

Commit c3842b3

Browse files
committed
branch: fix branch_checked_out() leaks
The branch_checked_out() method populates a strmap linking a refname to a worktree that has that branch checked out. While unlikely, it is possible that a bug or filesystem manipulation could create a scenario where the same ref is checked out in multiple places. Further, there are some states in an interactive rebase where HEAD and REBASE_HEAD point to the same ref, leading to multiple insertions into the strmap. In either case, the strmap_put() method returns the old value which is leaked. Update branch_checked_out() to consume that pointer and free it. Add a test in t2407 that checks this erroneous case. The test "checks itself" by first confirming that the filesystem manipulations it makes trigger the branch_checked_out() logic, and then sets up similar manipulations to make it look like there are multiple worktrees pointing to the same ref. While TEST_PASSES_SANITIZE_LEAK would be helpful to demonstrate the leakage and prevent it in the future, t2407 uses helpers such as 'git clone' that cause the test to fail under that mode. Signed-off-by: Derrick Stolee <[email protected]>
1 parent af645b4 commit c3842b3

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

branch.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,25 +385,29 @@ static void prepare_checked_out_branches(void)
385385
worktrees = get_worktrees();
386386

387387
while (worktrees[i]) {
388+
char *old;
388389
struct wt_status_state state = { 0 };
389390
struct worktree *wt = worktrees[i++];
390391

391392
if (wt->is_bare)
392393
continue;
393394

394-
if (wt->head_ref)
395-
strmap_put(&current_checked_out_branches,
396-
wt->head_ref,
397-
xstrdup(wt->path));
395+
if (wt->head_ref) {
396+
old = strmap_put(&current_checked_out_branches,
397+
wt->head_ref,
398+
xstrdup(wt->path));
399+
free(old);
400+
}
398401

399402
if (wt_status_check_rebase(wt, &state) &&
400403
(state.rebase_in_progress || state.rebase_interactive_in_progress) &&
401404
state.branch) {
402405
struct strbuf ref = STRBUF_INIT;
403406
strbuf_addf(&ref, "refs/heads/%s", state.branch);
404-
strmap_put(&current_checked_out_branches,
405-
ref.buf,
406-
xstrdup(wt->path));
407+
old = strmap_put(&current_checked_out_branches,
408+
ref.buf,
409+
xstrdup(wt->path));
410+
free(old);
407411
strbuf_release(&ref);
408412
}
409413
wt_status_state_free_buffers(&state);
@@ -412,9 +416,10 @@ static void prepare_checked_out_branches(void)
412416
state.branch) {
413417
struct strbuf ref = STRBUF_INIT;
414418
strbuf_addf(&ref, "refs/heads/%s", state.branch);
415-
strmap_put(&current_checked_out_branches,
416-
ref.buf,
417-
xstrdup(wt->path));
419+
old = strmap_put(&current_checked_out_branches,
420+
ref.buf,
421+
xstrdup(wt->path));
422+
free(old);
418423
strbuf_release(&ref);
419424
}
420425
wt_status_state_free_buffers(&state);

t/t2407-worktree-heads.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,43 @@ test_expect_success 'refuse to overwrite: worktree in rebase' '
7878
grep "refusing to fetch into branch '\''refs/heads/wt-4'\''" err
7979
'
8080

81+
test_expect_success 'refuse to overwrite when in error states' '
82+
test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
83+
test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
84+
85+
git branch -f fake1 &&
86+
mkdir -p .git/worktrees/wt-3/rebase-merge &&
87+
touch .git/worktrees/wt-3/rebase-merge/interactive &&
88+
echo refs/heads/fake1 >.git/worktrees/wt-3/rebase-merge/head-name &&
89+
echo refs/heads/fake2 >.git/worktrees/wt-3/rebase-merge/onto &&
90+
91+
git branch -f fake2 &&
92+
touch .git/worktrees/wt-4/BISECT_LOG &&
93+
echo refs/heads/fake2 >.git/worktrees/wt-4/BISECT_START &&
94+
95+
# First, ensure we prevent writing when only one reason to fail.
96+
for i in 1 2
97+
do
98+
test_must_fail git branch -f fake$i HEAD 2>err &&
99+
grep "cannot force update the branch '\''fake$i'\'' checked out at" err ||
100+
return 1
101+
done &&
102+
103+
# Second, set up duplicate values.
104+
mkdir -p .git/worktrees/wt-4/rebase-merge &&
105+
touch .git/worktrees/wt-4/rebase-merge/interactive &&
106+
echo refs/heads/fake2 >.git/worktrees/wt-4/rebase-merge/head-name &&
107+
echo refs/heads/fake1 >.git/worktrees/wt-4/rebase-merge/onto &&
108+
109+
touch .git/worktrees/wt-1/BISECT_LOG &&
110+
echo refs/heads/fake1 >.git/worktrees/wt-1/BISECT_START &&
111+
112+
for i in 1 2
113+
do
114+
test_must_fail git branch -f fake$i HEAD 2>err &&
115+
grep "cannot force update the branch '\''fake$i'\'' checked out at" err ||
116+
return 1
117+
done
118+
'
119+
81120
test_done

0 commit comments

Comments
 (0)