From de64ef157c3f36b28e90ff24faa4d432bff381ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20G=C3=BCnd=C3=BCz?= Date: Wed, 25 Nov 2026 13:14:31 +0100 Subject: [PATCH] refs/debug: release the wrapped ref store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The debug ref store owns both the wrapped store and a copy of its backend descriptor. However, debug_release() only invokes the wrapped backend's release callback. This leaves the wrapped store, its gitdir and the copied descriptor allocated when the debug store is freed. Release the wrapped store through ref_store_release() to free its gitdir as well as its backend-specific resources, then free the store itself. Embed the copied backend descriptor in the debug store so that it shares the wrapper's lifetime. Add a test helper that reads HEAD through a repository's main ref store and then clears the repository. Exercise it with tracing on and off. With GIT_TRACE_REFS=1, the test fails under LeakSanitizer without this fix for both the files and reftable backends. Signed-off-by: Onur Gündüz --- refs/debug.c | 13 ++++++------- t/helper/test-repository.c | 19 ++++++++++++++++++- t/t1405-main-ref-store.sh | 7 +++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/refs/debug.c b/refs/debug.c index 639db0f26e1bbb..78d1eb5a986abd 100644 --- a/refs/debug.c +++ b/refs/debug.c @@ -8,6 +8,7 @@ static struct trace_key trace_refs = TRACE_KEY_INIT(REFS); struct debug_ref_store { struct ref_store base; struct ref_store *refs; + struct ref_storage_be backend; }; extern struct ref_storage_be refs_be_debug; @@ -15,27 +16,25 @@ extern struct ref_storage_be refs_be_debug; struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store) { struct debug_ref_store *res; - struct ref_storage_be *be_copy; if (!trace_want(&trace_refs)) { return store; } res = xmalloc(sizeof(struct debug_ref_store)); - be_copy = xmalloc(sizeof(*be_copy)); - *be_copy = refs_be_debug; - /* we never deallocate backends, so safe to copy the pointer. */ - be_copy->name = store->be->name; + res->backend = refs_be_debug; + res->backend.name = store->be->name; trace_printf_key(&trace_refs, "ref_store for %s\n", gitdir); res->refs = store; base_ref_store_init((struct ref_store *)res, store->repo, gitdir, - be_copy); + &res->backend); return (struct ref_store *)res; } static void debug_release(struct ref_store *refs) { struct debug_ref_store *drefs = (struct debug_ref_store *)refs; - drefs->refs->be->release(drefs->refs); + ref_store_release(drefs->refs); + free(drefs->refs); trace_printf_key(&trace_refs, "release\n"); } diff --git a/t/helper/test-repository.c b/t/helper/test-repository.c index 9ba94cdffa4c47..6184db863f9462 100644 --- a/t/helper/test-repository.c +++ b/t/helper/test-repository.c @@ -6,6 +6,7 @@ #include "environment.h" #include "hex.h" #include "object.h" +#include "refs.h" #include "repository.h" #include "setup.h" #include "tree.h" @@ -65,11 +66,27 @@ static void test_get_commit_tree_in_graph(const char *gitdir, repo_clear(&r); } +static void test_ref_store(const char *gitdir) +{ + struct repository r; + struct object_id oid; + + if (repo_init(&r, gitdir, NULL)) + die("cannot open repository %s", gitdir); + if (refs_read_ref(get_main_ref_store(&r), "HEAD", &oid)) + die("cannot read HEAD"); + repo_clear(&r); +} + int cmd__repository(int argc, const char **argv) { if (argc < 2) die("must have at least 2 arguments"); - if (!strcmp(argv[1], "parse_commit_in_graph")) { + if (!strcmp(argv[1], "ref-store")) { + if (argc != 3) + die("usage: test-tool repository ref-store "); + test_ref_store(argv[2]); + } else if (!strcmp(argv[1], "parse_commit_in_graph")) { struct object_id oid; if (argc < 5) die("not enough arguments"); diff --git a/t/t1405-main-ref-store.sh b/t/t1405-main-ref-store.sh index 6d8f401a2a9827..fca3b7415b562c 100755 --- a/t/t1405-main-ref-store.sh +++ b/t/t1405-main-ref-store.sh @@ -14,6 +14,13 @@ test_expect_success 'setup' ' test_commit one ' +for trace in 0 1 +do + test_expect_success "release main ref store with GIT_TRACE_REFS=$trace" ' + GIT_TRACE_REFS=$trace test-tool repository ref-store .git + ' +done + test_expect_success 'create_symref(FOO, refs/heads/main)' ' $RUN create-symref FOO refs/heads/main nothing && echo refs/heads/main >expected &&