From 048756314709c57d58e6c4894b44c7c232f19eae Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <noritnk@kcn.ne.jp>
Date: Sun, 5 Oct 2014 12:09:24 +0900
Subject: [PATCH] dfa: make find_pred return NULL for an invalid predicate

This could never happen when invoked via grep, but could have triggered
a bug if dfa.c's find_pred function were invoked by some other program.
* src/dfa.c (find_pred): Return NULL for an invalid predicate.
* tests/invalid-char-class: New file to test for this.
* tests/Makefile.am (TESTS): Add that new file name to the list.
This addresses http://debbugs.gnu.org/18631
---
 src/dfa.c                |  5 ++---
 tests/Makefile.am        |  1 +
 tests/invalid-char-class | 30 ++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100755 tests/invalid-char-class

diff --git a/src/dfa.c b/src/dfa.c
index 52eac37..3b936c8 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -971,9 +971,8 @@ find_pred (const char *str)
   unsigned int i;
   for (i = 0; prednames[i].name; ++i)
     if (STREQ (str, prednames[i].name))
-      break;
-
-  return &prednames[i];
+      return &prednames[i];
+  return NULL;
 }

 /* Multibyte character handling sub-routine for lex.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 33b6adc..53c2f94 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -73,6 +73,7 @@ TESTS =						\
   in-eq-out-infloop				\
   include-exclude				\
   inconsistent-range				\
+  invalid-char-class				\
   invalid-multibyte-infloop			\
   khadafy					\
   kwset-abuse					\
diff --git a/tests/invalid-char-class b/tests/invalid-char-class
new file mode 100755
index 0000000..5060b50
--- /dev/null
+++ b/tests/invalid-char-class
@@ -0,0 +1,30 @@
+#!/bin/sh
+# This use of our DFA-testing helper would fail for grep-2.21.
+
+# Copyright 2014-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
+
+# Add "." to PATH for the use of dfa-match-aux.
+path_prepend_ .
+
+fail=0
+
+echo 'dfaerror: invalid character class' > exp
+LC_ALL=C dfa-match-aux '[[:foo:]]' a > out 2>&1
+compare exp out || fail=1
+
+Exit $fail
-- 
2.3.6

