grep: use memmem() for fixed string search
Allow searching beyond NUL characters by using memmem() instead of strstr(). Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 22, 2010 at 23:32 UTC
1baddf4b3781c0c714442adfda496d667e1850cd
2 files changed
+13
-7
grep.c
+9
-7
@@ -329,14 +329,15 @@ static void show_name(struct grep_opt *opt, const char *name)
329
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
330
}
331
332
-
333
-static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
332
+static int fixmatch(const char *pattern, char *line, char *eol,
333
+ int ignore_case, regmatch_t *match)
334
{
335
char *hit;
336
+
337
if (ignore_case)
338
hit = strcasestr(line, pattern);
339
else
339
- hit = strstr(line, pattern);
340
+ hit = memmem(line, eol - line, pattern, strlen(pattern));
341
342
if (!hit) {
343
match->rm_so = match->rm_eo = -1;
@@ -399,7 +400,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
400
401
again:
402
if (p->fixed)
402
- hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
403
+ hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
404
else
405
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
406
@@ -725,9 +726,10 @@ static int look_ahead(struct grep_opt *opt,
726
int hit;
727
regmatch_t m;
728
728
- if (p->fixed)
729
- hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
730
- else {
729
+ if (p->fixed) {
730
+ hit = !fixmatch(p->pattern, bol, bol + *left_p,
731
+ p->ignore_case, &m);
732
+ } else {
733
#ifdef REG_STARTEND
734
m.rm_so = 0;
735
m.rm_eo = *left_p;
t/t7008-grep-binary.sh
+4
@@ -51,4 +51,8 @@ test_expect_success 'git grep -q ina a' '
51
test_cmp expect actual
52
'
53
54
+test_expect_success 'git grep -F ile a' '
55
+ git grep -F ile a
56
+'
57
+
58
test_done