Subject: Re: bug#18738: Reference of a uninitialized variable in grep -P

From: Paul Eggert <[email-masked]>
Date: Wed, 15 Oct 2014 11:51:57 -0700

Thanks for reporting that.  It's a bug in one of the optimizations I 
recently added to work around the libpcre slownesses with UTF-8.  It's 
also a bug in our test cases, which should have caught the bug.  I fixed 
it with the attached patch.
>From bc66a23879afc54063708afce4f79d0928e444c0 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 15 Oct 2014 11:49:49 -0700
Subject: [PATCH] grep: fix off-by-one bug in -P optimization

Reported by Norihiro Tanaka in: http://bugs.gnu.org/18738
* src/pcresearch.c (Pexecute): Fix off-by-one bug with
validation_boundary.
* tests/init.cfg (envvar_check_fail): Catch off-by-one bug.
---
 src/pcresearch.c | 2 +-
 tests/init.cfg   | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/pcresearch.c b/src/pcresearch.c
index 6f016b6..1fd5bde 100644
--- a/src/pcresearch.c
+++ b/src/pcresearch.c
@@ -214,7 +214,7 @@ Pexecute (char const *buf, size_t size, size_t *match_size,
             options |= PCRE_NO_UTF8_CHECK;
 
           int valid_bytes = validated - p;
-          if (valid_bytes < 0)
+          if (valid_bytes <= 0)
             {
               e = pcre_exec (cre, extra, p, search_bytes, 0,
                              options, sub, NSUB);
diff --git a/tests/init.cfg b/tests/init.cfg
index 3e9ab03..882feb1 100644
--- a/tests/init.cfg
+++ b/tests/init.cfg
@@ -47,7 +47,11 @@ require_timeout_()
 
 require_pcre_()
 {
-  echo . | grep -P . 2>err || skip_ no PCRE support
+  echo . | grep -P . 2>err
+  echo . | grep -P . 2>err || {
+    test $? -eq 1 && fail_ PCRE available, but does not work.
+    skip_ no PCRE support
+  }
   compare /dev/null err || fail_ PCRE available, but stderr not empty.
 }
 
-- 
1.9.3