51e48f
--- a/THANKS
51e48f
+++ b/THANKS
51e48f
@@ -70,6 +70,7 @@ Mike Frysinger             <vapier@gentoo.org>
51e48f
 Miles Bader                <miles@ccs.mt.nec.co.jp>
51e48f
 Mirraz Mirraz              <mirraz1@rambler.ru>
51e48f
 Nelson H. F. Beebe         <beebe@math.utah.edu>
51e48f
+Nima Aghdaii               <naghdaii@fb.com>
51e48f
 Olaf Kirch                 <okir@ns.lst.de>
51e48f
 Paolo Bonzini              <bonzini@gnu.org>
51e48f
 Paul Eggert                <eggert@twinsun.com>
51e48f
--- a/src/kwset.c
51e48f
+++ b/src/kwset.c
51e48f
@@ -643,6 +643,8 @@ bmexec_trans (kwset_t kwset, char const *text, size_t size)
51e48f
                     if (! tp)
51e48f
                       return -1;
51e48f
                     tp++;
51e48f
+                    if (ep <= tp)
51e48f
+                      break;
51e48f
                   }
51e48f
               }
51e48f
           }
51e48f
--- a/tests/Makefile.am
51e48f
+++ b/tests/Makefile.am
51e48f
@@ -72,6 +72,7 @@ TESTS =						\
51e48f
   inconsistent-range				\
51e48f
   invalid-multibyte-infloop			\
51e48f
   khadafy					\
51e48f
+  kwset-abuse					\
51e48f
   long-line-vs-2GiB-read			\
51e48f
   max-count-overread				\
51e48f
   max-count-vs-context				\
51e48f
--- a/tests/Makefile.in
51e48f
+++ b/tests/Makefile.in
51e48f
@@ -1371,6 +1371,7 @@ TESTS = \
51e48f
   inconsistent-range				\
51e48f
   invalid-multibyte-infloop			\
51e48f
   khadafy					\
51e48f
+  kwset-abuse					\
51e48f
   long-line-vs-2GiB-read			\
51e48f
   max-count-overread				\
51e48f
   max-count-vs-context				\
51e48f
@@ -2021,6 +2022,13 @@ khadafy.log: khadafy
51e48f
 	--log-file $$b.log --trs-file $$b.trs \
51e48f
 	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
51e48f
 	"$$tst" $(AM_TESTS_FD_REDIRECT)
51e48f
+kwset-abuse.log: kwset-abuse
51e48f
+	@p='kwset-abuse'; \
51e48f
+	b='kwset-abuse'; \
51e48f
+	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
51e48f
+	--log-file $$b.log --trs-file $$b.trs \
51e48f
+	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
51e48f
+	"$$tst" $(AM_TESTS_FD_REDIRECT)
51e48f
 long-line-vs-2GiB-read.log: long-line-vs-2GiB-read
51e48f
 	@p='long-line-vs-2GiB-read'; \
51e48f
 	b='long-line-vs-2GiB-read'; \
51e48f
--- a/dev/null
51e48f
+++ b/tests/kwset-abuse
51e48f
@@ -0,0 +1,32 @@
51e48f
+#! /bin/sh
51e48f
+# Evoke a segfault in a hard-to-reach code path of kwset.c.
51e48f
+# This bug affected grep versions 2.19 through 2.21.
51e48f
+#
51e48f
+# Copyright (C) 2015 Free Software Foundation, Inc.
51e48f
+#
51e48f
+# This program is free software: you can redistribute it and/or modify
51e48f
+# it under the terms of the GNU General Public License as published by
51e48f
+# the Free Software Foundation, either version 3 of the License, or
51e48f
+# (at your option) any later version.
51e48f
+
51e48f
+# This program is distributed in the hope that it will be useful,
51e48f
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
51e48f
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51e48f
+# GNU General Public License for more details.
51e48f
+
51e48f
+# You should have received a copy of the GNU General Public License
51e48f
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
51e48f
+
51e48f
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
51e48f
+
51e48f
+fail=0
51e48f
+
51e48f
+# This test case chooses a haystack of size 260,000, since prodding
51e48f
+# with gdb showed a reallocation slightly larger than that in fillbuf.
51e48f
+# To reach the buggy code, the needle must have length < 1/11 that of
51e48f
+# the haystack, and 10,000 is a nice round number that fits the bill.
51e48f
+printf '%0260000dXy\n' 0 | grep -F $(printf %010000dy 0)
51e48f
+
51e48f
+test $? = 1 || fail=1
51e48f
+
51e48f
+Exit $fail