Skip to content

Commit b5137cc

Browse files
derrickstoleegitster
authored andcommitted
branch: fix branch_checked_out() leaks
On 6/8/2022 4:08 PM, Derrick Stolee via GitGitGadget wrote: > This is a replacement for some patches from v2 of my 'git rebase > --update-refs' topic [1]. After some feedback from Philip, I've decided to > pull that topic while I rework how I track the refs to rewrite [2]. This > series moves forward with the branch_checked_out() helper that was a bit > more complicated than expected at first glance. This series is a culmination > of the discussion started by Junio at [3]. > Junio pointed out that patch 1 introduced a memory leak when a ref is checked out in multiple places. Here is a patch to fix that scenario. It applies cleanly on top of patch 4, so I include it as a new "patch 5". I will include it in any v2 of the full series, if needed. Thanks, -Stolee ---- >8 ---- From c3842b3 Mon Sep 17 00:00:00 2001 From: Derrick Stolee <[email protected]> Date: Mon, 13 Jun 2022 10:33:20 -0400 Subject: [PATCH 5/5] 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 e7339ca commit b5137cc

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
@@ -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: 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)