From 6894816c653adef54f3a85becbf75a865d6d39d6 Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Tue, 16 Dec 2014 12:36:39 +0000 Subject: diagnose too-large numbers better Following on from commit v8.23-82-gaddae94, consistently diagnose numbers that are too large, so as to distinguish from other errors, and make the limits obvious. * gl/modules/xdectoint: A new module implementing xdecto[iu]max(), which handles the common case of parsing a bounded integer and exiting with a diagnostic on error. * gl/lib/xdectoimax.c: The signed variant. * gl/lib/xdectoint.c: The parameterized implementation. * gl/lib/xdectoint.h: The interface. * gl/lib/xdectoumax.c: The unsigned variant. * bootstrap.conf: Reference the new module. * cfg.mk (exclude_file_name_regexp--sc_require_config_h_first): Exclude the parameterized templates. * src/csplit.c: Output EOVERFLOW or ERANGE errors if appropriate. * src/fmt.c: Likewise. * src/fold.c: Likewise. * src/head.c: Likewise. * src/ls.c: Likewise. * src/nl.c: Likewise. * src/nproc.c: Likewise. * src/shred.c: Likewise. * src/shuf.c: Likewise. * src/stdbuf.c: Likewise. * src/stty.c: Likewise. * src/tail.c: Likewise. * src/truncate.c: Likewise. * src/split.c: Likewise. * src/pr.c: Likewise. * tests/pr/pr-tests.pl: Adjust to avoid matching errno diagnostic. * tests/fmt/base.pl: Likewise. * tests/split/l-chunk.sh: Likewise. * tests/misc/shred-negative.sh: Likewise. * tests/misc/tail.pl: Likewise. Also remove the redundant existing ERR_SUBST from test err-6. * tests/ls/hex-option.sh: Check HEX/OCT options. * tests/misc/shred-size.sh: Likewise. * tests/misc/stty-row-col.sh: Likewise. --- src/shuf.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/shuf.c') diff --git a/src/shuf.c b/src/shuf.c index df0092b90..0678c3da6 100644 --- a/src/shuf.c +++ b/src/shuf.c @@ -32,6 +32,7 @@ #include "randperm.h" #include "read-file.h" #include "stdio--.h" +#include "xdectoint.h" #include "xstrtol.h" /* The official name of this program (e.g., no 'g' prefix). */ @@ -422,7 +423,6 @@ main (int argc, char **argv) case 'i': { - unsigned long int argval = 0; char *p = strchr (optarg, '-'); char const *hi_optarg = optarg; bool invalid = !p; @@ -434,22 +434,19 @@ main (int argc, char **argv) if (p) { *p = '\0'; - invalid = ((xstrtoul (optarg, NULL, 10, &argval, NULL) - != LONGINT_OK) - || SIZE_MAX < argval); + lo_input = xdectoumax (optarg, 0, SIZE_MAX, "", + _("invalid input range"), 0); *p = '-'; - lo_input = argval; hi_optarg = p + 1; } - invalid |= ((xstrtoul (hi_optarg, NULL, 10, &argval, NULL) - != LONGINT_OK) - || SIZE_MAX < argval); - hi_input = argval; + hi_input = xdectoumax (hi_optarg, 0, SIZE_MAX, "", + _("invalid input range"), 0); + n_lines = hi_input - lo_input + 1; invalid |= ((lo_input <= hi_input) == (n_lines == 0)); if (invalid) - error (EXIT_FAILURE, 0, _("invalid input range %s"), + error (EXIT_FAILURE, errno, "%s: %s", _("invalid input range"), quote (optarg)); } break; @@ -462,7 +459,7 @@ main (int argc, char **argv) if (e == LONGINT_OK) head_lines = MIN (head_lines, argval); else if (e != LONGINT_OVERFLOW) - error (EXIT_FAILURE, 0, _("invalid line count %s"), + error (EXIT_FAILURE, 0, _("invalid line count: %s"), quote (optarg)); } break; -- cgit v1.2.3-54-g00ecf