From 1665a237f4faaa4b893804f8b264e3f3f62f86b9 Mon Sep 17 00:00:00 2001 From: Bernhard Voelker Date: Wed, 4 Jan 2023 20:14:01 +0100 Subject: [PATCH] find: fix error diagnostics of options with mandatory, numeric arguments The error diagnostic for wrong invocations with option that require numeric arguments (-inum, -links, -gid, -uid) was wrong and not helpful: $ find -gid find: invalid argument `-gid' to `-gid' * find/parser.c (parse_gid): Remove changing back of the ARG_PTR in the error case; thus simplify. (parse_inum,parse_links,parse_uid): Likewise. (get_num): While at it, mention -gid and -uid in the comment as well. (insert_num): Also improve the error diagnostic in the case the user has provided a non-numeric argument. Previously, it was just "invalid argument". * tests/find/opt-numeric-arg.sh: Add test. * tests/local.mk: Reference it. * NEWS (Bug Fixes): Mention the fix. Reported by Andreas Schwab in --- NEWS | 4 ++++ find/parser.c | 36 +++++++++++++++-------------------- tests/find/opt-numeric-arg.sh | 34 +++++++++++++++++++++++++++++++++ tests/local.mk | 1 + 4 files changed, 54 insertions(+), 21 deletions(-) create mode 100755 tests/find/opt-numeric-arg.sh diff --git a/NEWS b/NEWS index c43297c6..3ac3d7ce 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ GNU findutils NEWS - User visible changes. -*- outline -*- (allout) to match the root directory "/". Previously, a diagnostic falsely claimed that this pattern would not match anything. [#62227] + 'find -inum' (without the mandatory argument) now outputs a correct error + diagnostic. Previously it output: "find: invalid argument `-gid' to `-gid'". + Likewise for -links, -gid and -ui. + ** Changes to the build process findutils now builds again on systems with musl-libc. diff --git a/find/parser.c b/find/parser.c index f1fe7408..78c3f969 100644 --- a/find/parser.c +++ b/find/parser.c @@ -1127,11 +1127,7 @@ parse_gid (const struct parser_table* entry, char **argv, int *arg_ptr) p->est_success_rate = (p->args.numinfo.l_val < 100) ? 0.99 : 0.2; return true; } - else - { - --*arg_ptr; /* don't consume the invalid argument. */ - return false; - } + return false; } @@ -1319,11 +1315,7 @@ parse_inum (const struct parser_table* entry, char **argv, int *arg_ptr) p->need_type = false; return true; } - else - { - --*arg_ptr; /* don't consume the invalid argument. */ - return false; - } + return false; } static bool @@ -1346,11 +1338,7 @@ parse_links (const struct parser_table* entry, char **argv, int *arg_ptr) p->est_success_rate = 1e-3; return true; } - else - { - --*arg_ptr; /* don't consume the invalid argument. */ - return false; - } + return false; } static bool @@ -2415,11 +2403,7 @@ parse_uid (const struct parser_table* entry, char **argv, int *arg_ptr) p->est_success_rate = (p->args.numinfo.l_val < 100) ? 0.99 : 0.2; return true; } - else - { - --*arg_ptr; /* don't consume the invalid argument. */ - return false; - } + return false; } static bool @@ -3334,7 +3318,7 @@ get_num (const char *str, A new predicate node is assigned, along with an argument node obtained with malloc. - Used by -inum and -links parsers. */ + Used by -inum, -uid, -gid and -links parsers. */ static struct predicate * insert_num (char **argv, int *arg_ptr, const struct parser_table *entry) @@ -3364,6 +3348,16 @@ insert_num (char **argv, int *arg_ptr, const struct parser_table *entry) } return our_pred; } + else + { + const char *predicate = argv[(*arg_ptr)-2]; + die (EXIT_FAILURE, 0, + _("non-numeric argument to %s: %s"), + predicate, + quotearg_n_style (0, options.err_quoting_style, numstr)); + /*NOTREACHED*/ + return NULL; + } } return NULL; } diff --git a/tests/find/opt-numeric-arg.sh b/tests/find/opt-numeric-arg.sh new file mode 100755 index 00000000..bbf0dd8c --- /dev/null +++ b/tests/find/opt-numeric-arg.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# Exercise error diagnostics for options with mandatory numeric arguments. + +# Copyright (C) 2023 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 . + +. "${srcdir=.}/tests/init.sh"; fu_path_prepend_ +print_ver_ find + +for o in -inum -links -uid -gid; do + # Check error diagnosic for missing argument. + returns_ 1 find $o >out 2>err || fail=1 + compare /dev/null out || fail=1 + grep -F 'missing argument to' err || { fail=1; cat err; } + + # Check error diagnosic for non-numeric argument. + returns_ 1 find $o foo >out 2>err || fail=1 + compare /dev/null out || fail=1 + grep -F 'non-numeric argument to' err || { fail=1; cat err; } +done + +Exit $fail diff --git a/tests/local.mk b/tests/local.mk index 45a1cc73..ff4dd909 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -121,6 +121,7 @@ all_tests = \ tests/find/debug-missing-arg.sh \ tests/find/used.sh \ tests/find/newer.sh \ + tests/find/opt-numeric-arg.sh \ tests/xargs/conflicting_opts.sh \ tests/xargs/verbose-quote.sh \ $(all_root_tests) -- 2.39.0