Skip to content

Commit 7ffbcf5

Browse files
committed
Merge branch 'rs/grep-lookahead' into jch
* rs/grep-lookahead: grep: disable lookahead on error
2 parents e5e7c7c + 1ba6a16 commit 7ffbcf5

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

grep.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -907,15 +907,17 @@ static int patmatch(struct grep_pat *p,
907907
const char *line, const char *eol,
908908
regmatch_t *match, int eflags)
909909
{
910-
int hit;
911-
912910
if (p->pcre2_pattern)
913-
hit = !pcre2match(p, line, eol, match, eflags);
914-
else
915-
hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
916-
eflags);
911+
return !pcre2match(p, line, eol, match, eflags);
917912

918-
return hit;
913+
switch (regexec_buf(&p->regexp, line, eol - line, 1, match, eflags)) {
914+
case 0:
915+
return 1;
916+
case REG_NOMATCH:
917+
return 0;
918+
default:
919+
return -1;
920+
}
919921
}
920922

921923
static void strip_timestamp(const char *bol, const char **eol_p)
@@ -953,6 +955,8 @@ static int headerless_match_one_pattern(struct grep_pat *p,
953955

954956
again:
955957
hit = patmatch(p, bol, eol, pmatch, eflags);
958+
if (hit < 0)
959+
hit = 0;
956960

957961
if (hit && p->word_regexp) {
958962
if ((pmatch[0].rm_so < 0) ||
@@ -1462,6 +1466,8 @@ static int look_ahead(struct grep_opt *opt,
14621466
regmatch_t m;
14631467

14641468
hit = patmatch(p, bol, bol + *left_p, &m, 0);
1469+
if (hit < 0)
1470+
return -1;
14651471
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
14661472
continue;
14671473
if (earliest < 0 || m.rm_so < earliest)
@@ -1656,9 +1662,13 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16561662
if (try_lookahead
16571663
&& !(last_hit
16581664
&& (show_function ||
1659-
lno <= last_hit + opt->post_context))
1660-
&& look_ahead(opt, &left, &lno, &bol))
1661-
break;
1665+
lno <= last_hit + opt->post_context))) {
1666+
hit = look_ahead(opt, &left, &lno, &bol);
1667+
if (hit < 0)
1668+
try_lookahead = 0;
1669+
else if (hit)
1670+
break;
1671+
}
16621672
eol = end_of_line(bol, &left);
16631673

16641674
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))

t/t7810-grep.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ test_expect_success setup '
8888
# Still a no-op.
8989
function dummy() {}
9090
EOF
91+
printf "\200\nASCII\n" >invalid-utf8 &&
9192
if test_have_prereq FUNNYNAMES
9293
then
9394
echo unusual >"\"unusual\" pathname" &&
@@ -535,6 +536,14 @@ do
535536
test_cmp expected actual
536537
'
537538

539+
test_expect_success "grep $L searches past invalid lines on UTF-8 locale" '
540+
LC_ALL=en_US.UTF-8 git grep A. invalid-utf8 >actual &&
541+
cat >expected <<-EOF &&
542+
invalid-utf8:ASCII
543+
EOF
544+
test_cmp expected actual
545+
'
546+
538547
test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
539548
cat >expected <<-EOF &&
540549
${HC}"\"unusual\" pathname":unusual

0 commit comments

Comments
 (0)