#884075 glibc: Wrong results with regex backreferences

Package:
glibc
Source:
glibc
Submitter:
Mathias Pietsch
Date:
2017-12-11 08:57:12 UTC
Severity:
normal
Tags:
#884075#5
Date:
2017-12-06 22:51:52 UTC
From:
To:
when trying to test this famous regexp for matching non-prime numbers
(^1?$|^(11+?)\1+$) which works fine with 'grep -P', i wondered if it
also would work without the non-greedy quantifier so egrep or even
plain grep could use it, and found the following problem e.g., with the
prime number 13:

$ echo "1111111111111" | grep -E '^(11+)\1+$|^1?$' || echo prime
1111111111111

the expected output would have been 'prime' because '1111111111111'
doesn't match '^1?$' and is also no concatanation of two or more
'11', two or more '111', ... opposite to the orignal perl-style
non-greedy version, here the substrings should be tested for a match
beginning with the longest (13 x '1') down to the shortest ('11').

next i removed the empty line term from the regexp (i.e., the '?' from
the '^1?$' term):

$ echo "1111111111111" | grep -E '^(11+)\1+$|^1$' || echo prime
prime

now the result is correct. but since the input in not an empty line,
using '^(11+)\1+$|^1?$' or '^(11+)\1+$|^1$' should not make any
difference.

(making the empty line term a separate term '^(11+)\1+$|^1$|^$' doesn't
change anything. the same is true with using plain grep and
'^\(11\+\)\1\+$\|^1\?$' or '^\(11\+\)\1\+$\|^1$\|^$'.)

this bug also appears in the original upstream version 3.1
(http://ftp.gnu.org/gnu/grep/grep-3.1.tar.xz)

#884075#10
Date:
2017-12-08 11:11:10 UTC
From:
To:
Dear grep developers,

I would like to forward the report below, filed by Mathias Pietsch to
Debian. I don't want to introduce other noise than this:

$ echo 1111111111111 | grep -E '^1?$' ; echo $?
1
$ echo 1111111111111 | grep -E '^(11+)\1+$' ; echo $?
1
$ echo 1111111111111 | grep -E '^(11+)\1+$|^1?$' ; echo $?
1111111111111
0

Shouldn't the last grep command exit 1 too?

Cheers,
----- Forwarded message from Mathias Pietsch <m.pietsch@uke.uni-hamburg.de> ----- Date: Wed, 6 Dec 2017 23:51:52 +0100 From: Mathias Pietsch <m.pietsch@uke.uni-hamburg.de> To: Debian Bug Tracking System <submit@bugs.debian.org> Subject: Bug#883733: grep returns 0 even if there is no match X-Mailer: reportbug 7.1.7 Package: grep Version: 2.27-2 Severity: normal Tags: upstream when trying to test this famous regexp for matching non-prime numbers (^1?$|^(11+?)\1+$) which works fine with 'grep -P', i wondered if it also would work without the non-greedy quantifier so egrep or even plain grep could use it, and found the following problem e.g., with the prime number 13: $ echo "1111111111111" | grep -E '^(11+)\1+$|^1?$' || echo prime 1111111111111 the expected output would have been 'prime' because '1111111111111' doesn't match '^1?$' and is also no concatanation of two or more '11', two or more '111', ... opposite to the orignal perl-style non-greedy version, here the substrings should be tested for a match beginning with the longest (13 x '1') down to the shortest ('11'). next i removed the empty line term from the regexp (i.e., the '?' from the '^1?$' term): $ echo "1111111111111" | grep -E '^(11+)\1+$|^1$' || echo prime prime now the result is correct. but since the input in not an empty line, using '^(11+)\1+$|^1?$' or '^(11+)\1+$|^1$' should not make any difference. (making the empty line term a separate term '^(11+)\1+$|^1$|^$' doesn't change anything. the same is true with using plain grep and '^\(11\+\)\1\+$\|^1\?$' or '^\(11\+\)\1\+$\|^1$\|^$'.) this bug also appears in the original upstream version 3.1 (http://ftp.gnu.org/gnu/grep/grep-3.1.tar.xz)
----- End forwarded message -----
#884075#15
Date:
2017-12-08 11:21:43 UTC
From:
To:

#884075#22
Date:
2017-12-08 18:38:45 UTC
From:
To:
Yikes! Thanks for forwarding that.
That is indeed a bug. I think it must be due to a bug in glibc's
regexp code, since that's the matcher that grep uses when there is any
back-reference.

#884075#27
Date:
2017-12-08 18:34:53 UTC
From:
To:
Yes it should. This appears to be due to a longstanding bug in the glibc
regular expression matcher. See:

https://sourceware.org/bugzilla/show_bug.cgi?id=11053

#884075#32
Date:
2017-12-11 08:53:17 UTC
From:
To:
El 08/12/17 a las 10:34, Paul Eggert escribió:

Hi,

Thanks for the info. I am reassigning this bug to glibc (and keeping a
copy of it for grep, in case possible future users will notice the
issue).

Cheers,