grep: bug: backref in last of multiple patterns

* NEWS: Mention this.
* src/dfasearch.c (GEAcompile): Trim trailing newline from
the last pattern, even if it has back-references and follows
a pattern that lacks back-references.
* tests/backref: Add test for this bug.
This commit is contained in:
Paul Eggert 2022-12-05 14:16:45 -08:00
parent 429b3497d1
commit b061d24916
3 changed files with 26 additions and 13 deletions

6
NEWS
View File

@ -2,6 +2,12 @@ GNU grep NEWS -*- outline -*-
* Noteworthy changes in release ?.? (????-??-??) [?]
** Bug fixes
When given multiple patterns the last of which has a back-reference,
grep no longer sometimes mistakenly matches lines in some cases.
[Bug#36148#13 introduced in grep 3.4]
* Noteworthy changes in release 3.8 (2022-09-02) [stable]

View File

@ -281,20 +281,19 @@ GEAcompile (char *pattern, idx_t size, reg_syntax_t syntax_bits,
if (compilation_failed)
exit (EXIT_TROUBLE);
if (prev <= patlim)
if (patlim < prev)
buflen--;
else if (pattern < prev)
{
if (pattern < prev)
{
idx_t prevlen = patlim - prev;
buf = xirealloc (buf, buflen + prevlen);
memcpy (buf + buflen, prev, prevlen);
buflen += prevlen;
}
else
{
buf = pattern;
buflen = size;
}
idx_t prevlen = patlim - prev;
buf = xirealloc (buf, buflen + prevlen);
memcpy (buf + buflen, prev, prevlen);
buflen += prevlen;
}
else
{
buf = pattern;
buflen = size;
}
/* In the match_words and match_lines cases, we use a different pattern

View File

@ -43,4 +43,12 @@ if test $? -ne 2 ; then
failures=1
fi
# https://bugs.gnu.org/36148#13
echo 'Total failed: 2 (1 ignored)' |
grep -e '^Total failed: 0$' -e '^Total failed: \([0-9]*\) (\1 ignored)$'
if test $? -ne 1 ; then
echo "Backref: Multiple -e test, test #5 failed"
failures=1
fi
Exit $failures