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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 26, 2024 at 13:46 UTC dea4a9521ea134bbc12bce492b1ae467f5ff1bd1
3 files changed +45 -15
builtin/repack.c
+42 -15
@@ -85,17 +85,34 @@ static int repack_config(const char *var, const char *value,
85 run_update_server_info = git_config_bool(var, value);
86 return 0;
87 }
88 - if (!strcmp(var, "repack.cruftwindow"))
88 + if (!strcmp(var, "repack.cruftwindow")) {
89 + free(cruft_po_args->window);
90 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);
94 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);
98 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);
102 return git_config_string(&cruft_po_args->threads, var, value);
103 + }
104 return git_default_config(var, value, ctx, cb);
105 }
106
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 +
116 struct existing_packs {
117 struct string_list kept_packs;
118 struct string_list non_kept_packs;
@@ -1152,12 +1169,16 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1169 const char *unpack_unreachable = NULL;
1170 int keep_unreachable = 0;
1171 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 };
1174 int write_midx = 0;
1175 const char *cruft_expiration = NULL;
1176 const char *expire_to = NULL;
1177 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;
1182
1183 struct option builtin_repack_options[] = {
1184 OPT_BIT('a', NULL, &pack_everything,
@@ -1191,13 +1212,13 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1212 N_("with -A, do not loosen objects older than this")),
1213 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1214 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"),
1216 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"),
1218 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"),
1220 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"),
1222 N_("limits the maximum number of threads")),
1223 OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
1224 N_("maximum size of each packfile")),
@@ -1224,6 +1245,11 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1245 argc = parse_options(argc, argv, prefix, builtin_repack_options,
1246 git_repack_usage, 0);
1247
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 +
1253 if (delete_redundant && repository_format_precious_objects)
1254 die(_("cannot delete packs in a precious-objects repo"));
1255
@@ -1389,13 +1415,13 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1415 const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1416
1417 if (!cruft_po_args.window)
1392 - cruft_po_args.window = po_args.window;
1418 + cruft_po_args.window = xstrdup_or_null(po_args.window);
1419 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);
1421 if (!cruft_po_args.depth)
1396 - cruft_po_args.depth = po_args.depth;
1422 + cruft_po_args.depth = xstrdup_or_null(po_args.depth);
1423 if (!cruft_po_args.threads)
1398 - cruft_po_args.threads = po_args.threads;
1424 + cruft_po_args.threads = xstrdup_or_null(po_args.threads);
1425 if (!cruft_po_args.max_pack_size)
1426 cruft_po_args.max_pack_size = po_args.max_pack_size;
1427
@@ -1547,7 +1573,8 @@ cleanup:
1573 string_list_clear(&names, 1);
1574 existing_packs_release(&existing);
1575 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);
1578
1579 return ret;
1580 }
t/t5329-pack-objects-cruft.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='cruft pack related pack-objects tests'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 objdir=.git/objects
t/t7700-repack.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git repack works correctly'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "${TEST_DIRECTORY}/lib-bitmap.sh"
8 . "${TEST_DIRECTORY}/lib-midx.sh"