Subject: bug#29613: Debian Bug#883733: grep returns 0 even if there is no match

From: "Santiago R.R." <[email-masked]>
Date: Fri, 8 Dec 2017 12:11:10 +0100

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,

 -- Santiago

----- Forwarded message from Mathias Pietsch <[email-masked]> -----

Date: Wed, 6 Dec 2017 23:51:52 +0100
From: Mathias Pietsch <[email-masked]>
To: Debian Bug Tracking System <[email-masked]>
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)

-- System Information:
Debian Release: 9.3
  APT prefers proposed-updates
  APT policy: (500, 'proposed-updates'), (500, 'stable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 4.9.0-4-amd64 (SMP w/4 CPU cores)
Locale: LANG=C, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8), LANGUAGE=C (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash
Init: sysvinit (via /sbin/init)

Versions of packages grep depends on:
ii  dpkg          1.18.24
ii  install-info  6.3.0.dfsg.1-1+b2
ii  libc6         2.24-11+deb9u2
ii  libpcre3      2:8.39-3

grep recommends no packages.

Versions of packages grep suggests:
ii  libpcre3  2:8.39-3

-- no debconf information

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts;
Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe
Koch-Gromus, Joachim Prölß, Martina Saurin (komm.)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

SAVE PAPER - THINK BEFORE PRINTING

----- End forwarded message -----