shell: fix leaking strings

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>

Patrick Steinhardt committed Sep 30, 2024 at 11:13 UTC c75841687b39f4c62fba56bde08653591b9c2149
3 files changed +6 -3
shell.c
+3 -3
@@ -143,6 +143,7 @@ static void run_shell(void)
143 }
144
145 free(argv);
146 + free(split_args);
147 free(rawargs);
148 } while (!done);
149 }
@@ -216,9 +217,8 @@ int cmd_main(int argc, const char **argv)
217 count = split_cmdline(prog, &user_argv);
218 if (count >= 0) {
219 if (is_valid_cmd_name(user_argv[0])) {
219 - prog = make_cmd(user_argv[0]);
220 - user_argv[0] = prog;
221 - execv(user_argv[0], (char *const *) user_argv);
220 + char *cmd = make_cmd(user_argv[0]);
221 + execv(cmd, (char *const *) user_argv);
222 }
223 free(prog);
224 free(user_argv);
t/t9400-git-cvsserver-server.sh
+1
@@ -11,6 +11,7 @@ cvs CLI client via git-cvsserver server'
11 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
12 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13
14 +TEST_PASSES_SANITIZE_LEAK=true
15 . ./test-lib.sh
16
17 if ! test_have_prereq PERL; then
t/t9850-shell.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='git shell tests'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'shell allows upload-pack' '