]> git.ipfire.org Git - thirdparty/git.git/commitdiff
midx-write: fix leaking hashfile on error cases
authorPatrick Steinhardt <ps@pks.im>
Thu, 22 Aug 2024 09:17:33 +0000 (11:17 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Aug 2024 16:18:04 +0000 (09:18 -0700)
When writing the MIDX file we first create the `struct hashfile` used to
write the trailer hash, and then afterwards we verify whether we can
actually write the MIDX in the first place. When we decide that we
can't, this leads to a memory leak because we never free the hash file
contents.

We could fix this by freeing the hashfile on the exit path. There is a
better option though: we can simply move the checks for the error
condition earlier. As there is no early exit between creating the
hashfile and finalizing it anymore this is sufficient to fix the memory
leak.

While at it, also move around the block checking for `ctx.entries_nr`.
This change is not required to fix the memory leak, but it feels natural
to move together all massaging of parameters before we go with them and
execute the actual logic.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx-write.c

index e3fa33203fa33f3856e3bf919b35e9457cf03e2d..07d98d494aacf6aa3421aee63cb0ab2cf22f3f33 100644 (file)
@@ -1308,6 +1308,18 @@ static int write_midx_internal(const char *object_dir,
                pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
                                        (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
 
+       if (ctx.nr - dropped_packs == 0) {
+               error(_("no pack files to index."));
+               result = 1;
+               goto cleanup;
+       }
+
+       if (!ctx.entries_nr) {
+               if (flags & MIDX_WRITE_BITMAP)
+                       warning(_("refusing to write multi-pack .bitmap without any objects"));
+               flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
+       }
+
        if (ctx.incremental) {
                struct strbuf lock_name = STRBUF_INIT;
 
@@ -1333,18 +1345,6 @@ static int write_midx_internal(const char *object_dir,
                f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
        }
 
-       if (ctx.nr - dropped_packs == 0) {
-               error(_("no pack files to index."));
-               result = 1;
-               goto cleanup;
-       }
-
-       if (!ctx.entries_nr) {
-               if (flags & MIDX_WRITE_BITMAP)
-                       warning(_("refusing to write multi-pack .bitmap without any objects"));
-               flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
-       }
-
        cf = init_chunkfile(f);
 
        add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,