]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cut: diagnose a range starting with 0 (-f 0-2) as invalid, and
authorJim Meyering <jim@meyering.net>
Tue, 22 May 2007 12:25:19 +0000 (14:25 +0200)
committerJim Meyering <jim@meyering.net>
Tue, 22 May 2007 16:47:17 +0000 (18:47 +0200)
give a better diagnostic for a field-number/offset of 0.
* NEWS: Mention the fix.
* src/cut.c (ADD_RANGE_PAIR): Add an explicit check for 0.
Based on a patch from James Youngman.
* tests/misc/cut: Add tests for the above.

ChangeLog
NEWS
src/cut.c
tests/misc/cut

index c1fe8720521f8a5e24ccc87855f3dd4e2597ac95..464266770950948f56a5b3e9557460a3e299ce65 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2007-05-22  Jim Meyering  <jim@meyering.net>
 
+       cut: diagnose a range starting with 0 (-f 0-2) as invalid, and
+       give a better diagnostic for a field-number/offset of 0.
+       * NEWS: Mention the fix.
+       * src/cut.c (ADD_RANGE_PAIR): Add an explicit check.
+       Based on a patch from James Youngman.
+       * tests/misc/cut: Add tests for the above.
+
        "cut -f 2-0" now fails; before, it was equivalent to "cut -f 2-"
        Also, diagnose the '-' in "cut -f -" as an invalid range, rather
        than interpreting it as the unlimited range, "1-".
@@ -8,6 +15,7 @@
        of 0 as an unspecified range endpoint.
        Give better diagnostics.
        Adjust a comment so that it is true also for 64-bit size_t.
+
        * tests/cut/Test.pm: Add tests for the above.
 
        stty: fix a harmless syntax nit
diff --git a/NEWS b/NEWS
index 03266813df189ed4af96a155a4e578bf989a25d0..ea08e0a929ca56573e5e0822821617ca31c4ba27 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Bug fixes
 
+  cut now diagnoses a range starting with zero (e.g., -f 0-2) as invalid;
+  before, it would treat it as if it started with 1 (-f 1-2).
+
   "cut -f 2-0" now fails; before, it was equivalent to "cut -f 2-"
 
   cut now diagnoses the '-' in "cut -f -" as an invalid range, rather
index ab14abcb2c0552956b330a81d48ee298f85061c7..e89460e1b72bf9fdd7afafc1c781b27f55605c5f 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -57,6 +57,8 @@
 #define ADD_RANGE_PAIR(rp, low, high)                  \
   do                                                   \
     {                                                  \
+      if (low == 0 || high == 0)                       \
+       FATAL_ERROR (_("fields and positions are numbered from 1")); \
       if (n_rp >= n_rp_allocated)                      \
        {                                               \
          (rp) = X2NREALLOC (rp, &n_rp_allocated);      \
index 3db4c9baeea30190bfa86832baa200f4d6edc52c..803fbc143b503ae61f4f9caa159ec1cc8456c09a 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 # Test "cut".                                                   -*- perl -*-
 
-# Copyright (C) 2006 Free Software Foundation, Inc.
+# Copyright (C) 2006, 2007 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
@@ -27,7 +27,7 @@ $PERL -e 1 > /dev/null 2>&1 || {
   exit 77
 }
 
-exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
+exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\FILE_EOF
 require 5.003;
 use strict;
 
@@ -36,16 +36,29 @@ use strict;
 # Turn off localisation of executable's ouput.
 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
 
+my $prog = 'cut';
+my $diag = <<EOF;
+$prog: fields and positions are numbered from 1
+Try \`cut --help' for more information.
+EOF
+
 my @Tests =
   (
-  # Provoke a double-free in cut from coreutils-6.7.
-  ['dbl-free', '-f2-', {IN=>{f=>'x'}}, {IN=>{g=>'y'}}, {OUT=>"x\ny\n"}],
+   # Provoke a double-free in cut from coreutils-6.7.
+   ['dbl-free', '-f2-', {IN=>{f=>'x'}}, {IN=>{g=>'y'}}, {OUT=>"x\ny\n"}],
+
+   # This failed (as it should) even before coreutils-6.10,
+   # but cut from 6.10 produces a more useful diagnostic.
+   ['zero-1', '-b0',   {ERR=>$diag}, {EXIT => 1} ],
+
+   # Before coreutils-6.10, specifying a range of 0-2 was not an error.
+   # It was treated just like "-2".
+   ['zero-2', '-f0-2', {ERR=>$diag}, {EXIT => 1} ],
   );
 
 my $save_temps = $ENV{DEBUG};
 my $verbose = $ENV{VERBOSE};
 
-my $prog = 'cut';
 my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
 exit $fail;
-EOF
+FILE_EOF