]> git.ipfire.org Git - thirdparty/git.git/commitdiff
shell: fix leaking strings
authorPatrick Steinhardt <ps@pks.im>
Mon, 30 Sep 2024 09:13:22 +0000 (11:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Sep 2024 18:23:03 +0000 (11:23 -0700)
There are two memory leaks in "shell.c". The first one in `run_shell()`
is trivial and fixed without further explanation. The second one in
`cmd_main()` happens because we overwrite the `prog` variable, which
contains an allocated string. In fact though, the memory pointed to by
that variable is still in use because we use `split_cmdline()`, which
may create pointers into the middle of that string. But as we do not
have a direct pointer to the head of the allocated string anymore, we
get a complaint by the leak checker.

Address this by not overwriting the `prog` pointer.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
shell.c
t/t9400-git-cvsserver-server.sh
t/t9850-shell.sh

diff --git a/shell.c b/shell.c
index 2ece8b16e2e8e1e3473dbc7bae6deedea51ee4e8..76333c80686d8f427a36d9ca70e1995976e1c4ae 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -143,6 +143,7 @@ static void run_shell(void)
                }
 
                free(argv);
+               free(split_args);
                free(rawargs);
        } while (!done);
 }
@@ -216,9 +217,8 @@ int cmd_main(int argc, const char **argv)
        count = split_cmdline(prog, &user_argv);
        if (count >= 0) {
                if (is_valid_cmd_name(user_argv[0])) {
-                       prog = make_cmd(user_argv[0]);
-                       user_argv[0] = prog;
-                       execv(user_argv[0], (char *const *) user_argv);
+                       char *cmd = make_cmd(user_argv[0]);
+                       execv(cmd, (char *const *) user_argv);
                }
                free(prog);
                free(user_argv);
index e499c7f955125eb25ce0dfdcd78012adf8cf4d7a..6da7440e73c5fa7f9b17a67f0655f0b4d8520510 100755 (executable)
@@ -11,6 +11,7 @@ cvs CLI client via git-cvsserver server'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 if ! test_have_prereq PERL; then
index cfc71c3bd4318758570f0aa7206d63fa71fedb4e..f503f16d1b6a417bc7c7d160cc69433efa082b34 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='git shell tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'shell allows upload-pack' '