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 <j.sixt@viscovery.net> for the help with the test cases. Signed-off-by: Matt Kraai <kraai@ftbfs.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matt Kraai committed
Oct 16, 2009 at 07:13 UTC
cfe370c6476392095bc3f18013d195b1cccd6184
2 files changed
+67
-1
builtin-grep.c
+1
-1
@@ -599,7 +599,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
599
struct grep_opt *grep_opt = opt->value;
600
FILE *patterns;
601
int lno = 0;
602
- struct strbuf sb;
602
+ struct strbuf sb = STRBUF_INIT;
603
604
patterns = fopen(arg, "r");
605
if (!patterns)
t/t7002-grep.sh
+66
@@ -164,6 +164,72 @@ test_expect_success 'grep -e A --and --not -e B' '
164
test_cmp expected actual
165
'
166
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
+
233
cat >expected <<EOF
234
y:y yy
235
--