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