From bb03809aa8eb577ce8e7747a9ea39d9e594a2c11 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <noritnk@kcn.ne.jp>
Date: Mon, 9 Jan 2017 08:54:28 +0900
Subject: [PATCH 1/2] tests: new test for dfa crash bug

sed-4.3 would segfault with:
   printf '$LINENO $LINEN\nB\n' | sed -e 'N;s/\$LINENO\(.*\n\)/\1/'

Culprit is dfa crash if multi-lines are read in pattern space.
It is fixed at gnulib commit v0.1-1117-g823b5cb .
Reported by S. Gilles in https://bugs.gnu.org/25390

* testsuite/newline.sh: New test.
* testsuite/newline-valgrind.sh: Repeat above test, with valgrind.
* testsuite/local.mk: Add the tests.
---
 testsuite/local.mk            |  2 ++
 testsuite/newline-valgrind.sh | 38 ++++++++++++++++++++++++++++++++++++++
 testsuite/newline.sh          | 28 ++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)
 create mode 100755 testsuite/newline-valgrind.sh
 create mode 100755 testsuite/newline.sh

diff --git a/testsuite/local.mk b/testsuite/local.mk
index fae6225..2a5cd4d 100644
--- a/testsuite/local.mk
+++ b/testsuite/local.mk
@@ -38,6 +38,8 @@ T =					\
   testsuite/mb-charclass-non-utf8.sh	\
   testsuite/mb-match-slash.sh		\
   testsuite/mb-y-translate.sh		\
+  testsuite/newline.sh			\
+  testsuite/newline-valgrind.sh		\
   testsuite/normalize-text.sh		\
   testsuite/nulldata.sh			\
   testsuite/panic-tests.sh		\
diff --git a/testsuite/newline-valgrind.sh b/testsuite/newline-valgrind.sh
new file mode 100755
index 0000000..91f1ed1
--- /dev/null
+++ b/testsuite/newline-valgrind.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+# sed may access to uninitialized memory if transit to 15th dfa state
+# with newline.  This bug affected sed version 4.3.
+
+# Copyright (C) 2017 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=.}/testsuite/init.sh"; path_prepend_ ./sed
+print_ver_ sed
+
+require_valgrind_
+
+printf 'abcdefg abcdefg\nB\n' > in || framework_failure_
+printf 'B\n' > exp || framework_failure_
+
+valgrind --quiet --error-exitcode=1 \
+  sed 'N;s/abcdefg.*\n//' in > out 2> err || fail=1
+
+# Work around a bug in CentOS 5.10's valgrind
+# FIXME: remove in 2018 or when CentOS 5 is no longer officially supported
+grep 'valgrind: .*Assertion.*failed' err > /dev/null \
+  && skip_ 'you seem to have a buggy version of valgrind'
+
+compare exp out || fail=1
+compare /dev/null err || fail=1
+
+Exit $fail
diff --git a/testsuite/newline.sh b/testsuite/newline.sh
new file mode 100755
index 0000000..72bb2ec
--- /dev/null
+++ b/testsuite/newline.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+# sed may access to uninitialized memory if transit to 15th dfa state
+# with newline.  This bug affected sed version 4.3.
+
+# Copyright (C) 2017 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=.}/testsuite/init.sh"; path_prepend_ ./sed
+print_ver_ sed
+
+printf 'abcdefg abcdefg\nB\n' > in || framework_failure_
+printf 'B\n' > exp || framework_failure_
+sed 'N;s/abcdefg.*\n//' in > out || fail=1
+
+compare exp out || fail=1
+
+Exit $fail
-- 
2.10.2


From 5f611a1b8a980acce339305dc978a2cdd9dd2909 Mon Sep 17 00:00:00 2001
From: Assaf Gordon <assafgordon@gmail.com>
Date: Tue, 10 Jan 2017 00:09:54 -0500
Subject: [PATCH 2/2] gnulib: update to latest (with DFA fixes for bug#25390)

---
 gnulib | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gnulib b/gnulib
index a3fd683..8d3c493 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit a3fd683de3decbb58ab5fb5d32ad2e62f74fbf12
+Subproject commit 8d3c4933ccfae50a15bcb088161bfd33adcdbf6d
-- 
2.10.2

