Subject: bug#17617: bug / regression in grep-2.19

From: Norihiro Tanaka <[email-masked]>
Date: Thu, 29 May 2014 08:27:32 +0900

Thanks for the report.  I submit a patch to fix and test it.

When `is' of `must' is cleared, should be also cleared `begline' and
`endline' at OR in dfamust.
From 753ed82cb9514dd836895405a1e683b497e9a146 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Thu, 29 May 2014 08:03:03 +0900
Subject: [PATCH] dfa: fix bug mishandled for a pattern of multiple begin or
 end line constraints

This bug is introduced in grep-2.19.  See: <http://bugs.gnu.org/17617>.
* src/dfa.c (dfamust): When reset `is' in OR, also reset `begline' and
`endline' of `must'.
---
 src/dfa.c                        | 15 +++++++++++----
 tests/Makefile.am                |  1 +
 tests/multiple-begin-or-end-line | 28 ++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)
 create mode 100755 tests/multiple-begin-or-end-line

diff --git a/src/dfa.c b/src/dfa.c
index 636f2d4..0f27f85 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -3962,10 +3962,17 @@ dfamust (struct dfa *d)
             size_t j, ln, rn, n;
 
             /* Guaranteed to be.  Unlikely, but ...  */
-            if (!STREQ (lmp->is, rmp->is))
-              lmp->is[0] = '\0';
-            lmp->begline &= rmp->begline;
-            lmp->endline &= rmp->endline;
+            if (STREQ (lmp->is, rmp->is))
+              {
+                lmp->begline &= rmp->begline;
+                lmp->endline &= rmp->endline;
+              }
+            else
+              {
+                lmp->is[0] = '\0';
+                lmp->begline = false;
+                lmp->endline = false;
+              }
             /* Left side--easy */
             i = 0;
             while (lmp->left[i] != '\0' && lmp->left[i] == rmp->left[i])
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 82a6d2d..a38f074 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -76,6 +76,7 @@ TESTS =						\
   max-count-vs-context				\
   mb-non-UTF8-performance			\
   multibyte-white-space				\
+  multiple-begin-or-end-line			\
   null-byte					\
   empty-line-mb					\
   unibyte-bracket-expr				\
diff --git a/tests/multiple-begin-or-end-line b/tests/multiple-begin-or-end-line
new file mode 100755
index 0000000..04a950f
--- /dev/null
+++ b/tests/multiple-begin-or-end-line
@@ -0,0 +1,28 @@
+#!/bin/sh
+# Test a pattern of multiple begin or end line constraints.  This would
+# mistakenly print a line prior to grep-2.19.
+
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+
+echo aa | grep 'a\(b$\|c$\)' >out && fail=1
+compare /dev/null out || fail=1
+
+echo aa | grep '\(^b\|^c\)a' >out && fail=1
+compare /dev/null out || fail=1
+
+Exit $fail
-- 
1.9.3