Subject: bug#43527: [PATCH] grep: avoid unneeded compilation of regex

From: Norihiro Tanaka <[email-masked]>
Date: Wed, 23 Sep 2020 12:04:26 +0900

On Tue, 22 Sep 2020 16:25:06 -0700
Jim Meyering <[email-masked]> wrote:

> Oh! Good timing. I was about to make a new snapshot.
> Do you happen to have a test case handy that demonstrates the failure?

I added test case to previous patch.

By the way, I found the following bug in making the test case, and it's
still left.

$ env LC_ALL=tr_TR.utf8 grep -Fio i in
Aborted (core dumped)

(gdb) bt
#0  0x0000003b8d032495 in raise () from /lib64/libc.so.6
#1  0x0000003b8d033c75 in abort () from /lib64/libc.so.6
#2  0x000000000040cdde in kwsinit (mb_trans=true) at searchutils.c:64
#3  0x0000000000409624 in Fcompile (pattern=0x23c1240 "i\n", size=1, ignored=0, exact=true) at kwsearch.c:56
#4  0x0000000000409378 in main (argc=4, argv=0x7ffe76048388) at grep.c:2977
From 6118c3ee14c6131ec544244b1fabf05c3a913bd6 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Wed, 23 Sep 2020 07:33:32 +0900
Subject: [PATCH] grep: fix a bug in the previous commit

* src/kwsearch.c (struct kwsearch): Add new member.
(Fexecute): Use it.
* tests/incomplete-regex: Add test for the changes.
* tests/Makefile.am: Run the new test.
---
 src/kwsearch.c         |    8 ++++++--
 tests/Makefile.am      |    1 +
 tests/incomplete-regex |   20 ++++++++++++++++++++
 3 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100755 tests/incomplete-regex

diff --git a/src/kwsearch.c b/src/kwsearch.c
index 1174dbc..820352b 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -38,6 +38,9 @@ struct kwsearch
   char *pattern;
   size_t size;
 
+  /* True when the precision of either -o or --color is required.  */
+  bool exact;
+
   /* The user's pattern compiled as a regular expression,
      or null if it has not been compiled.  */
   void *re;
@@ -128,6 +131,7 @@ Fcompile (char *pattern, size_t size, reg_syntax_t ignored, bool exact)
   kwsearch->words = words;
   kwsearch->pattern = pattern;
   kwsearch->size = size;
+  kwsearch->exact = exact;
   kwsearch->re = NULL;
   return kwsearch;
 }
@@ -178,7 +182,7 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
             {
               fgrep_to_grep_pattern (&kwsearch->pattern, &kwsearch->size);
               kwsearch->re = GEAcompile (kwsearch->pattern, kwsearch->size,
-                                         RE_SYNTAX_GREP, !!start_ptr);
+                                         RE_SYNTAX_GREP, kwsearch->exact);
             }
           return EGexecute (kwsearch->re, buf, size, match_size, start_ptr);
         }
@@ -245,7 +249,7 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
                     fgrep_to_grep_pattern (&kwsearch->pattern, &kwsearch->size);
                     kwsearch->re = GEAcompile (kwsearch->pattern,
                                                kwsearch->size,
-                                               RE_SYNTAX_GREP, !!start_ptr);
+                                               RE_SYNTAX_GREP, kwsearch->exact);
                   }
                 if (beg + len < buf + size)
                   {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 83e7087..a4d9a80 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -113,6 +113,7 @@ TESTS =						\
   high-bit-range				\
   in-eq-out-infloop				\
   include-exclude				\
+  incomplete-regex				\
   inconsistent-range				\
   initial-tab					\
   invalid-multibyte-infloop			\
diff --git a/tests/incomplete-regex b/tests/incomplete-regex
new file mode 100755
index 0000000..d5bf375
--- /dev/null
+++ b/tests/incomplete-regex
@@ -0,0 +1,20 @@
+#! /bin/sh
+# Test for the bug in grep -Fio
+#
+# Copyright (C) 2020 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+require_en_utf8_locale_
+
+fail=0
+
+printf '\304\261\n' >in || framework_failure_
+
+LC_ALL=en_US.UTF-8 grep -Fio i in >out
+compare in out || fail=1
+
+Exit $fail
-- 
1.7.1