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

From: [email-masked] (GNU bug Tracking System)
Date: Wed, 15 Oct 2014 18:53:03 +0000

Your bug report

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

which was filed against the grep package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
18738: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18738
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#18738: Reference of a uninitialized variable in grep -P
From: Paul Eggert <[email-masked]>
To: Norihiro Tanaka <[email-masked]>, [email-masked]
Date: Wed, 15 Oct 2014 11:51:57 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

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

Subject: Reference of a uninitialized variable in grep -P
From: Norihiro Tanaka <[email-masked]>
To: [email-masked]
Date: Thu, 16 Oct 2014 00:57:07 +0900
List-Id: <debbugs-submit.debbugs.gnu.org>

A variable validated_boundary is used in grep -P.  However, it isn't
initialized correctly.  I found that it causes reference of the
uninitialized variable and wrong result.

  $ echo . | env LC_ALL=C src/grep -P .
  $ echo . | env LC_ALL=en_US.utf8 src/grep -P .

We expect that they return each one line, but return each no line.  Their
test cases are also used to judge whether pcre option is enabled in tests.
So all tests with pcre option will be skipped by the bug.

Sorry, I have no idea to fix the bug.  BTW, the bug isn't reproduced in
grep-2.20 or prior.