t/helper/test-read-midx.c: plug memory leak when selecting layer

Though our 'read-midx' test tool is capable of printing information about a single MIDX layer identified by its checksum, no caller in our test suite exercises this path. Unfortunately, there is a memory leak lurking in this (currently) unused path that would otherwise be exposed by the following commit. This occurs when providing a MIDX layer checksum other than the tip. As we walk over the MIDX chain trying to find the matching layer, we drop our reference to the top-most MIDX layer. Thus, our call to 'close_midx()' later on leaks memory between the top-most MIDX layer and the MIDX layer immediately following the specified one. Plug this leak by holding a reference to the tip of the MIDX chain, and ensure that we call `close_midx()` before terminating the test tool. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Feb 24, 2026 at 14:00 UTC dedf71f0b1b9a64d5aa7558ef45b26166d8d66fc
1 file changed +11 -6
t/helper/test-read-midx.c
+11 -6
@@ -26,9 +26,10 @@ static int read_midx_file(const char *object_dir, const char *checksum,
26 int show_objects)
27 {
28 uint32_t i;
29 - struct multi_pack_index *m;
29 + struct multi_pack_index *m, *tip;
30 + int ret = 0;
31
31 - m = setup_midx(object_dir);
32 + m = tip = setup_midx(object_dir);
33
34 if (!m)
35 return 1;
@@ -36,8 +37,11 @@ static int read_midx_file(const char *object_dir, const char *checksum,
37 if (checksum) {
38 while (m && strcmp(midx_get_checksum_hex(m), checksum))
39 m = m->base_midx;
39 - if (!m)
40 - return 1;
40 + if (!m) {
41 + ret = error(_("could not find MIDX with checksum %s"),
42 + checksum);
43 + goto out;
44 + }
45 }
46
47 printf("header: %08x %d %d %d %d\n",
@@ -82,9 +86,10 @@ static int read_midx_file(const char *object_dir, const char *checksum,
86 }
87 }
88
85 - close_midx(m);
89 +out:
90 + close_midx(tip);
91
87 - return 0;
92 + return ret;
93 }
94
95 static int read_midx_checksum(const char *object_dir)