From 26552cb62a5b375d4df651184941edf84f88a485 Mon Sep 17 00:00:00 2001 From: Jacob Keller Date: Tue, 22 Jul 2025 16:18:26 -0700 Subject: [PATCH] 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 #3 0x000000df0898 in reflog_expire_config ../reflog.c:70 #4 0x00000095c451 in configset_iter ../config.c:2116 #5 0x0000006d29e7 in git_config ../config.h:724 #6 0x0000006d29e7 in cmd_reflog_expire ../builtin/reflog.c:205 #7 0x0000006d504c in cmd_reflog ../builtin/reflog.c:419 #8 0x0000007e4054 in run_builtin ../git.c:480 #9 0x0000007e4054 in handle_builtin ../git.c:746 #10 0x0000007e8a35 in run_argv ../git.c:813 #11 0x0000007e8a35 in cmd_main ../git.c:953 #12 0x000000441e8f in main ../common-main.c:9 #13 0x7ff9754115f4 in __libc_start_call_main (/lib64/libc.so.6+0x35f4) #14 0x7ff9754116a7 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x36a7) #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 3cb22b8efe (Per-ref reflog expiry configuration, 2008-06-15). We've had this support for years, but lacked any tests. Co-developed-by: Jeff King Signed-off-by: Jacob Keller Signed-off-by: Junio C Hamano --- builtin/gc.c | 1 + builtin/reflog.c | 3 +++ reflog.c | 14 ++++++++++++++ reflog.h | 2 ++ t/t1410-reflog.sh | 28 ++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+) diff --git a/builtin/gc.c b/builtin/gc.c index 7dc94f243d..dea1c0d926 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -324,6 +324,7 @@ static int reflog_expire_condition(struct gc_config *cfg UNUSED) count_reflog_entries, &data); reflog_expiry_cleanup(&data.policy); + reflog_clear_expire_config(&data.policy.opts); return data.count >= data.limit; } diff --git a/builtin/reflog.c b/builtin/reflog.c index 3acaf3e32c..d4da41aaea 100644 --- a/builtin/reflog.c +++ b/builtin/reflog.c @@ -283,6 +283,9 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix, &cb); free(ref); } + + reflog_clear_expire_config(&opts); + return status; } diff --git a/reflog.c b/reflog.c index 15d81ebea9..e2a2f3ad3e 100644 --- a/reflog.c +++ b/reflog.c @@ -81,6 +81,20 @@ int reflog_expire_config(const char *var, const char *value, return 0; } +void reflog_clear_expire_config(struct reflog_expire_options *opts) +{ + struct reflog_expire_entry_option *ent = opts->entries, *tmp; + + while (ent) { + tmp = ent; + ent = ent->next; + free(tmp); + } + + opts->entries = NULL; + opts->entries_tail = NULL; +} + void reflog_expire_options_set_refname(struct reflog_expire_options *cb, const char *ref) { diff --git a/reflog.h b/reflog.h index 63bb56280f..74b3f3c4f0 100644 --- a/reflog.h +++ b/reflog.h @@ -34,6 +34,8 @@ struct reflog_expire_options { int reflog_expire_config(const char *var, const char *value, const struct config_context *ctx, void *cb); +void reflog_clear_expire_config(struct reflog_expire_options *opts); + /* * Adapt the options so that they apply to the given refname. This applies any * per-reference reflog expiry configuration that may exist to the options. diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh index 42b501f163..e30f87a358 100755 --- a/t/t1410-reflog.sh +++ b/t/t1410-reflog.sh @@ -673,4 +673,32 @@ test_expect_success 'reflog drop --all with reference' ' ) ' +test_expect_success 'expire with pattern config' ' + # Split refs/heads/ into two roots so we can apply config to each. Make + # two branches per root to verify that config is applied correctly + # multiple times. + git branch root1/branch1 && + git branch root1/branch2 && + git branch root2/branch1 && + git branch root2/branch2 && + + test_config "gc.reflogexpire" "never" && + test_config "gc.refs/heads/root2/*.reflogExpire" "now" && + git reflog expire \ + root1/branch1 root1/branch2 \ + root2/branch1 root2/branch2 && + + cat >expect <<-\EOF && + root1/branch1@{0} + root1/branch2@{0} + EOF + git log -g --branches="root*" --format=%gD >actual.raw && + # The sole reflog entry of each branch points to the same commit, so + # the order in which they are shown is nondeterministic. We just care + # about the what was expired (and what was not), so sort to get a known + # order. + sort actual.sorted && + test_cmp expect actual.sorted +' + test_done