]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
seq: do not allow NaN arguments
authorBernhard Voelker <mail@bernhard-voelker.de>
Thu, 14 Apr 2016 10:39:28 +0000 (12:39 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Thu, 14 Apr 2016 10:39:28 +0000 (12:39 +0200)
* src/seq.c (isnan): Define macro.
(scan_arg): Add check if the given argument is NaN, and exit with
a proper error diagnostic in such a case.
(usage): Document it.
* tests/misc/seq.pl: Add tests.
* doc/coreutils.texi (seq invocation): Document the change.
* NEWS (Changes in behavior): Mention the change.

NEWS
doc/coreutils.texi
src/seq.c
tests/misc/seq.pl

diff --git a/NEWS b/NEWS
index 13af702420b86c93b629015e7d62d9449d70461f..4cb5caf84ea00edd96b0d14443853c13b9fb4d29 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -17,7 +17,8 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Changes in behavior
 
-   seq no longer accepts 0 value as increment argument.
+   seq no longer accepts 0 value as increment, and now also rejects NaN
+   values for any argument.
 
    stat now outputs nanosecond information for time stamps even if
    they are out of localtime range.
index 6b706359e532fdc048432ffc2604379752920be3..5630201488ab815f5e1bfae39bc2ebc6ebbe0339 100644 (file)
@@ -17424,6 +17424,7 @@ The sequence of numbers ends when the sum of the current number and
 so @code{seq 1 10 10} only produces @samp{1}.
 @var{increment} must not be @samp{0}; use @command{yes} to get
 repeated output of a constant number.
+@var{first}, @var{increment} and @var{last} must not be @code{NaN}.
 Floating-point numbers may be specified.  @xref{Floating point}.
 
 The program accepts the following options.  Also see @ref{Common options}.
index 91cf625e971889ca8b98b2700e86c87ee334eb6f..de92bc278378712bcf07a0671b0f0c1e1af2e262 100644 (file)
--- a/src/seq.c
+++ b/src/seq.c
 #include "quote.h"
 #include "xstrtod.h"
 
-/* Roll our own isfinite rather than using <math.h>, so that we don't
+/* Roll our own isfinite/isnan rather than using <math.h>, so that we don't
    have to worry about linking -lm just for isfinite.  */
 #ifndef isfinite
 # define isfinite(x) ((x) * 0 == 0)
 #endif
+#ifndef isnan
+# define isnan(x) ((x) != (x))
+#endif
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "seq"
@@ -92,7 +95,7 @@ INCREMENT would become greater than LAST.\n\
 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
 INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
 INCREMENT is usually negative if FIRST is greater than LAST.\n\
-INCREMENT must not be 0.\n\
+INCREMENT must not be 0; none of FIRST, INCREMENT and LAST may be NaN.\n\
 "), stdout);
       fputs (_("\
 FORMAT must be suitable for printing one argument of type 'double';\n\
@@ -144,6 +147,13 @@ scan_arg (const char *arg)
       usage (EXIT_FAILURE);
     }
 
+  if (isnan (ret.value))
+    {
+      error (0, 0, _("invalid %s argument: %s"), quote_n (0, "not-a-number"),
+             quote_n (1, arg));
+      usage (EXIT_FAILURE);
+    }
+
   /* We don't output spaces or '+' so don't include in width */
   while (isspace (to_uchar (*arg)) || *arg == '+')
     arg++;
index ac0664fc674803d1d36d8285b718ae6624ea9442..130bbf3d70da99c1b50cfcc0ecca9ad6c19e2f7d 100755 (executable)
@@ -26,6 +26,7 @@ use strict;
 my $prog = 'seq';
 my $try_help = "Try '$prog --help' for more information.\n";
 my $err_inc_zero = "seq: invalid Zero increment value: '0'\n".$try_help;
+my $err_nan_arg = "seq: invalid 'not-a-number' argument: 'nan'\n".$try_help;
 
 my $locale = $ENV{LOCALE_FR_UTF8};
 ! defined $locale || $locale eq 'none'
@@ -161,6 +162,22 @@ my @Tests =
     {ERR_SUBST => 's/0.0/0/'}],
    ['inc-zero-4',      qw(1 -0.0e-10 10), {EXIT => 1},{ERR => $err_inc_zero},
     {ERR_SUBST => 's/-0\.0e-10/0/'}],
+
+   # Ensure NaN arguments rejected.
+   ['nan-first-1', qw(nan),       {EXIT => 1}, {ERR => $err_nan_arg}],
+   ['nan-first-2', qw(NaN 2),     {EXIT => 1}, {ERR => $err_nan_arg},
+    {ERR_SUBST => 's/NaN/nan/'}],
+   ['nan-first-3', qw(nan 1 2),   {EXIT => 1}, {ERR => $err_nan_arg}],
+   ['nan-first-4', qw(-- -nan),   {EXIT => 1}, {ERR => $err_nan_arg},
+    {ERR_SUBST => 's/-nan/nan/'}],
+   ['nan-inc-1',   qw(1 nan 2),   {EXIT => 1}, {ERR => $err_nan_arg}],
+   ['nan-inc-2',   qw(1 -NaN 2),  {EXIT => 1}, {ERR => $err_nan_arg},
+    {ERR_SUBST => 's/-NaN/nan/'}],
+   ['nan-last-1',  qw(1 1 nan),   {EXIT => 1}, {ERR => $err_nan_arg}],
+   ['nan-last-2',  qw(1 NaN),     {EXIT => 1}, {ERR => $err_nan_arg},
+    {ERR_SUBST => 's/NaN/nan/'}],
+   ['nan-last-3',  qw(0 -1 -NaN), {EXIT => 1}, {ERR => $err_nan_arg},
+    {ERR_SUBST => 's/-NaN/nan/'}],
   );
 
 # Append a newline to each entry in the OUT array.