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>
This commit is contained in:
Patrick Steinhardt 2024-09-30 11:13:22 +02:00 committed by Junio C Hamano
parent d607bd8816
commit c75841687b
3 changed files with 6 additions and 3 deletions

View File

@ -143,6 +143,7 @@ static void run_shell(void)
} }
free(argv); free(argv);
free(split_args);
free(rawargs); free(rawargs);
} while (!done); } while (!done);
} }
@ -216,9 +217,8 @@ int cmd_main(int argc, const char **argv)
count = split_cmdline(prog, &user_argv); count = split_cmdline(prog, &user_argv);
if (count >= 0) { if (count >= 0) {
if (is_valid_cmd_name(user_argv[0])) { if (is_valid_cmd_name(user_argv[0])) {
prog = make_cmd(user_argv[0]); char *cmd = make_cmd(user_argv[0]);
user_argv[0] = prog; execv(cmd, (char *const *) user_argv);
execv(user_argv[0], (char *const *) user_argv);
} }
free(prog); free(prog);
free(user_argv); free(user_argv);

View File

@ -11,6 +11,7 @@ cvs CLI client via git-cvsserver server'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
if ! test_have_prereq PERL; then if ! test_have_prereq PERL; then

View File

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
test_description='git shell tests' test_description='git shell tests'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'shell allows upload-pack' ' test_expect_success 'shell allows upload-pack' '