arn...@skeeve.com wrote:
Y'all might want to consider basing RE_SYNTAX_POSIX_EGREP
off of RE_SYNTAX_POSIX_EXTENDED; that would have gotten this case.

At some point we should clean out those old RE_SYNTAX_* macros; half of them are just plain wrong anyway. However, doing that will require a fix to glibc, which would take quite a long time to propagate through, and in the meantime the attached patch should suffice. I've installed it and it should appear in the next grep release. Thanks for reporting the bug, Nathan.
From 626eba1b8a7ccd839fe96ee94800b059c5981461 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Fri, 27 Jun 2014 12:31:29 -0700
Subject: [PATCH] grep: with -E, unmatched ')' matches itself

Problem reported by Nathan Weeks in: http://bugs.gnu.org/17856
* src/grep.c (Ecompile): Also specify RE_UNMATCHED_RIGHT_PAREN_ORD.
* doc/grep.texi (Fundamental Structure), NEWS: Document this.
* tests/ere.tests: Add a couple of tests for this.
* tests/spencer1.tests: Fix exit status.
---
 NEWS                 | 4 ++++
 doc/grep.texi        | 1 +
 src/grep.c           | 4 +++-
 tests/ere.tests      | 3 +++
 tests/spencer1.tests | 2 +-
 5 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 1af3def..550bf4c 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,10 @@ GNU grep NEWS                                    -*- outline 
-*-
 
   Performance has improved for very long strings in patterns.
 
+** Bug fixes
+
+  grep -E rejected unmatched ')', instead of treating it like '\)'.
+  [bug present since "the beginning"]
 
 * Noteworthy changes in release 2.20 (2014-06-03) [stable]
 
diff --git a/doc/grep.texi b/doc/grep.texi
index c9417e5..10cc2e4 100644
--- a/doc/grep.texi
+++ b/doc/grep.texi
@@ -1208,6 +1208,7 @@ Repetition takes precedence over concatenation,
 which in turn takes precedence over alternation.
 A whole expression may be enclosed in parentheses
 to override these precedence rules and form a subexpression.
+An unmatched @samp{)} matches just itself.
 
 @node Character Classes and Bracket Expressions
 @section Character Classes and Bracket Expressions
diff --git a/src/grep.c b/src/grep.c
index 7c0f8a8..f2fca74 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1638,7 +1638,9 @@ Gcompile (char const *pattern, size_t size)
 static void
 Ecompile (char const *pattern, size_t size)
 {
-  GEAcompile (pattern, size, RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
+  GEAcompile (pattern, size,
+              (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES
+               | RE_UNMATCHED_RIGHT_PAREN_ORD));
 }
 
 static void
diff --git a/tests/ere.tests b/tests/ere.tests
index e0aad2a..8ab6510 100644
--- a/tests/ere.tests
+++ b/tests/ere.tests
@@ -215,3 +215,6 @@
 0@CC[13]1|a{21}[23][EO][123][Es][12]a{15}aa[34][EW]aaaaaaa[X]a@CC11
 0@a?b@ab
 2@b{1000000000}@ESIZE
+0@)@)
+1@)@x
+0@\()\((a\())(b))@()(a()b)
diff --git a/tests/spencer1.tests b/tests/spencer1.tests
index 36c0b9e..50581e8 100644
--- a/tests/spencer1.tests
+++ b/tests/spencer1.tests
@@ -59,7 +59,7 @@
 0@a\(*b@ab
 0@a\(*b@a((b
 1@a\x@a\x
-2@abc)@-
+1@abc)@-
 2@(abc@-
 0@((a))@abc
 0@(a)b(c)@abc
-- 
1.9.3

Reply via email to