7b76bb
From d844b7bbf3952998a906f21ba432aa62a3b9c7c6 Mon Sep 17 00:00:00 2001
7b76bb
From: Bernhard Voelker <mail@bernhard-voelker.de>
7b76bb
Date: Tue, 14 Jun 2016 20:49:42 +0200
7b76bb
Subject: [PATCH] Fix bug #48180: find: avoid segfault for internal '-noop'
7b76bb
 option
7b76bb
7b76bb
The pseudo-option '-noop' was never meant to be exposed to the user
7b76bb
interface.  If specified by the user, find(1) segfaulted.
7b76bb
Bug introduced in commit FINDUTILS_4_3_0-1-12-g6b8a4db.
7b76bb
7b76bb
* find/parser.c (struct parser_table): Rename the parser_name element of
7b76bb
the ARG_NOOP entry from 'noop' to '--noop', thus indicating its pure
7b76bb
internal character.
7b76bb
(found_parser): Return NULL when the user has passed the '---noop' option;
7b76bb
the caller does the error handling.
7b76bb
* find/testsuite/sv-48180-refuse-noop.sh: Add test.
7b76bb
* find/testsuite/Makefile.am (test_shell_progs): Reference the test.
7b76bb
* NEWS (Bug fixes): Document the fix.
7b76bb
7b76bb
Reported by Tavian Barnes <tavianator@tavianator.com> in
7b76bb
    https://savannah.gnu.org/bugs/?48180
7b76bb
7b76bb
Upstream-commit: 595060f28eb5f658fa8d98970959c617fab0f078
7b76bb
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
7b76bb
---
7b76bb
 find/parser.c                          |   6 +-
7b76bb
 find/testsuite/Makefile.am             |   3 +-
7b76bb
 find/testsuite/sv-48180-refuse-noop.sh | 117 +++++++++++++++++++++++++++++++++
7b76bb
 3 files changed, 124 insertions(+), 2 deletions(-)
7b76bb
 create mode 100644 find/testsuite/sv-48180-refuse-noop.sh
7b76bb
7b76bb
diff --git a/find/parser.c b/find/parser.c
7b76bb
index 2d45349..697b2a2 100644
7b76bb
--- a/find/parser.c
7b76bb
+++ b/find/parser.c
7b76bb
@@ -321,7 +321,8 @@ static struct parser_table const parse_table[] =
7b76bb
    */
7b76bb
   {ARG_TEST, "false",                 parse_false,   pred_false}, /* GNU */
7b76bb
   {ARG_TEST, "true",                  parse_true,    pred_true }, /* GNU */
7b76bb
-  {ARG_NOOP, "noop",                  NULL,          pred_true }, /* GNU, internal use only */
7b76bb
+  /* Internal pseudo-option, therefore 3 minus: ---noop.  */
7b76bb
+  {ARG_NOOP, "--noop",                NULL,          pred_true }, /* GNU, internal use only */
7b76bb
 
7b76bb
   /* Various other cases that don't fit neatly into our macro scheme. */
7b76bb
   {ARG_TEST, "help",                  parse_help,    NULL},       /* GNU */
7b76bb
@@ -596,6 +597,9 @@ found_parser (const char *original_arg, const struct parser_table *entry)
7b76bb
    */
7b76bb
   if (entry->type != ARG_POSITIONAL_OPTION)
7b76bb
     {
7b76bb
+      if (entry->type == ARG_NOOP)
7b76bb
+        return NULL;  /* internal use only, trap -noop here.  */
7b76bb
+
7b76bb
       /* Something other than -follow/-daystart.
7b76bb
        * If this is an option, check if it followed
7b76bb
        * a non-option and if so, issue a warning.
7b76bb
diff --git a/find/testsuite/Makefile.am b/find/testsuite/Makefile.am
7b76bb
index ab5dbe8..1371c70 100644
7b76bb
--- a/find/testsuite/Makefile.am
7b76bb
+++ b/find/testsuite/Makefile.am
7b76bb
@@ -259,7 +259,8 @@ test_escape_c.sh \
7b76bb
 test_inode.sh \
7b76bb
 sv-34079.sh \
7b76bb
 sv-34976-execdir-fd-leak.sh \
7b76bb
-sv-48030-exec-plus-bug.sh
7b76bb
+sv-48030-exec-plus-bug.sh \
7b76bb
+sv-48180-refuse-noop.sh
7b76bb
 
7b76bb
 EXTRA_DIST = $(EXTRA_DIST_EXP) $(EXTRA_DIST_XO) $(EXTRA_DIST_GOLDEN) \
7b76bb
 	$(test_shell_progs) binary_locations.sh checklists.py
7b76bb
diff --git a/find/testsuite/sv-48180-refuse-noop.sh b/find/testsuite/sv-48180-refuse-noop.sh
7b76bb
new file mode 100755
7b76bb
index 0000000..974f0f0
7b76bb
--- /dev/null
7b76bb
+++ b/find/testsuite/sv-48180-refuse-noop.sh
7b76bb
@@ -0,0 +1,117 @@
7b76bb
+#! /bin/sh
7b76bb
+# Copyright (C) 2016 Free Software Foundation, Inc.
7b76bb
+#
7b76bb
+# This program is free software: you can redistribute it and/or modify
7b76bb
+# it under the terms of the GNU General Public License as published by
7b76bb
+# the Free Software Foundation, either version 3 of the License, or
7b76bb
+# (at your option) any later version.
7b76bb
+#
7b76bb
+# This program is distributed in the hope that it will be useful,
7b76bb
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
7b76bb
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7b76bb
+# GNU General Public License for more details.
7b76bb
+#
7b76bb
+# You should have received a copy of the GNU General Public License
7b76bb
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7b76bb
+#
7b76bb
+
7b76bb
+# This test verifies that find refuses the internal -noop, ---noop option.
7b76bb
+# Between findutils-4.3.1 and 4.6, find dumped core ($? = 139).
7b76bb
+
7b76bb
+testname="$(basename $0)"
7b76bb
+
7b76bb
+. "${srcdir}"/binary_locations.sh
7b76bb
+
7b76bb
+die() {
7b76bb
+  echo "$@" >&2
7b76bb
+  exit 1
7b76bb
+}
7b76bb
+
7b76bb
+# This is used to simplify checking of the return value
7b76bb
+# which is useful when ensuring a command fails as desired.
7b76bb
+# I.e., just doing `command ... &&fail=1` will not catch
7b76bb
+# a segfault in command for example.  With this helper you
7b76bb
+# instead check an explicit exit code like
7b76bb
+#   returns_ 1 command ... || fail
7b76bb
+returns_ () {
7b76bb
+  # Disable tracing so it doesn't interfere with stderr of the wrapped command
7b76bb
+  { set +x; } 2>/dev/null
7b76bb
+
7b76bb
+  local exp_exit="$1"
7b76bb
+  shift
7b76bb
+  "$@"
7b76bb
+  test $? -eq $exp_exit && ret_=0 || ret_=1
7b76bb
+
7b76bb
+  set -x
7b76bb
+  { return $ret_; } 2>/dev/null
7b76bb
+}
7b76bb
+
7b76bb
+# Define the nicest compare available (borrowed from gnulib).
7b76bb
+if diff_out_=`exec 2>/dev/null; diff -u "$0" "$0" < /dev/null` \
7b76bb
+   && diff -u Makefile "$0" 2>/dev/null | grep '^[+]#!' >/dev/null; then
7b76bb
+  # diff accepts the -u option and does not (like AIX 7 'diff') produce an
7b76bb
+  # extra space on column 1 of every content line.
7b76bb
+  if test -z "$diff_out_"; then
7b76bb
+    compare () { diff -u "$@"; }
7b76bb
+  else
7b76bb
+    compare ()
7b76bb
+    {
7b76bb
+      if diff -u "$@" > diff.out; then
7b76bb
+        # No differences were found, but Solaris 'diff' produces output
7b76bb
+        # "No differences encountered". Hide this output.
7b76bb
+        rm -f diff.out
7b76bb
+        true
7b76bb
+      else
7b76bb
+        cat diff.out
7b76bb
+        rm -f diff.out
7b76bb
+        false
7b76bb
+      fi
7b76bb
+    }
7b76bb
+  fi
7b76bb
+elif diff_out_=`exec 2>/dev/null; diff -c "$0" "$0" < /dev/null`; then
7b76bb
+  if test -z "$diff_out_"; then
7b76bb
+    compare () { diff -c "$@"; }
7b76bb
+  else
7b76bb
+    compare ()
7b76bb
+    {
7b76bb
+      if diff -c "$@" > diff.out; then
7b76bb
+        # No differences were found, but AIX and HP-UX 'diff' produce output
7b76bb
+        # "No differences encountered" or "There are no differences between the
7b76bb
+        # files.". Hide this output.
7b76bb
+        rm -f diff.out
7b76bb
+        true
7b76bb
+      else
7b76bb
+        cat diff.out
7b76bb
+        rm -f diff.out
7b76bb
+        false
7b76bb
+      fi
7b76bb
+    }
7b76bb
+  fi
7b76bb
+elif cmp -s /dev/null /dev/null 2>/dev/null; then
7b76bb
+  compare () { cmp -s "$@"; }
7b76bb
+else
7b76bb
+  compare () { cmp "$@"; }
7b76bb
+fi
7b76bb
+
7b76bb
+set -x
7b76bb
+tmpdir="$(mktemp -d)" \
7b76bb
+  && cd "$tmpdir" \
7b76bb
+  || die "FAIL: failed to set up the test in ${tmpdir}"
7b76bb
+
7b76bb
+fail=0
7b76bb
+# Exercise both the previous name of the pseudo-option '-noop',
7b76bb
+# and the now renamed '---noop' option for both find executables.
7b76bb
+for exe in "${ftsfind}" "${oldfind}"; do
7b76bb
+  for opt in 'noop' '--noop'; do
7b76bb
+    out="${exe}${opt}.out"
7b76bb
+    err="${exe}${opt}.err"
7b76bb
+    returns_ 1 "$exe" "-${opt}" >"$out" 2> "$err" || fail=1
7b76bb
+    compare /dev/null "$out" || fail=1
7b76bb
+    grep "find: unknown predicate .-${opt}." "$err" \
7b76bb
+      || { cat "$err"; fail=1; }
7b76bb
+  done
7b76bb
+done
7b76bb
+
7b76bb
+cd ..
7b76bb
+rm -rf "$tmpdir" || exit 1
7b76bb
+exit $fail
7b76bb
-- 
7b76bb
2.5.5
7b76bb