Skip to content

Commit 5cb2827

Browse files
avargitster
authored andcommitted
pack-objects: lazily set up "struct rev_info", don't leak
In the preceding [1] (pack-objects: move revs out of get_object_list(), 2022-03-22) the "repo_init_revisions()" was moved to cmd_pack_objects() so that it unconditionally took place for all invocations of "git pack-objects". We'd thus start leaking memory, which is easily reproduced in e.g. git.git by feeding e83c516 (Initial revision of "git", the information manager from hell, 2005-04-07) to "git pack-objects"; $ echo e83c516 | ./git pack-objects initial [...] ==19130==ERROR: LeakSanitizer: detected memory leaks Direct leak of 7120 byte(s) in 1 object(s) allocated from: #0 0x455308 in __interceptor_malloc (/home/avar/g/git/git+0x455308) #1 0x75b399 in do_xmalloc /home/avar/g/git/wrapper.c:41:8 #2 0x75b356 in xmalloc /home/avar/g/git/wrapper.c:62:9 #3 0x5d7609 in prep_parse_options /home/avar/g/git/diff.c:5647:2 #4 0x5d415a in repo_diff_setup /home/avar/g/git/diff.c:4621:2 #5 0x6dffbb in repo_init_revisions /home/avar/g/git/revision.c:1853:2 #6 0x4f599d in cmd_pack_objects /home/avar/g/git/builtin/pack-objects.c:3980:2 #7 0x4592ca in run_builtin /home/avar/g/git/git.c:465:11 #8 0x457d81 in handle_builtin /home/avar/g/git/git.c:718:3 #9 0x458ca5 in run_argv /home/avar/g/git/git.c:785:4 #10 0x457b40 in cmd_main /home/avar/g/git/git.c:916:19 #11 0x562259 in main /home/avar/g/git/common-main.c:56:11 #12 0x7fce792ac7ec in __libc_start_main csu/../csu/libc-start.c:332:16 #13 0x4300f9 in _start (/home/avar/g/git/git+0x4300f9) SUMMARY: LeakSanitizer: 7120 byte(s) leaked in 1 allocation(s). Aborted Narrowly fixing that commit would have been easy, just add call repo_init_revisions() right before get_object_list(), which is effectively what was done before that commit. But an unstated constraint when setting it up early is that it was needed for the subsequent [2] (pack-objects: parse --filter directly into revs.filter, 2022-03-22), i.e. we might have a --filter command-line option, and need to either have the "struct rev_info" setup when we encounter that option, or later. Let's just change the control flow so that we'll instead set up the "struct rev_info" only when we need it. Doing so leads to a bit more verbosity, but it's a lot clearer what we're doing and why. An earlier version of this commit[3] went behind opt_parse_list_objects_filter()'s back by faking up a "struct option" before calling it. Let's avoid that and instead create a blessed API for this pattern. We could furthermore combine the two get_object_list() invocations here by having repo_init_revisions() invoked on &pfd.revs, but I think clearly separating the two makes the flow clearer. Likewise redundantly but explicitly (i.e. redundant v.s. a "{ 0 }") "0" to "have_revs" early in cmd_pack_objects(). While we're at it add parentheses around the arguments to the OPT_* macros in in list-objects-filter-options.h, as we need to change those lines anyway. It doesn't matter in this case, but is good general practice. 1. https://lore.kernel.org/git/619b757d98465dbc4995bdc11a5282fbfcbd3daa.1647970119.git.gitgitgadget@gmail.com 2. https://lore.kernel.org/git/97de926904988b89b5663bd4c59c011a1723a8f5.1647970119.git.gitgitgadget@gmail.com 3. https://lore.kernel.org/git/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ba221e commit 5cb2827

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

builtin/pack-objects.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,6 +3857,21 @@ static int option_parse_unpack_unreachable(const struct option *opt,
38573857
return 0;
38583858
}
38593859

3860+
struct po_filter_data {
3861+
unsigned have_revs:1;
3862+
struct rev_info revs;
3863+
};
3864+
3865+
static struct list_objects_filter_options *po_filter_revs_init(void *value)
3866+
{
3867+
struct po_filter_data *data = value;
3868+
3869+
repo_init_revisions(the_repository, &data->revs, NULL);
3870+
data->have_revs = 1;
3871+
3872+
return &data->revs.filter;
3873+
}
3874+
38603875
int cmd_pack_objects(int argc, const char **argv, const char *prefix)
38613876
{
38623877
int use_internal_rev_list = 0;
@@ -3867,7 +3882,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
38673882
int rev_list_index = 0;
38683883
int stdin_packs = 0;
38693884
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
3870-
struct rev_info revs;
3885+
struct po_filter_data pfd = { .have_revs = 0 };
38713886

38723887
struct option pack_objects_options[] = {
38733888
OPT_SET_INT('q', "quiet", &progress,
@@ -3954,7 +3969,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
39543969
&write_bitmap_index,
39553970
N_("write a bitmap index if possible"),
39563971
WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
3957-
OPT_PARSE_LIST_OBJECTS_FILTER(&revs.filter),
3972+
OPT_PARSE_LIST_OBJECTS_FILTER_INIT(&pfd, po_filter_revs_init),
39583973
OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
39593974
N_("handling for missing objects"), PARSE_OPT_NONEG,
39603975
option_parse_missing_action),
@@ -3973,8 +3988,6 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
39733988

39743989
read_replace_refs = 0;
39753990

3976-
repo_init_revisions(the_repository, &revs, NULL);
3977-
39783991
sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
39793992
if (the_repository->gitdir) {
39803993
prepare_repo_settings(the_repository);
@@ -4076,7 +4089,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
40764089
if (!rev_list_all || !rev_list_reflog || !rev_list_index)
40774090
unpack_unreachable_expiration = 0;
40784091

4079-
if (revs.filter.choice) {
4092+
if (pfd.have_revs && pfd.revs.filter.choice) {
40804093
if (!pack_to_stdout)
40814094
die(_("cannot use --filter without --stdout"));
40824095
if (stdin_packs)
@@ -4152,7 +4165,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
41524165
add_unreachable_loose_objects();
41534166
} else if (!use_internal_rev_list) {
41544167
read_object_list_from_stdin();
4168+
} else if (pfd.have_revs) {
4169+
get_object_list(&pfd.revs, rp.nr, rp.v);
41554170
} else {
4171+
struct rev_info revs;
4172+
4173+
repo_init_revisions(the_repository, &revs, NULL);
41564174
get_object_list(&revs, rp.nr, rp.v);
41574175
}
41584176
cleanup_preferred_base();

list-objects-filter-options.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ int opt_parse_list_objects_filter(const struct option *opt,
285285
const char *arg, int unset)
286286
{
287287
struct list_objects_filter_options *filter_options = opt->value;
288+
opt_lof_init init = (opt_lof_init)opt->defval;
289+
290+
if (init)
291+
filter_options = init(opt->value);
288292

289293
if (unset || !arg)
290294
list_objects_filter_set_no_filter(filter_options);

list-objects-filter-options.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,31 @@ void parse_list_objects_filter(
104104
struct list_objects_filter_options *filter_options,
105105
const char *arg);
106106

107+
/**
108+
* The opt->value to opt_parse_list_objects_filter() is either a
109+
* "struct list_objects_filter_option *" when using
110+
* OPT_PARSE_LIST_OBJECTS_FILTER().
111+
*
112+
* Or, if using no "struct option" field is used by the callback,
113+
* except the "defval" which is expected to be an "opt_lof_init"
114+
* function, which is called with the "opt->value" and must return a
115+
* pointer to the ""struct list_objects_filter_option *" to be used.
116+
*
117+
* The OPT_PARSE_LIST_OBJECTS_FILTER_INIT() can be used e.g. the
118+
* "struct list_objects_filter_option" is embedded in a "struct
119+
* rev_info", which the "defval" could be tasked with lazily
120+
* initializing. See cmd_pack_objects() for an example.
121+
*/
107122
int opt_parse_list_objects_filter(const struct option *opt,
108123
const char *arg, int unset);
124+
typedef struct list_objects_filter_options *(*opt_lof_init)(void *);
125+
#define OPT_PARSE_LIST_OBJECTS_FILTER_INIT(fo, init) \
126+
{ OPTION_CALLBACK, 0, "filter", (fo), N_("args"), \
127+
N_("object filtering"), 0, opt_parse_list_objects_filter, \
128+
(intptr_t)(init) }
109129

110130
#define OPT_PARSE_LIST_OBJECTS_FILTER(fo) \
111-
OPT_CALLBACK(0, "filter", fo, N_("args"), \
112-
N_("object filtering"), \
113-
opt_parse_list_objects_filter)
131+
OPT_PARSE_LIST_OBJECTS_FILTER_INIT((fo), NULL)
114132

115133
/*
116134
* Translates abbreviated numbers in the filter's filter_spec into their

0 commit comments

Comments
 (0)