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

From: Norihiro Tanaka <[email-masked]>
Date: Wed, 23 Sep 2020 07:57:30 +0900

On Tue, 22 Sep 2020 08:50:03 -0700
Jim Meyering <[email-masked]> wrote:

> On Tue, Sep 22, 2020 at 7:54 AM Norihiro Tanaka <[email-masked]> wrote:
> > On Mon, 21 Sep 2020 17:33:25 -0700
> > Jim Meyering <[email-masked]> wrote:
> ...
> > > Here are the two patches (tested on top of a third that updates to
> > > latest gnulib). I'll await an 'ok' from Norihiro Tanaka before
> > > pushing, since commit-log metadata is essentially immutable once
> > > pushed.
> >
> > Great, thank you.  I confirmed it.
> 
> Thanks. Pushed.

Oh, I found a bug for this fix.  If Fexecute is called first without
start_ptr and next with start_ptr, it may break.
From 52996da41c9d430ac4d9a6932634fd12495bb8f2 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.
---
 src/kwsearch.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

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)
                   {
-- 
1.7.1