Skip to content

Commit 43fedde

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a6590cc commit 43fedde

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

builtin/grep.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ int cmd_grep(int argc,
906906
int dummy;
907907
int use_index = 1;
908908
int allow_revs;
909+
int ret;
909910

910911
struct option options[] = {
911912
OPT_BOOL(0, "cached", &cached,
@@ -1172,8 +1173,10 @@ int cmd_grep(int argc,
11721173
* Optimize out the case where the amount of matches is limited to zero.
11731174
* We do this to keep results consistent with GNU grep(1).
11741175
*/
1175-
if (opt.max_count == 0)
1176-
return 1;
1176+
if (opt.max_count == 0) {
1177+
ret = 1;
1178+
goto out;
1179+
}
11771180

11781181
if (show_in_pager) {
11791182
if (num_threads > 1)
@@ -1267,10 +1270,14 @@ int cmd_grep(int argc,
12671270
hit |= wait_all();
12681271
if (hit && show_in_pager)
12691272
run_pager(&opt, prefix);
1273+
1274+
ret = !hit;
1275+
1276+
out:
12701277
clear_pathspec(&pathspec);
12711278
string_list_clear(&path_list, 0);
12721279
free_grep_patterns(&opt);
12731280
object_array_clear(&list);
12741281
free_repos();
1275-
return !hit;
1282+
return ret;
12761283
}

t/t7810-grep.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test_description='git grep various.
99
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
1010
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1111

12+
TEST_PASSES_SANITIZE_LEAK=true
1213
. ./test-lib.sh
1314

1415
test_invalid_grep_expression() {

0 commit comments

Comments
 (0)