aboutsummaryrefslogtreecommitdiffstats
path: root/help.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-11-20 14:39:45 +0100
committerJunio C Hamano <gitster@pobox.com>2024-11-21 08:23:44 +0900
commit7720dbe99b303b3d658898587e02d7cf224a93c3 (patch)
tree7ea30898b27757fb7469050f7052de489ec7c2b9 /help.c
parenthelp: fix leaking `struct cmdnames` (diff)
downloadgit-7720dbe99b303b3d658898587e02d7cf224a93c3.tar.gz
git-7720dbe99b303b3d658898587e02d7cf224a93c3.zip
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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'help.c')
-rw-r--r--help.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/help.c b/help.c
index 8b56cd6e25..8a830ba35c 100644
--- a/help.c
+++ b/help.c
@@ -612,7 +612,7 @@ static const char bad_interpreter_advice[] =
N_("'%s' appears to be a git command, but we were not\n"
"able to execute it. Maybe git-%s is broken?");
-const char *help_unknown_cmd(const char *cmd)
+char *help_unknown_cmd(const char *cmd)
{
struct help_unknown_cmd_config cfg = { 0 };
int i, n, best_similarity = 0;
@@ -695,9 +695,8 @@ const char *help_unknown_cmd(const char *cmd)
; /* still counting */
}
if (cfg.autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
- const char *assumed = main_cmds.names[0]->name;
- main_cmds.names[0] = NULL;
- cmdnames_release(&main_cmds);
+ char *assumed = xstrdup(main_cmds.names[0]->name);
+
fprintf_ln(stderr,
_("WARNING: You called a Git command named '%s', "
"which does not exist."),