Skip to content

Commit 2379b5c

Browse files
pks-tgitster
authored andcommitted
builtin/help: fix leaks in check_git_cmd()
The `check_git_cmd()` function is declared to return a string constant. And while it sometimes does return a constant, it may also return an allocated string in two cases: - When handling aliases. This case is already marked with `UNLEAK()` to work around the leak. - When handling unknown commands in case "help.autocorrect" is enabled. This one is not marked with `UNLEAK()`. The function only has a single caller, so let's fix its return type to be non-constant, consistently return an allocated string and free it at its callsite to plug the leak. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7720dbe commit 2379b5c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

builtin/help.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,12 @@ static void show_html_page(const char *page)
551551
open_html(page_path.buf);
552552
}
553553

554-
static const char *check_git_cmd(const char* cmd)
554+
static char *check_git_cmd(const char *cmd)
555555
{
556556
char *alias;
557557

558558
if (is_git_command(cmd))
559-
return cmd;
559+
return xstrdup(cmd);
560560

561561
alias = alias_lookup(cmd);
562562
if (alias) {
@@ -589,14 +589,13 @@ static const char *check_git_cmd(const char* cmd)
589589
die(_("bad alias.%s string: %s"), cmd,
590590
split_cmdline_strerror(count));
591591
free(argv);
592-
UNLEAK(alias);
593592
return alias;
594593
}
595594

596595
if (exclude_guides)
597596
return help_unknown_cmd(cmd);
598597

599-
return cmd;
598+
return xstrdup(cmd);
600599
}
601600

602601
static void no_help_format(const char *opt_mode, enum help_format fmt)
@@ -642,6 +641,7 @@ int cmd_help(int argc,
642641
{
643642
int nongit;
644643
enum help_format parsed_help_format;
644+
char *command = NULL;
645645
const char *page;
646646

647647
argc = parse_options(argc, argv, prefix, builtin_help_options,
@@ -713,9 +713,9 @@ int cmd_help(int argc,
713713
if (help_format == HELP_FORMAT_NONE)
714714
help_format = parse_help_format(DEFAULT_HELP_FORMAT);
715715

716-
argv[0] = check_git_cmd(argv[0]);
716+
command = check_git_cmd(argv[0]);
717717

718-
page = cmd_to_page(argv[0]);
718+
page = cmd_to_page(command);
719719
switch (help_format) {
720720
case HELP_FORMAT_NONE:
721721
case HELP_FORMAT_MAN:
@@ -729,5 +729,6 @@ int cmd_help(int argc,
729729
break;
730730
}
731731

732+
free(command);
732733
return 0;
733734
}

0 commit comments

Comments
 (0)