Skip to content

Commit 4b6e18f

Browse files
derrickstoleegitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b489b9d commit 4b6e18f

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

branch.c

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

364364
while (worktrees[i]) {
365+
char *old;
365366
struct wt_status_state state = { 0 };
366367
struct worktree *wt = worktrees[i++];
367368

368369
if (wt->is_bare)
369370
continue;
370371

371-
if (wt->head_ref)
372-
strmap_put(&current_checked_out_branches,
373-
wt->head_ref,
374-
xstrdup(wt->path));
372+
if (wt->head_ref) {
373+
old = strmap_put(&current_checked_out_branches,
374+
wt->head_ref,
375+
xstrdup(wt->path));
376+
free(old);
377+
}
375378

376379
if (wt_status_check_rebase(wt, &state) &&
377380
(state.rebase_in_progress || state.rebase_interactive_in_progress) &&
378381
state.branch) {
379382
struct strbuf ref = STRBUF_INIT;
380383
strbuf_addf(&ref, "refs/heads/%s", state.branch);
381-
strmap_put(&current_checked_out_branches,
382-
ref.buf,
383-
xstrdup(wt->path));
384+
old = strmap_put(&current_checked_out_branches,
385+
ref.buf,
386+
xstrdup(wt->path));
387+
free(old);
384388
strbuf_release(&ref);
385389
}
386390
wt_status_state_free_buffers(&state);
@@ -389,9 +393,10 @@ static void prepare_checked_out_branches(void)
389393
state.branch) {
390394
struct strbuf ref = STRBUF_INIT;
391395
strbuf_addf(&ref, "refs/heads/%s", state.branch);
392-
strmap_put(&current_checked_out_branches,
393-
ref.buf,
394-
xstrdup(wt->path));
396+
old = strmap_put(&current_checked_out_branches,
397+
ref.buf,
398+
xstrdup(wt->path));
399+
free(old);
395400
strbuf_release(&ref);
396401
}
397402
wt_status_state_free_buffers(&state);

t/t2407-worktree-heads.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,32 @@ test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: worktree in rebase
9898
grep "refusing to fetch into branch" err
9999
'
100100

101+
test_expect_success 'refuse to overwrite when in error states' '
102+
test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
103+
test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
104+
105+
# Both branches are currently under rebase.
106+
mkdir -p .git/worktrees/wt-3/rebase-merge &&
107+
touch .git/worktrees/wt-3/rebase-merge/interactive &&
108+
echo refs/heads/fake-1 >.git/worktrees/wt-3/rebase-merge/head-name &&
109+
echo refs/heads/fake-2 >.git/worktrees/wt-3/rebase-merge/onto &&
110+
mkdir -p .git/worktrees/wt-4/rebase-merge &&
111+
touch .git/worktrees/wt-4/rebase-merge/interactive &&
112+
echo refs/heads/fake-2 >.git/worktrees/wt-4/rebase-merge/head-name &&
113+
echo refs/heads/fake-1 >.git/worktrees/wt-4/rebase-merge/onto &&
114+
115+
# Both branches are currently under bisect.
116+
touch .git/worktrees/wt-4/BISECT_LOG &&
117+
echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
118+
touch .git/worktrees/wt-1/BISECT_LOG &&
119+
echo refs/heads/fake-1 >.git/worktrees/wt-1/BISECT_START &&
120+
121+
for i in 1 2
122+
do
123+
test_must_fail git branch -f fake-$i HEAD 2>err &&
124+
grep "cannot force update the branch '\''fake-$i'\'' checked out at" err ||
125+
return 1
126+
done
127+
'
128+
101129
test_done

0 commit comments

Comments
 (0)