]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/helper/test-read-midx.c: plug memory leak when selecting layer
authorTaylor Blau <me@ttaylorr.com>
Sat, 6 Dec 2025 20:31:43 +0000 (15:31 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sat, 6 Dec 2025 22:38:10 +0000 (07:38 +0900)
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>
t/helper/test-read-midx.c

index dee603b3cd0e153ffd67dbb4c4744171aa174f3e..6e03aabca79c6c383053700ddd5445a665e164b3 100644 (file)
@@ -26,9 +26,10 @@ static int read_midx_file(const char *object_dir, const char *checksum,
                          int show_objects)
 {
        uint32_t i;
-       struct multi_pack_index *m;
+       struct multi_pack_index *m, *tip;
+       int ret = 0;
 
-       m = setup_midx(object_dir);
+       m = tip = setup_midx(object_dir);
 
        if (!m)
                return 1;
@@ -36,8 +37,11 @@ static int read_midx_file(const char *object_dir, const char *checksum,
        if (checksum) {
                while (m && strcmp(get_midx_checksum(m), checksum))
                        m = m->base_midx;
-               if (!m)
-                       return 1;
+               if (!m) {
+                       ret = error(_("could not find MIDX with checksum %s"),
+                                   checksum);
+                       goto out;
+               }
        }
 
        printf("header: %08x %d %d %d %d\n",
@@ -82,9 +86,10 @@ static int read_midx_file(const char *object_dir, const char *checksum,
                }
        }
 
-       close_midx(m);
+out:
+       close_midx(tip);
 
-       return 0;
+       return ret;
 }
 
 static int read_midx_checksum(const char *object_dir)