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>
Patrick Steinhardt committed
Nov 5, 2024 at 07:16 UTC
43fedde3dfcd1e0ccd638f1a8c70c39219680fb1
2 files changed
+11
-3
builtin/grep.c
+10
-3
@@ -906,6 +906,7 @@ int cmd_grep(int argc,
906
int dummy;
907
int use_index = 1;
908
int allow_revs;
909
+ int ret;
910
911
struct option options[] = {
912
OPT_BOOL(0, "cached", &cached,
@@ -1172,8 +1173,10 @@ int cmd_grep(int argc,
1173
* Optimize out the case where the amount of matches is limited to zero.
1174
* We do this to keep results consistent with GNU grep(1).
1175
*/
1175
- if (opt.max_count == 0)
1176
- return 1;
1176
+ if (opt.max_count == 0) {
1177
+ ret = 1;
1178
+ goto out;
1179
+ }
1180
1181
if (show_in_pager) {
1182
if (num_threads > 1)
@@ -1267,10 +1270,14 @@ int cmd_grep(int argc,
1270
hit |= wait_all();
1271
if (hit && show_in_pager)
1272
run_pager(&opt, prefix);
1273
+
1274
+ ret = !hit;
1275
+
1276
+out:
1277
clear_pathspec(&pathspec);
1278
string_list_clear(&path_list, 0);
1279
free_grep_patterns(&opt);
1280
object_array_clear(&list);
1281
free_repos();
1275
- return !hit;
1282
+ return ret;
1283
}
t/t7810-grep.sh
+1
@@ -9,6 +9,7 @@ test_description='git grep various.
9
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11
12
+TEST_PASSES_SANITIZE_LEAK=true
13
. ./test-lib.sh
14
15
test_invalid_grep_expression() {