Skip to content

Commit 8286296

Browse files
ahuntgitster
authored andcommitted
parse-options: don't leak alias help messages
preprocess_options() allocates new strings for help messages for OPTION_ALIAS. Therefore we also need to clean those help messages up when freeing the returned options. First introduced in: 7c28058 (parse-options: teach "git cmd -h" to show alias as alias, 2020-03-16) The preprocessed options themselves no longer contain any indication that a given option is/was an alias: the easiest and fastest way to figure it out is to look back at the original options. Alternatively we could iterate over the alias_groups list - but that would require nested looping and is likely to be a (little) less efficient. As far as I can tell, parse_options() is only ever used once per command, and the help messages are small - hence this leak has very little impact. This leak was found while running t0001. LSAN output can be found below: Direct leak of 65 byte(s) in 1 object(s) allocated from: #0 0x49a859 in realloc /home/abuild/rpmbuild/BUILD/llvm-11.0.0.src/build/../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0x9aae36 in xrealloc /home/ahunt/oss-fuzz/git/wrapper.c:126:8 #2 0x939d8d in strbuf_grow /home/ahunt/oss-fuzz/git/strbuf.c:98:2 #3 0x93b936 in strbuf_vaddf /home/ahunt/oss-fuzz/git/strbuf.c:392:3 #4 0x93b7ff in strbuf_addf /home/ahunt/oss-fuzz/git/strbuf.c:333:2 #5 0x86747e in preprocess_options /home/ahunt/oss-fuzz/git/parse-options.c:666:3 #6 0x866ed2 in parse_options /home/ahunt/oss-fuzz/git/parse-options.c:847:17 #7 0x51c4a7 in cmd_clone /home/ahunt/oss-fuzz/git/builtin/clone.c:989:9 #8 0x4cd60d in run_builtin /home/ahunt/oss-fuzz/git/git.c:453:11 #9 0x4cb2da in handle_builtin /home/ahunt/oss-fuzz/git/git.c:704:3 #10 0x4ccc37 in run_argv /home/ahunt/oss-fuzz/git/git.c:771:4 #11 0x4cac29 in cmd_main /home/ahunt/oss-fuzz/git/git.c:902:19 #12 0x69c9fe in main /home/ahunt/oss-fuzz/git/common-main.c:52:11 #13 0x7fdac42d4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Signed-off-by: Andrzej Hunt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07b8ff7 commit 8286296

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

parse-options.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ static int show_gitcomp(const struct option *opts, int show_all)
625625
*
626626
* Right now this is only used to preprocess and substitute
627627
* OPTION_ALIAS.
628+
*
629+
* The returned options should be freed using free_preprocessed_options.
628630
*/
629631
static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
630632
const struct option *options)
@@ -693,6 +695,21 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
693695
return newopt;
694696
}
695697

698+
static void free_preprocessed_options(const struct option ** preprocessed_options, const struct option *original_options)
699+
{
700+
int i;
701+
702+
if (!*preprocessed_options) {
703+
return;
704+
}
705+
for (i = 0; original_options[i].type != OPTION_END; i++) {
706+
if (original_options[i].type == OPTION_ALIAS) {
707+
free((void *)(*preprocessed_options)[i].help);
708+
}
709+
}
710+
free((void *)*preprocessed_options);
711+
}
712+
696713
static int usage_with_options_internal(struct parse_opt_ctx_t *,
697714
const char * const *,
698715
const struct option *, int, int);
@@ -838,15 +855,17 @@ int parse_options(int argc, const char **argv, const char *prefix,
838855
int flags)
839856
{
840857
struct parse_opt_ctx_t ctx;
841-
struct option *real_options;
858+
const struct option *preprocessed_options, *original_options = NULL;
842859

843860
disallow_abbreviated_options =
844861
git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
845862

846863
memset(&ctx, 0, sizeof(ctx));
847-
real_options = preprocess_options(&ctx, options);
848-
if (real_options)
849-
options = real_options;
864+
preprocessed_options = preprocess_options(&ctx, options);
865+
if (preprocessed_options) {
866+
original_options = options;
867+
options = preprocessed_options;
868+
}
850869
parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
851870
switch (parse_options_step(&ctx, options, usagestr)) {
852871
case PARSE_OPT_HELP:
@@ -870,7 +889,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
870889
}
871890

872891
precompose_argv_prefix(argc, argv, NULL);
873-
free(real_options);
892+
free_preprocessed_options(&preprocessed_options, original_options);
874893
free(ctx.alias_groups);
875894
return parse_options_end(&ctx);
876895
}

0 commit comments

Comments
 (0)