]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
seq: do not allow 0 as increment value
authorBernhard Voelker <mail@bernhard-voelker.de>
Thu, 14 Apr 2016 10:38:09 +0000 (12:38 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Thu, 14 Apr 2016 10:38:09 +0000 (12:38 +0200)
* src/seq.c (main): Exit with an error diagnostic when the given
step value is Zero.
(usage): Document it.
* doc/coreutils.texi (seq invocation): Likewise.
* tests/misc/seq.pl: Add tests.
* NEWS (Changes in behavior): Mention the change.
Reported by Маренков Евгений in:
http://bugs.gnu.org/23110

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

diff --git a/NEWS b/NEWS
index 9445977ad68c3043e7e6f85f992c6dfeba038416..13af702420b86c93b629015e7d62d9449d70461f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,8 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Changes in behavior
 
+   seq no longer accepts 0 value as increment argument.
+
    stat now outputs nanosecond information for time stamps even if
    they are out of localtime range.
 
index 45706bdbf957dd3e87fb6c477b4496f639eb4316..6b706359e532fdc048432ffc2604379752920be3 100644 (file)
@@ -17422,6 +17422,8 @@ even when @var{first} is larger than @var{last}.
 The sequence of numbers ends when the sum of the current number and
 @var{increment} would become greater than @var{last},
 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.
 Floating-point numbers may be specified.  @xref{Floating point}.
 
 The program accepts the following options.  Also see @ref{Common options}.
index fbb94a0c0f170ec2bdea2bf55476df50ed8afb67..91cf625e971889ca8b98b2700e86c87ee334eb6f 100644 (file)
--- a/src/seq.c
+++ b/src/seq.c
@@ -92,6 +92,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\
 "), stdout);
       fputs (_("\
 FORMAT must be suitable for printing one argument of type 'double';\n\
@@ -635,6 +636,13 @@ main (int argc, char **argv)
       if (optind < argc)
         {
           step = last;
+          if (step.value == 0)
+            {
+              error (0, 0, _("invalid Zero increment value: %s"),
+                     quote (argv[optind-1]));
+              usage (EXIT_FAILURE);
+            }
+
           last = scan_arg (argv[optind++]);
         }
     }
index 6564415d4906c4c7ed1c947b06959a63687c5a79..ac0664fc674803d1d36d8285b718ae6624ea9442 100755 (executable)
@@ -25,6 +25,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 $locale = $ENV{LOCALE_FR_UTF8};
 ! defined $locale || $locale eq 'none'
@@ -151,6 +152,15 @@ my @Tests =
    ['fast-1', qw(4), {OUT => [qw(1 2 3 4)]}],
    ['fast-2', qw(1 4), {OUT => [qw(1 2 3 4)]}],
    ['fast-3', qw(1 1 4), {OUT => [qw(1 2 3 4)]}],
+
+   # Ensure an INCREMENT of Zero is rejected.
+   ['inc-zero-1',      qw(1 0 10), {EXIT => 1}, {ERR => $err_inc_zero}],
+   ['inc-zero-2',      qw(0 -0 0), {EXIT => 1}, {ERR => $err_inc_zero},
+    {ERR_SUBST => 's/-0/0/'}],
+   ['inc-zero-3',      qw(1 0.0 10), {EXIT => 1},{ERR => $err_inc_zero},
+    {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/'}],
   );
 
 # Append a newline to each entry in the OUT array.