Skip to content

Commit 26552cb

Browse files
jacob-kellergitster
authored andcommitted
reflog: close leak of reflog expire entry
find_cfg_ent() allocates a struct reflog_expire_entry_option via FLEX_ALLOC_MEM and inserts it into a linked list in the reflog_expire_options structure. The entries in this list are never freed, resulting in a leak in cmd_reflog_expire and the gc reflog expire maintenance task: Direct leak of 39 byte(s) in 1 object(s) allocated from: #0 0x7ff975ee6883 in calloc (/lib64/libasan.so.8+0xe6883) #1 0x0000010edada in xcalloc ../wrapper.c:154 #2 0x000000df0898 in find_cfg_ent ../reflog.c:28 git-for-windows#3 0x000000df0898 in reflog_expire_config ../reflog.c:70 git-for-windows#4 0x00000095c451 in configset_iter ../config.c:2116 git-for-windows#5 0x0000006d29e7 in git_config ../config.h:724 git-for-windows#6 0x0000006d29e7 in cmd_reflog_expire ../builtin/reflog.c:205 git-for-windows#7 0x0000006d504c in cmd_reflog ../builtin/reflog.c:419 git-for-windows#8 0x0000007e4054 in run_builtin ../git.c:480 git-for-windows#9 0x0000007e4054 in handle_builtin ../git.c:746 git-for-windows#10 0x0000007e8a35 in run_argv ../git.c:813 git-for-windows#11 0x0000007e8a35 in cmd_main ../git.c:953 git-for-windows#12 0x000000441e8f in main ../common-main.c:9 git-for-windows#13 0x7ff9754115f4 in __libc_start_call_main (/lib64/libc.so.6+0x35f4) git-for-windows#14 0x7ff9754116a7 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x36a7) git-for-windows#15 0x000000444184 in _start (/home/jekeller/libexec/git-core/git+0x444184) Close this leak by adding a reflog_clear_expire_config() function which iterates the linked list and frees its elements. Call it upon exit of cmd_reflog_expire() and reflog_expire_condition(). Add a basic test which covers this leak. While at it, cover the functionality from commit commit 3cb22b8 (Per-ref reflog expiry configuration, 2008-06-15). We've had this support for years, but lacked any tests. Co-developed-by: Jeff King <[email protected]> Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16bd9f2 commit 26552cb

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

builtin/gc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ static int reflog_expire_condition(struct gc_config *cfg UNUSED)
324324
count_reflog_entries, &data);
325325

326326
reflog_expiry_cleanup(&data.policy);
327+
reflog_clear_expire_config(&data.policy.opts);
327328
return data.count >= data.limit;
328329
}
329330

builtin/reflog.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
283283
&cb);
284284
free(ref);
285285
}
286+
287+
reflog_clear_expire_config(&opts);
288+
286289
return status;
287290
}
288291

reflog.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ int reflog_expire_config(const char *var, const char *value,
8181
return 0;
8282
}
8383

84+
void reflog_clear_expire_config(struct reflog_expire_options *opts)
85+
{
86+
struct reflog_expire_entry_option *ent = opts->entries, *tmp;
87+
88+
while (ent) {
89+
tmp = ent;
90+
ent = ent->next;
91+
free(tmp);
92+
}
93+
94+
opts->entries = NULL;
95+
opts->entries_tail = NULL;
96+
}
97+
8498
void reflog_expire_options_set_refname(struct reflog_expire_options *cb,
8599
const char *ref)
86100
{

reflog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct reflog_expire_options {
3434
int reflog_expire_config(const char *var, const char *value,
3535
const struct config_context *ctx, void *cb);
3636

37+
void reflog_clear_expire_config(struct reflog_expire_options *opts);
38+
3739
/*
3840
* Adapt the options so that they apply to the given refname. This applies any
3941
* per-reference reflog expiry configuration that may exist to the options.

t/t1410-reflog.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,32 @@ test_expect_success 'reflog drop --all with reference' '
673673
)
674674
'
675675

676+
test_expect_success 'expire with pattern config' '
677+
# Split refs/heads/ into two roots so we can apply config to each. Make
678+
# two branches per root to verify that config is applied correctly
679+
# multiple times.
680+
git branch root1/branch1 &&
681+
git branch root1/branch2 &&
682+
git branch root2/branch1 &&
683+
git branch root2/branch2 &&
684+
685+
test_config "gc.reflogexpire" "never" &&
686+
test_config "gc.refs/heads/root2/*.reflogExpire" "now" &&
687+
git reflog expire \
688+
root1/branch1 root1/branch2 \
689+
root2/branch1 root2/branch2 &&
690+
691+
cat >expect <<-\EOF &&
692+
root1/branch1@{0}
693+
root1/branch2@{0}
694+
EOF
695+
git log -g --branches="root*" --format=%gD >actual.raw &&
696+
# The sole reflog entry of each branch points to the same commit, so
697+
# the order in which they are shown is nondeterministic. We just care
698+
# about the what was expired (and what was not), so sort to get a known
699+
# order.
700+
sort <actual.raw >actual.sorted &&
701+
test_cmp expect actual.sorted
702+
'
703+
676704
test_done

0 commit comments

Comments
 (0)