aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-11-05 07:16:52 +0100
committerJunio C Hamano <gitster@pobox.com>2024-11-04 22:37:52 -0800
commit43fedde3dfcd1e0ccd638f1a8c70c39219680fb1 (patch)
tree8a1c647069ad2de3d6a1cc7f5e9aec21839230fa
parenta6590ccdd431e2ab7b9c521cac674546725a54d2 (diff)
downloadgit-43fedde3dfcd1e0ccd638f1a8c70c39219680fb1.tar.gz
builtin/grep: fix leak with `--max-count=0`
When executing with `--max-count=0` we'll return early from git-grep(1) without performing any cleanup, which causes memory leaks. Plug these. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--builtin/grep.c13
-rwxr-xr-xt/t7810-grep.sh1
2 files changed, 11 insertions, 3 deletions
diff --git a/builtin/grep.c b/builtin/grep.c
index f17d46a06e..98b85c7fca 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -906,6 +906,7 @@ int cmd_grep(int argc,
int dummy;
int use_index = 1;
int allow_revs;
+ int ret;
struct option options[] = {
OPT_BOOL(0, "cached", &cached,
@@ -1172,8 +1173,10 @@ int cmd_grep(int argc,
* Optimize out the case where the amount of matches is limited to zero.
* We do this to keep results consistent with GNU grep(1).
*/
- if (opt.max_count == 0)
- return 1;
+ if (opt.max_count == 0) {
+ ret = 1;
+ goto out;
+ }
if (show_in_pager) {
if (num_threads > 1)
@@ -1267,10 +1270,14 @@ int cmd_grep(int argc,
hit |= wait_all();
if (hit && show_in_pager)
run_pager(&opt, prefix);
+
+ ret = !hit;
+
+out:
clear_pathspec(&pathspec);
string_list_clear(&path_list, 0);
free_grep_patterns(&opt);
object_array_clear(&list);
free_repos();
- return !hit;
+ return ret;
}
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index af2cf2f78a..9e7681f083 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -9,6 +9,7 @@ test_description='git grep various.
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_invalid_grep_expression() {