Skip to content

Commit 6dab49b

Browse files
To1negitster
authored andcommitted
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 <[email protected]> Acked-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 777489f commit 6dab49b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

bundle-uri.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,23 @@ static int unbundle_from_file(struct repository *r, const char *file)
368368
struct strbuf bundle_ref = STRBUF_INIT;
369369
size_t bundle_prefix_len;
370370

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+
}
373376

374377
/*
375378
* Skip the reachability walk here, since we will be adding
376379
* a reachable ref pointing to the new tips, which will reach
377380
* the prerequisite commits.
378381
*/
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+
}
382388

383389
/*
384390
* 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)
407413
0, UPDATE_REFS_MSG_ON_ERR);
408414
}
409415

416+
cleanup:
417+
strbuf_release(&bundle_ref);
410418
bundle_header_release(&header);
411419
return result;
412420
}

0 commit comments

Comments
 (0)