Skip to content

Commit c4ed563

Browse files
pks-tgitster
authored andcommitted
help: fix leaking return value from help_unknown_cmd()
While `help_unknown_cmd()` would usually die on an unknown command, it instead returns an autocorrected command when "help.autocorrect" is set. But while the function is declared to return a string constant, it actually returns an allocated string in that case. Callers thus aren't aware that they have to free the string, leading to a memory leak. Fix the function return type to be non-constant and free the returned value at its only callsite. Note that we cannot simply take ownership of `main_cmds.names[0]->name` and then eventually free it. This is because the `struct cmdname` is using a flex array to allocate the name, so the name pointer points into the middle of the structure and thus cannot be freed. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ba08570 commit c4ed563

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

git.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,9 @@ int cmd_main(int argc, const char **argv)
961961
exit(1);
962962
}
963963
if (!done_help) {
964-
strvec_replace(&args, 0, help_unknown_cmd(cmd));
964+
char *assumed = help_unknown_cmd(cmd);
965+
strvec_replace(&args, 0, assumed);
966+
free(assumed);
965967
cmd = args.v[0];
966968
done_help = 1;
967969
} else {

help.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,14 @@ static const char bad_interpreter_advice[] =
612612
N_("'%s' appears to be a git command, but we were not\n"
613613
"able to execute it. Maybe git-%s is broken?");
614614

615-
const char *help_unknown_cmd(const char *cmd)
615+
char *help_unknown_cmd(const char *cmd)
616616
{
617617
struct help_unknown_cmd_config cfg = { 0 };
618618
int i, n, best_similarity = 0;
619619
struct cmdnames main_cmds = { 0 };
620620
struct cmdnames other_cmds = { 0 };
621621
struct cmdname_help *common_cmds;
622-
const char *assumed = NULL;
622+
char *assumed = NULL;
623623

624624
read_early_config(the_repository, git_unknown_cmd_config, &cfg);
625625

@@ -696,9 +696,8 @@ const char *help_unknown_cmd(const char *cmd)
696696
; /* still counting */
697697
}
698698
if (cfg.autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
699-
assumed = main_cmds.names[0]->name;
700-
main_cmds.names[0] = NULL;
701-
cmdnames_release(&main_cmds);
699+
assumed = xstrdup(main_cmds.names[0]->name);
700+
702701
fprintf_ln(stderr,
703702
_("WARNING: You called a Git command named '%s', "
704703
"which does not exist."),
@@ -716,7 +715,7 @@ const char *help_unknown_cmd(const char *cmd)
716715
strbuf_release(&msg);
717716
if (!(starts_with(answer, "y") ||
718717
starts_with(answer, "Y"))) {
719-
assumed = NULL;
718+
FREE_AND_NULL(assumed);
720719
goto out;
721720
}
722721
} else {

help.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void list_all_other_cmds(struct string_list *list);
3232
void list_cmds_by_category(struct string_list *list,
3333
const char *category);
3434
void list_cmds_by_config(struct string_list *list);
35-
const char *help_unknown_cmd(const char *cmd);
35+
char *help_unknown_cmd(const char *cmd);
3636
void load_command_list(const char *prefix,
3737
struct cmdnames *main_cmds,
3838
struct cmdnames *other_cmds);

0 commit comments

Comments
 (0)