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: 7c280589cf (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 - therefore we add a new flag to indicate former aliases. (An alternative approach would be to look back at the original options to determine which options are aliases - but that seems like a fragile approach. Or we could even look at the alias_groups list - which might be less fragile, but would be slower as it requires nested looping.) 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 <ajrhunt@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andrzej Hunt committed Mar 21, 2021 at 16:58 UTC 64cc539fd2f8e02f39cfae21e9523da2532c0467
2 files changed +19 -1
parse-options.c
+18 -1
@@ -625,6 +625,8 @@ static int show_gitcomp(const struct option *opts, int show_all)
625 *
626 * Right now this is only used to preprocess and substitute
627 * OPTION_ALIAS.
628 + *
629 + * The returned options should be freed using free_preprocessed_options.
630 */
631 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
632 const struct option *options)
@@ -678,6 +680,7 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
680 newopt[i].short_name = short_name;
681 newopt[i].long_name = long_name;
682 newopt[i].help = strbuf_detach(&help, NULL);
683 + newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
684 break;
685 }
686
@@ -693,6 +696,20 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
696 return newopt;
697 }
698
699 +static void free_preprocessed_options(struct option *options)
700 +{
701 + int i;
702 +
703 + if (!options)
704 + return;
705 +
706 + for (i = 0; options[i].type != OPTION_END; i++) {
707 + if (options[i].flags & PARSE_OPT_FROM_ALIAS)
708 + free((void *)options[i].help);
709 + }
710 + free(options);
711 +}
712 +
713 static int usage_with_options_internal(struct parse_opt_ctx_t *,
714 const char * const *,
715 const struct option *, int, int);
@@ -870,7 +887,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
887 }
888
889 precompose_argv_prefix(argc, argv, NULL);
873 - free(real_options);
890 + free_preprocessed_options(real_options);
891 free(ctx.alias_groups);
892 return parse_options_end(&ctx);
893 }
parse-options.h
+1
@@ -44,6 +44,7 @@ enum parse_opt_option_flags {
44 PARSE_OPT_LASTARG_DEFAULT = 1 << 4,
45 PARSE_OPT_NODASH = 1 << 5,
46 PARSE_OPT_LITERAL_ARGHELP = 1 << 6,
47 + PARSE_OPT_FROM_ALIAS = 1 << 7,
48 PARSE_OPT_SHELL_EVAL = 1 << 8,
49 PARSE_OPT_NOCOMPLETE = 1 << 9,
50 PARSE_OPT_COMP_ARG = 1 << 10,