]> git.ipfire.org Git - thirdparty/git.git/commitdiff
bundle-uri: plug leak in unbundle_from_file()
authorToon Claes <toon@iotcl.com>
Thu, 10 Oct 2024 09:12:49 +0000 (11:12 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 10 Oct 2024 18:47:24 +0000 (11:47 -0700)
The function `unbundle_from_file()` has two memory leaks:

  - We do not release the `struct bundle_header header` when hitting
    errors because we return early without any cleanup.

  - We do not release the `struct strbuf bundle_ref` at all.

Plug these leaks by creating a common exit path where both of these
variables are released.

While at it, refactor the code such that the variable assignments do not
happen inside the conditional statement itself according to our coding
style.

Signed-off-by: Toon Claes <toon@iotcl.com>
Acked-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
bundle-uri.c

index 4b1a2e293757b9d196fdf5295563b1e7c18acb82..0df66e28729e5cd8649850d641bd385500a94b45 100644 (file)
@@ -368,17 +368,23 @@ static int unbundle_from_file(struct repository *r, const char *file)
        struct strbuf bundle_ref = STRBUF_INIT;
        size_t bundle_prefix_len;
 
-       if ((bundle_fd = read_bundle_header(file, &header)) < 0)
-               return 1;
+       bundle_fd = read_bundle_header(file, &header);
+       if (bundle_fd < 0) {
+               result = 1;
+               goto cleanup;
+       }
 
        /*
         * Skip the reachability walk here, since we will be adding
         * a reachable ref pointing to the new tips, which will reach
         * the prerequisite commits.
         */
-       if ((result = unbundle(r, &header, bundle_fd, NULL,
-                              VERIFY_BUNDLE_QUIET | (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0))))
-               return 1;
+       result = unbundle(r, &header, bundle_fd, NULL,
+                         VERIFY_BUNDLE_QUIET | (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0));
+       if (result) {
+               result = 1;
+               goto cleanup;
+       }
 
        /*
         * Convert all refs/heads/ from the bundle into refs/bundles/
@@ -407,6 +413,8 @@ static int unbundle_from_file(struct repository *r, const char *file)
                                0, UPDATE_REFS_MSG_ON_ERR);
        }
 
+cleanup:
+       strbuf_release(&bundle_ref);
        bundle_header_release(&header);
        return result;
 }