Skip to content

Commit cfe370c

Browse files
kraaigitster
authored andcommitted
grep: do not segfault when -f is used
"git grep" would segfault if its -f option was used because it would try to use an uninitialized strbuf, so initialize the strbuf. Thanks to Johannes Sixt <[email protected]> for the help with the test cases. Signed-off-by: Matt Kraai <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a0c342 commit cfe370c

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

builtin-grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
599599
struct grep_opt *grep_opt = opt->value;
600600
FILE *patterns;
601601
int lno = 0;
602-
struct strbuf sb;
602+
struct strbuf sb = STRBUF_INIT;
603603

604604
patterns = fopen(arg, "r");
605605
if (!patterns)

t/t7002-grep.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,72 @@ test_expect_success 'grep -e A --and --not -e B' '
164164
test_cmp expected actual
165165
'
166166

167+
test_expect_success 'grep -f, non-existent file' '
168+
test_must_fail git grep -f patterns
169+
'
170+
171+
cat >expected <<EOF
172+
file:foo mmap bar
173+
file:foo_mmap bar
174+
file:foo_mmap bar mmap
175+
file:foo mmap bar_mmap
176+
file:foo_mmap bar mmap baz
177+
EOF
178+
179+
cat >pattern <<EOF
180+
mmap
181+
EOF
182+
183+
test_expect_success 'grep -f, one pattern' '
184+
git grep -f pattern >actual &&
185+
test_cmp expected actual
186+
'
187+
188+
cat >expected <<EOF
189+
file:foo mmap bar
190+
file:foo_mmap bar
191+
file:foo_mmap bar mmap
192+
file:foo mmap bar_mmap
193+
file:foo_mmap bar mmap baz
194+
t/a/v:vvv
195+
t/v:vvv
196+
v:vvv
197+
EOF
198+
199+
cat >patterns <<EOF
200+
mmap
201+
vvv
202+
EOF
203+
204+
test_expect_success 'grep -f, multiple patterns' '
205+
git grep -f patterns >actual &&
206+
test_cmp expected actual
207+
'
208+
209+
cat >expected <<EOF
210+
file:foo mmap bar
211+
file:foo_mmap bar
212+
file:foo_mmap bar mmap
213+
file:foo mmap bar_mmap
214+
file:foo_mmap bar mmap baz
215+
t/a/v:vvv
216+
t/v:vvv
217+
v:vvv
218+
EOF
219+
220+
cat >patterns <<EOF
221+
222+
mmap
223+
224+
vvv
225+
226+
EOF
227+
228+
test_expect_success 'grep -f, ignore empty lines' '
229+
git grep -f patterns >actual &&
230+
test_cmp expected actual
231+
'
232+
167233
cat >expected <<EOF
168234
y:y yy
169235
--

0 commit comments

Comments
 (0)