summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Voelker <mail@bernhard-voelker.de>2023-01-04 20:14:01 +0100
committerBernhard Voelker <mail@bernhard-voelker.de>2023-01-05 19:43:25 +0100
commit4a7280927285dbcf0ac815aa3aecc65b144e3fa4 (patch)
tree532f9c4018e3443a29b4895442243f6ad3f5ba12
parent6490a8076526ea1b11ea6a4dcf4ce5afdbcabe02 (diff)
downloadfindutils-4a7280927285dbcf0ac815aa3aecc65b144e3fa4.tar.gz
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 <schwab@linux-m68k.org> in <https://lists.gnu.org/r/bug-findutils/2023-01/msg00001.html>
-rw-r--r--NEWS5
-rw-r--r--find/parser.c36
-rwxr-xr-xtests/find/opt-numeric-arg.sh34
-rw-r--r--tests/local.mk1
4 files changed, 55 insertions, 21 deletions
diff --git a/NEWS b/NEWS
index c43297c6..e9a2dae9 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,11 @@ 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 -gid' (without the mandatory argument) now outputs a correct error
+ diagnostic. Previously it output: "find: invalid argument `-gid' to `-gid'".
+ The error diagnostic for non-numeric arguments has been improved as well.
+ Likewise for -inum, -links and -uid.
+
** 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 <https://www.gnu.org/licenses/>.
+
+. "${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)