Skip to content

Commit a75dc71

Browse files
pks-tgitster
authored andcommitted
builtin/pack-refs: release allocated memory
Some of the command line options in `cmd_pack_refs()` require us to allocate memory. This memory is never released and thus leaking, but we paper over this leak by declaring the respective variables as `static` function-level variables, which is somewhat awkward. Refactor the code to release the allocated memory and drop the `static` declaration. While at it, remove the useless `flags` variable. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f89356d commit a75dc71

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

builtin/pack-refs.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ static char const * const pack_refs_usage[] = {
1313

1414
int cmd_pack_refs(int argc, const char **argv, const char *prefix)
1515
{
16-
unsigned int flags = PACK_REFS_PRUNE;
17-
static struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
18-
static struct string_list included_refs = STRING_LIST_INIT_NODUP;
19-
struct pack_refs_opts pack_refs_opts = { .exclusions = &excludes,
20-
.includes = &included_refs,
21-
.flags = flags };
22-
static struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
16+
struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
17+
struct string_list included_refs = STRING_LIST_INIT_NODUP;
18+
struct pack_refs_opts pack_refs_opts = {
19+
.exclusions = &excludes,
20+
.includes = &included_refs,
21+
.flags = PACK_REFS_PRUNE,
22+
};
23+
struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
2324
struct string_list_item *item;
2425
int pack_all = 0;
26+
int ret;
2527

2628
struct option opts[] = {
2729
OPT_BOOL(0, "all", &pack_all, N_("pack everything")),
@@ -45,5 +47,10 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
4547
if (!pack_refs_opts.includes->nr)
4648
string_list_append(pack_refs_opts.includes, "refs/tags/*");
4749

48-
return refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
50+
ret = refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
51+
52+
clear_ref_exclusions(&excludes);
53+
string_list_clear(&included_refs, 0);
54+
string_list_clear(&option_excluded_refs, 0);
55+
return ret;
4956
}

0 commit comments

Comments
 (0)