Subject: Re: bug#43863: [PATCH] grep: remove unusable code

From: Paul Eggert <[email-masked]>
Date: Sun, 11 Oct 2020 09:56:36 -0700

Thanks for the patch. I installed it, and also installed the attached minor 
cleanups of related code that I discovered while reviewing it.
>From f31abf786f61f4bdd7134559a5f155fc9c8c2513 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 11 Oct 2020 09:53:22 -0700
Subject: [PATCH] grep: minor kwset cleanups

* src/kwsearch.c (Fexecute):
Assume C99 to put declarations nearer uses.
* src/kwset.c (bmexec): Omit unnecessary test.
* src/kwset.h (struct kwsmatch): Make OFFSET and SIZE individual
elements, not arrays of size 1 (a revenant of an earlier API).
All uses changed.
---
 src/kwsearch.c | 36 ++++++++++++------------------------
 src/kwset.c    | 14 +++++---------
 src/kwset.h    |  6 +++---
 3 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/src/kwsearch.c b/src/kwsearch.c
index 1b31917..685502d 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -87,8 +87,8 @@ Fcompile (char *pattern, size_t size, reg_syntax_t ignored, bool exact)
   while (p <= pattern + size);
 
   free (buf);
-  ptrdiff_t words = kwswords (kwset);
 
+  ptrdiff_t words = kwswords (kwset);
   kwsprep (kwset);
 
   struct kwsearch *kwsearch = xmalloc (sizeof *kwsearch);
@@ -111,32 +111,22 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
   char const *beg, *end, *mb_start;
   ptrdiff_t len;
   char eol = eolbyte;
-  struct kwsmatch kwsmatch;
-  size_t ret_val;
-  bool mb_check;
-  bool longest;
   struct kwsearch *kwsearch = vcp;
   kwset_t kwset = kwsearch->kwset;
-  size_t mbclen;
-
-  if (match_lines)
-    mb_check = longest = false;
-  else
-    {
-      mb_check = localeinfo.multibyte & !localeinfo.using_utf8;
-      longest = mb_check | !!start_ptr | match_words;
-    }
+  bool mb_check = localeinfo.multibyte & !localeinfo.using_utf8 & !match_lines;
+  bool longest = (mb_check | !!start_ptr | match_words) & !match_lines;
 
   for (mb_start = beg = start_ptr ? start_ptr : buf; beg <= buf + size; beg++)
     {
+      struct kwsmatch kwsmatch;
       ptrdiff_t offset = kwsexec (kwset, beg - match_lines,
                                   buf + size - beg + match_lines, &kwsmatch,
                                   longest);
       if (offset < 0)
         break;
-      len = kwsmatch.size[0] - 2 * match_lines;
+      len = kwsmatch.size - 2 * match_lines;
 
-      mbclen = 0;
+      size_t mbclen = 0;
       if (mb_check
           && mb_goback (&mb_start, &mbclen, beg + offset, buf + size) != 0)
         {
@@ -216,10 +206,11 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
               }
             if (!len)
               break;
-            offset = kwsexec (kwset, beg, --len, &kwsmatch, true);
-            if (offset != 0)
+
+            struct kwsmatch shorter_match;
+            if (kwsexec (kwset, beg, --len, &shorter_match, true) != 0)
               break;
-            len = kwsmatch.size[0];
+            len = shorter_match.size;
           }
 
       /* No word match was found at BEG.  Skip past word constituents,
@@ -227,7 +218,7 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
          them could make things much slower.  */
       beg += wordchars_size (beg, buf + size);
       mb_start = beg;
-    } /* for (beg in buf) */
+    }
 
   return -1;
 
@@ -244,9 +235,6 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
   beg = beg ? beg + 1 : buf;
   len = end - beg;
  success_in_beg_and_len:;
-  size_t off = beg - buf;
-
   *match_size = len;
-  ret_val = off;
-  return ret_val;
+  return beg - buf;
 }
diff --git a/src/kwset.c b/src/kwset.c
index 403af7e..b271e39 100644
--- a/src/kwset.c
+++ b/src/kwset.c
@@ -760,13 +760,9 @@ bmexec (kwset_t kwset, char const *text, ptrdiff_t size,
                    (kwset->trans
                     ? bmexec_trans (kwset, text, size)
                     : bmexec_trans (kwset, text, size)));
-  if (0 <= ret)
-    {
-       kwsmatch->index = 0;
-       kwsmatch->offset[0] = ret;
-       kwsmatch->size[0] = kwset->mind;
-    }
-
+  kwsmatch->index = 0;
+  kwsmatch->offset = ret;
+  kwsmatch->size = kwset->mind;
   return ret;
 }
 
@@ -891,8 +887,8 @@ acexec_trans (kwset_t kwset, char const *text, ptrdiff_t len,
     }
 
   kwsmatch->index = accept->accepting / 2;
-  kwsmatch->offset[0] = left - text;
-  kwsmatch->size[0] = accept->depth;
+  kwsmatch->offset = left - text;
+  kwsmatch->size = accept->depth;
 
   return left - text;
 }
diff --git a/src/kwset.h b/src/kwset.h
index 793e210..eee3d73 100644
--- a/src/kwset.h
+++ b/src/kwset.h
@@ -24,9 +24,9 @@
 
 struct kwsmatch
 {
-  ptrdiff_t index;			/* Index number of matching keyword.  */
-  ptrdiff_t offset[1];		/* Offset of match.  */
-  ptrdiff_t size[1];		/* Length of match.  */
+  ptrdiff_t index;	/* Index number of matching keyword.  */
+  ptrdiff_t offset;	/* Offset of match.  */
+  ptrdiff_t size;	/* Length of match.  */
 };
 
 #include "arg-nonnull.h"
-- 
2.25.1