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>
This commit is contained in:
parent
321ffcc055
commit
1baddf4b37
16
grep.c
16
grep.c
@ -329,14 +329,15 @@ static void show_name(struct grep_opt *opt, const char *name)
|
|||||||
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
|
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int fixmatch(const char *pattern, char *line, char *eol,
|
||||||
static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
|
int ignore_case, regmatch_t *match)
|
||||||
{
|
{
|
||||||
char *hit;
|
char *hit;
|
||||||
|
|
||||||
if (ignore_case)
|
if (ignore_case)
|
||||||
hit = strcasestr(line, pattern);
|
hit = strcasestr(line, pattern);
|
||||||
else
|
else
|
||||||
hit = strstr(line, pattern);
|
hit = memmem(line, eol - line, pattern, strlen(pattern));
|
||||||
|
|
||||||
if (!hit) {
|
if (!hit) {
|
||||||
match->rm_so = match->rm_eo = -1;
|
match->rm_so = match->rm_eo = -1;
|
||||||
@ -399,7 +400,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
|
|||||||
|
|
||||||
again:
|
again:
|
||||||
if (p->fixed)
|
if (p->fixed)
|
||||||
hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
|
hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
|
||||||
else
|
else
|
||||||
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
|
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
|
||||||
|
|
||||||
@ -725,9 +726,10 @@ static int look_ahead(struct grep_opt *opt,
|
|||||||
int hit;
|
int hit;
|
||||||
regmatch_t m;
|
regmatch_t m;
|
||||||
|
|
||||||
if (p->fixed)
|
if (p->fixed) {
|
||||||
hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
|
hit = !fixmatch(p->pattern, bol, bol + *left_p,
|
||||||
else {
|
p->ignore_case, &m);
|
||||||
|
} else {
|
||||||
#ifdef REG_STARTEND
|
#ifdef REG_STARTEND
|
||||||
m.rm_so = 0;
|
m.rm_so = 0;
|
||||||
m.rm_eo = *left_p;
|
m.rm_eo = *left_p;
|
||||||
|
@ -51,4 +51,8 @@ test_expect_success 'git grep -q ina a' '
|
|||||||
test_cmp expect actual
|
test_cmp expect actual
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git grep -F ile a' '
|
||||||
|
git grep -F ile a
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
Loading…
Reference in New Issue
Block a user