Skip to content

Commit 1baddf4

Browse files
René Scharfegitster
authored andcommitted
grep: use memmem() for fixed string search
Allow searching beyond NUL characters by using memmem() instead of strstr(). Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 321ffcc commit 1baddf4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

grep.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,15 @@ static void show_name(struct grep_opt *opt, const char *name)
329329
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
330330
}
331331

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)
334334
{
335335
char *hit;
336+
336337
if (ignore_case)
337338
hit = strcasestr(line, pattern);
338339
else
339-
hit = strstr(line, pattern);
340+
hit = memmem(line, eol - line, pattern, strlen(pattern));
340341

341342
if (!hit) {
342343
match->rm_so = match->rm_eo = -1;
@@ -399,7 +400,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
399400

400401
again:
401402
if (p->fixed)
402-
hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
403+
hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
403404
else
404405
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
405406

@@ -725,9 +726,10 @@ static int look_ahead(struct grep_opt *opt,
725726
int hit;
726727
regmatch_t m;
727728

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 {
731733
#ifdef REG_STARTEND
732734
m.rm_so = 0;
733735
m.rm_eo = *left_p;

t/t7008-grep-binary.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ test_expect_success 'git grep -q ina a' '
5151
test_cmp expect actual
5252
'
5353

54+
test_expect_success 'git grep -F ile a' '
55+
git grep -F ile a
56+
'
57+
5458
test_done

0 commit comments

Comments
 (0)