bundle-uri: plug leak in unbundle_from_file()

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>

Toon Claes committed Oct 10, 2024 at 11:12 UTC 6dab49b9fbbb63a5f58a3cc6e2295f01b1f628f0
1 file changed +13 -5
bundle-uri.c
+13 -5
@@ -368,17 +368,23 @@ static int unbundle_from_file(struct repository *r, const char *file)
368 struct strbuf bundle_ref = STRBUF_INIT;
369 size_t bundle_prefix_len;
370
371 - if ((bundle_fd = read_bundle_header(file, &header)) < 0)
372 - return 1;
371 + bundle_fd = read_bundle_header(file, &header);
372 + if (bundle_fd < 0) {
373 + result = 1;
374 + goto cleanup;
375 + }
376
377 /*
378 * Skip the reachability walk here, since we will be adding
379 * a reachable ref pointing to the new tips, which will reach
380 * the prerequisite commits.
381 */
379 - if ((result = unbundle(r, &header, bundle_fd, NULL,
380 - VERIFY_BUNDLE_QUIET | (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0))))
381 - return 1;
382 + result = unbundle(r, &header, bundle_fd, NULL,
383 + VERIFY_BUNDLE_QUIET | (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0));
384 + if (result) {
385 + result = 1;
386 + goto cleanup;
387 + }
388
389 /*
390 * 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)
413 0, UPDATE_REFS_MSG_ON_ERR);
414 }
415
416 +cleanup:
417 + strbuf_release(&bundle_ref);
418 bundle_header_release(&header);
419 return result;
420 }