]> git.ipfire.org Git - thirdparty/git.git/commitdiff
midx-write: use cleanup when incremental midx fails
authorDerrick Stolee <stolee@gmail.com>
Fri, 5 Sep 2025 19:26:15 +0000 (19:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 5 Sep 2025 19:32:00 +0000 (12:32 -0700)
The incremental mode of writing a multi-pack-index has a few extra
conditions that could lead to failure, but these are currently
short-ciruiting with 'return -1' instead of setting the method's
'result' variable and going to the cleanup tag.

Replace these returns with gotos to avoid memory issues when exiting
early due to error conditions.

Unfortunately, these error conditions are difficult to reproduce with
test cases, which is perhaps one reason why the memory loss was not
caught by existing test cases in memory tracking modes.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx-write.c

index 4c8af23861afcde9293013eeab71bfe8cd019c7b..e8864386d8d84ea4b30151c777d28a0483682620 100644 (file)
@@ -1345,13 +1345,15 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
                incr = mks_tempfile_m(midx_name.buf, 0444);
                if (!incr) {
                        error(_("unable to create temporary MIDX layer"));
-                       return -1;
+                       result = -1;
+                       goto cleanup;
                }
 
                if (adjust_shared_perm(r, get_tempfile_path(incr))) {
                        error(_("unable to adjust shared permissions for '%s'"),
                              get_tempfile_path(incr));
-                       return -1;
+                       result = -1;
+                       goto cleanup;
                }
 
                f = hashfd(r->hash_algo, get_tempfile_fd(incr),
@@ -1451,18 +1453,22 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 
                if (!chainf) {
                        error_errno(_("unable to open multi-pack-index chain file"));
-                       return -1;
+                       result = -1;
+                       goto cleanup;
                }
 
-               if (link_midx_to_chain(ctx.base_midx) < 0)
-                       return -1;
+               if (link_midx_to_chain(ctx.base_midx) < 0) {
+                       result = -1;
+                       goto cleanup;
+               }
 
                get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
                                            object_dir, midx_hash, MIDX_EXT_MIDX);
 
                if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
                        error_errno(_("unable to rename new multi-pack-index layer"));
-                       return -1;
+                       result = -1;
+                       goto cleanup;
                }
 
                strbuf_release(&final_midx_name);