Skip to content

Commit dea4a95

Browse files
pks-tgitster
authored andcommitted
builtin/repack: fix leaking configuration
When repacking, we assemble git-pack-objects(1) arguments both for the "normal" pack and for the cruft pack. This configuration gets populated with a bunch of `OPT_PASSTHRU` options that we end up passing to the child process. These options are allocated, but never free'd. Create a new `pack_objects_args_release()` function that releases the memory for us and call it for both sets of options. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6932ec8 commit dea4a95

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

builtin/repack.c

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,34 @@ static int repack_config(const char *var, const char *value,
8585
run_update_server_info = git_config_bool(var, value);
8686
return 0;
8787
}
88-
if (!strcmp(var, "repack.cruftwindow"))
88+
if (!strcmp(var, "repack.cruftwindow")) {
89+
free(cruft_po_args->window);
8990
return git_config_string(&cruft_po_args->window, var, value);
90-
if (!strcmp(var, "repack.cruftwindowmemory"))
91+
}
92+
if (!strcmp(var, "repack.cruftwindowmemory")) {
93+
free(cruft_po_args->window_memory);
9194
return git_config_string(&cruft_po_args->window_memory, var, value);
92-
if (!strcmp(var, "repack.cruftdepth"))
95+
}
96+
if (!strcmp(var, "repack.cruftdepth")) {
97+
free(cruft_po_args->depth);
9398
return git_config_string(&cruft_po_args->depth, var, value);
94-
if (!strcmp(var, "repack.cruftthreads"))
99+
}
100+
if (!strcmp(var, "repack.cruftthreads")) {
101+
free(cruft_po_args->threads);
95102
return git_config_string(&cruft_po_args->threads, var, value);
103+
}
96104
return git_default_config(var, value, ctx, cb);
97105
}
98106

107+
static void pack_objects_args_release(struct pack_objects_args *args)
108+
{
109+
free(args->window);
110+
free(args->window_memory);
111+
free(args->depth);
112+
free(args->threads);
113+
list_objects_filter_release(&args->filter_options);
114+
}
115+
99116
struct existing_packs {
100117
struct string_list kept_packs;
101118
struct string_list non_kept_packs;
@@ -1152,12 +1169,16 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11521169
const char *unpack_unreachable = NULL;
11531170
int keep_unreachable = 0;
11541171
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1155-
struct pack_objects_args po_args = {NULL};
1156-
struct pack_objects_args cruft_po_args = {NULL};
1172+
struct pack_objects_args po_args = { 0 };
1173+
struct pack_objects_args cruft_po_args = { 0 };
11571174
int write_midx = 0;
11581175
const char *cruft_expiration = NULL;
11591176
const char *expire_to = NULL;
11601177
const char *filter_to = NULL;
1178+
const char *opt_window = NULL;
1179+
const char *opt_window_memory = NULL;
1180+
const char *opt_depth = NULL;
1181+
const char *opt_threads = NULL;
11611182

11621183
struct option builtin_repack_options[] = {
11631184
OPT_BIT('a', NULL, &pack_everything,
@@ -1191,13 +1212,13 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11911212
N_("with -A, do not loosen objects older than this")),
11921213
OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
11931214
N_("with -a, repack unreachable objects")),
1194-
OPT_STRING(0, "window", &po_args.window, N_("n"),
1215+
OPT_STRING(0, "window", &opt_window, N_("n"),
11951216
N_("size of the window used for delta compression")),
1196-
OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
1217+
OPT_STRING(0, "window-memory", &opt_window_memory, N_("bytes"),
11971218
N_("same as the above, but limit memory size instead of entries count")),
1198-
OPT_STRING(0, "depth", &po_args.depth, N_("n"),
1219+
OPT_STRING(0, "depth", &opt_depth, N_("n"),
11991220
N_("limits the maximum delta depth")),
1200-
OPT_STRING(0, "threads", &po_args.threads, N_("n"),
1221+
OPT_STRING(0, "threads", &opt_threads, N_("n"),
12011222
N_("limits the maximum number of threads")),
12021223
OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
12031224
N_("maximum size of each packfile")),
@@ -1224,6 +1245,11 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
12241245
argc = parse_options(argc, argv, prefix, builtin_repack_options,
12251246
git_repack_usage, 0);
12261247

1248+
po_args.window = xstrdup_or_null(opt_window);
1249+
po_args.window_memory = xstrdup_or_null(opt_window_memory);
1250+
po_args.depth = xstrdup_or_null(opt_depth);
1251+
po_args.threads = xstrdup_or_null(opt_threads);
1252+
12271253
if (delete_redundant && repository_format_precious_objects)
12281254
die(_("cannot delete packs in a precious-objects repo"));
12291255

@@ -1389,13 +1415,13 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
13891415
const char *pack_prefix = find_pack_prefix(packdir, packtmp);
13901416

13911417
if (!cruft_po_args.window)
1392-
cruft_po_args.window = po_args.window;
1418+
cruft_po_args.window = xstrdup_or_null(po_args.window);
13931419
if (!cruft_po_args.window_memory)
1394-
cruft_po_args.window_memory = po_args.window_memory;
1420+
cruft_po_args.window_memory = xstrdup_or_null(po_args.window_memory);
13951421
if (!cruft_po_args.depth)
1396-
cruft_po_args.depth = po_args.depth;
1422+
cruft_po_args.depth = xstrdup_or_null(po_args.depth);
13971423
if (!cruft_po_args.threads)
1398-
cruft_po_args.threads = po_args.threads;
1424+
cruft_po_args.threads = xstrdup_or_null(po_args.threads);
13991425
if (!cruft_po_args.max_pack_size)
14001426
cruft_po_args.max_pack_size = po_args.max_pack_size;
14011427

@@ -1547,7 +1573,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
15471573
string_list_clear(&names, 1);
15481574
existing_packs_release(&existing);
15491575
free_pack_geometry(&geometry);
1550-
list_objects_filter_release(&po_args.filter_options);
1576+
pack_objects_args_release(&po_args);
1577+
pack_objects_args_release(&cruft_po_args);
15511578

15521579
return ret;
15531580
}

t/t5329-pack-objects-cruft.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='cruft pack related pack-objects tests'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
objdir=.git/objects

t/t7700-repack.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git repack works correctly'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67
. "${TEST_DIRECTORY}/lib-bitmap.sh"
78
. "${TEST_DIRECTORY}/lib-midx.sh"

0 commit comments

Comments
 (0)