Skip to content

Commit b202e51

Browse files
avargitster
authored andcommitted
grep: fix a "path_list" memory leak
Free the "path_list" used in builtin/grep.c, it was declared as STRING_LIST_INIT_NODUP, let's change it to a STRING_LIST_INIT_DUP since an early user in cmd_grep() appends a string passed via parse-options.c to it, which needs to be duplicated. Let's then convert the remaining callers to use string_list_append_nodup() instead, allowing us to free the list. This makes all the tests in t7811-grep-open.sh pass, 6/10 would fail before this change. The only remaining failure would have been due to a stray "git checkout" (which still leaks memory). In this case we can use a "git reset --hard" instead, so let's do that, and move the test_when_finished() above the code that would modify the relevant file. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96c1012 commit b202e51

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

builtin/grep.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ static void append_path(struct grep_opt *opt, const void *data, size_t len)
401401

402402
if (len == 1 && *(const char *)data == '\0')
403403
return;
404-
string_list_append(path_list, xstrndup(data, len));
404+
string_list_append_nodup(path_list, xstrndup(data, len));
405405
}
406406

407407
static void run_pager(struct grep_opt *opt, const char *prefix)
@@ -839,7 +839,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
839839
struct grep_opt opt;
840840
struct object_array list = OBJECT_ARRAY_INIT;
841841
struct pathspec pathspec;
842-
struct string_list path_list = STRING_LIST_INIT_NODUP;
842+
struct string_list path_list = STRING_LIST_INIT_DUP;
843843
int i;
844844
int dummy;
845845
int use_index = 1;
@@ -1159,8 +1159,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11591159
strbuf_addf(&buf, "+/%s%s",
11601160
strcmp("less", pager) ? "" : "*",
11611161
opt.pattern_list->pattern);
1162-
string_list_append(&path_list,
1163-
strbuf_detach(&buf, NULL));
1162+
string_list_append_nodup(&path_list,
1163+
strbuf_detach(&buf, NULL));
11641164
}
11651165
}
11661166

@@ -1195,6 +1195,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11951195
if (hit && show_in_pager)
11961196
run_pager(&opt, prefix);
11971197
clear_pathspec(&pathspec);
1198+
string_list_clear(&path_list, 0);
11981199
free_grep_patterns(&opt);
11991200
object_array_clear(&list);
12001201
free_repos();

t/t7811-grep-open.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test_description='git grep --open-files-in-pager
44
'
55

6+
TEST_PASSES_SANITIZE_LEAK=true
67
. ./test-lib.sh
78
. "$TEST_DIRECTORY"/lib-pager.sh
89
unset PAGER GIT_PAGER
@@ -114,8 +115,8 @@ test_expect_success 'modified file' '
114115
unrelated
115116
EOF
116117
118+
test_when_finished "git reset --hard" &&
117119
echo "enum grep_pat_token" >unrelated &&
118-
test_when_finished "git checkout HEAD unrelated" &&
119120
GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
120121
test_cmp expect actual &&
121122
test_must_be_empty out

0 commit comments

Comments
 (0)