From f9df2006dfd1840e4e793b0a44d3318fd4fb0a09 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@fb.com>
Date: Sat, 16 Nov 2019 09:24:00 -0800
Subject: [PATCH] grep: avoid false -Fw match in non-UTF8 multibyte locales

For example, this command would erroneously print its input line:
  echo ab | LC_CTYPE=ja_JP.eucjp grep -Fw b
* src/kwsearch.c (Fexecute): Set MB_START also when there is no
preceding newline.
Reported by NIDE, Naoyuki in https://bugs.gnu.org/38223.
---
 src/kwsearch.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/kwsearch.c b/src/kwsearch.c
index 42567e9..2ad8a87 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -223,12 +223,14 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
       if (! match_words)
         goto success;

+      /* We need a preceding mb_start pointer.  Use the beginning of line
+         if there is a preceding newline, else BUF. */
+      char const *bol = memrchr (mb_start, eol, beg - mb_start);
+      mb_start = bol ? bol + 1 : buf;
+
       /* Succeed if the preceding and following characters are word
          constituents.  If the following character is not a word
          constituent, keep trying with shorter matches.  */
-      char const *bol = memrchr (mb_start, eol, beg - mb_start);
-      if (bol)
-        mb_start = bol + 1;
       if (! wordchar_prev (mb_start, beg, buf + size))
         for (;;)
           {
-- 
2.24.0.155.gd9f6f3b619

