add: plug a leak on interactive_add

Plug a leak we have since 5a76aff1a6 (add: convert to use parse_pathspec, 2013-07-14). This leak can be triggered with: $ git add -p anything Fixing this leak allows us to mark as leak-free the following tests: + t3701-add-interactive.sh + t7514-commit-patch.sh Mark them with "TEST_PASSES_SANITIZE_LEAK=true" to notice and fix promply any new leak that may be introduced and triggered by them in the future. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Apr 23, 2024 at 00:54 UTC 16727404c48ba1c2f43fb966276aee0b8cb24389
3 files changed +9 -3
builtin/add.c
+6 -3
@@ -150,7 +150,7 @@ static int refresh(int verbose, const struct pathspec *pathspec)
150 int interactive_add(const char **argv, const char *prefix, int patch)
151 {
152 struct pathspec pathspec;
153 - int unused;
153 + int unused, ret;
154
155 if (!git_config_get_bool("add.interactive.usebuiltin", &unused))
156 warning(_("the add.interactive.useBuiltin setting has been removed!\n"
@@ -163,9 +163,12 @@ int interactive_add(const char **argv, const char *prefix, int patch)
163 prefix, argv);
164
165 if (patch)
166 - return !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
166 + ret = !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
167 else
168 - return !!run_add_i(the_repository, &pathspec);
168 + ret = !!run_add_i(the_repository, &pathspec);
169 +
170 + clear_pathspec(&pathspec);
171 + return ret;
172 }
173
174 static int edit_patch(int argc, const char **argv, const char *prefix)
t/t3701-add-interactive.sh
+1
@@ -4,6 +4,7 @@ test_description='add -i basic tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 +TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-terminal.sh
10
t/t7514-commit-patch.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='hunk edit with "commit -p -m"'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup (initial)' '