Skip to content

Commit 9e39acc

Browse files
ttaylorrgitster
authored andcommitted
builtin/pack-objects.c: don't leak memory via arguments
When constructing arguments to pass to setup_revision(), pack-objects only frees the memory used by that array after calling get_object_list(). Ensure that we call strvec_clear() whether or not we use the arguments array by cleaning up whenever we exit the function (and rewriting one early return to jump to a label which frees the memory and then returns). We could avoid setting this array up altogether unless we are in the if-else block that calls get_object_list(), but setting up the argument array is intermingled with lots of other side-effects, e.g.: if (exclude_promisor_objects) { use_internal_rev_list = 1; fetch_if_missing = 0; strvec_push(&rp, "--exclude-promisor-objects"); } So it would be awkward to check exclude_promisor_objects twice: first to set use_internal_rev_list and fetch_if_missing, and then again above get_object_list() to push the relevant argument onto the array. Instead, leave the array's construction alone and make sure to free it unconditionally. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f4c350 commit 9e39acc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

builtin/pack-objects.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4148,11 +4148,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
41484148
read_packs_list_from_stdin();
41494149
if (rev_list_unpacked)
41504150
add_unreachable_loose_objects();
4151-
} else if (!use_internal_rev_list)
4151+
} else if (!use_internal_rev_list) {
41524152
read_object_list_from_stdin();
4153-
else {
4153+
} else {
41544154
get_object_list(rp.nr, rp.v);
4155-
strvec_clear(&rp);
41564155
}
41574156
cleanup_preferred_base();
41584157
if (include_tag && nr_result)
@@ -4162,7 +4161,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
41624161
the_repository);
41634162

41644163
if (non_empty && !nr_result)
4165-
return 0;
4164+
goto cleanup;
41664165
if (nr_result) {
41674166
trace2_region_enter("pack-objects", "prepare-pack",
41684167
the_repository);
@@ -4183,5 +4182,9 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
41834182
" pack-reused %"PRIu32),
41844183
written, written_delta, reused, reused_delta,
41854184
reuse_packfile_objects);
4185+
4186+
cleanup:
4187+
strvec_clear(&rp);
4188+
41864189
return 0;
41874190
}

0 commit comments

Comments
 (0)