Subject: Re: bug#21670: surprising bug in grep -e with anchors
From: Paul Eggert <[email-masked]>
To: greg boyd <[email-masked]>
Cc: Norihiro Tanaka <[email-masked]>, [email-masked]
Date: Sun, 11 Oct 2015 21:34:05 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

greg boyd wrote:
> test case (single line)
> abchelloabc
>
> grep does not find the line with grep -e '^hello'  nor with grep -e 'hello$'
> however, the line is output with
> grep -e '^hello' -e 'hello$'

Oooo, that's a good one.  Give your student extra credit!  As it happens, the 
bug was recently fixed by this patch by Norihiro Tanaka:

http://git.savannah.gnu.org/cgit/grep.git/commit/?id=256a4b494fe1c48083ba73b4f62607234e4fefd5

and the fix should appear in the next grep release.  However, since the patch 
was supposed to affect only performance, it appears that the bug fix was due to 
luck, and I'm taking the liberty of adding your student's test case by 
installing the attached further patch, to help prevent this bug from coming back 
in a future version.
From 603daf1171caffd26705cf5c83d5deebf02bfd71 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 11 Oct 2015 21:27:50 -0700
Subject: [PATCH] tests: add test case for Bug#21670

* tests/options: Add test #4 to catch Bug#21670.
Also, do not overescape # in shell strings.
---
 tests/options | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/tests/options b/tests/options
index f2c7126..249e20c 100755
--- a/tests/options
+++ b/tests/options
@@ -20,21 +20,29 @@ fail=0
 # checking for -E extended regex
 echo "abababccccccd" | grep -E -e 'c{3}' > /dev/null 2>&1
 if test $? -ne 0 ; then
-        echo "Options: Wrong status code, test \#1 failed"
+        echo "Options: Wrong status code, test #1 failed"
         fail=1
 fi
 
 # checking for basic regex
 echo "abababccccccd" | grep -G -e 'c\{3\}' > /dev/null 2>&1
 if test $? -ne 0 ; then
-        echo "Options: Wrong status code, test \#2 failed"
+        echo "Options: Wrong status code, test #2 failed"
         fail=1
 fi
 
 # checking for fixed string
 echo "abababccccccd" | grep -F -e 'c\{3\}' > /dev/null 2>&1
 if test $? -ne 1 ; then
-        echo "Options: Wrong status code, test \#3 failed"
+        echo "Options: Wrong status code, test #3 failed"
+        fail=1
+fi
+
+# checking for multiple -e options; see:
+# http://bugs.gnu.org/21670
+echo abchelloabc | grep -e '^hello' -e 'hello$' > /dev/null 2>&1
+if test $? -ne 1 ; then
+        echo "Options: Wrong status code, test #4 failed"
         fail=1
 fi
 
-- 
2.1.0