cut: do not accept the invalid range 0-
authorBernhard Voelker <mail@bernhard-voelker.de>
Sun, 18 Nov 2012 21:20:16 +0000 (22:20 +0100)
committerBernhard Voelker <mail@bernhard-voelker.de>
Sun, 18 Nov 2012 23:08:38 +0000 (00:08 +0100)
The command "echo 12345 | cut -b 0-" prints an empty line while
it should fail with "fields and positions are numbered from 1".

* src/cut.c (set_fields): Add a diagnostic for the invalid open
range which starts with Zero, i.e., the range 0-.
* tests/misc/cut.pl: Add tests to ensure the range 0- fails for
fields (-f) and for positions (-b, -c).
* NEWS: Mention the fix.

Reported by Marcel Böhme in <http://bugs.gnu.org/12903>.

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

diff --git a/NEWS b/NEWS
index db6a207..e4a284c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,10 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Bug fixes
 
+  cut no longer accepts the invalid range 0-, which made it print empty lines.
+  Instead, cut now fails and emits an appropriate diagnostic.
+  [This bug was present in "the beginning".]
+
   pr -n no longer crashes when passed values >= 32.  Also line numbers are
   consistently padded with spaces, rather than with zeros for certain widths.
   [bug introduced in TEXTUTILS-1_22i]
index 87380ac..2a57148 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -369,6 +369,9 @@ set_fields (const char *fieldstr)
           dash_found = true;
           fieldstr++;
 
+          if (lhs_specified && !value)
+            FATAL_ERROR (_("fields and positions are numbered from 1"));
+
           initial = (lhs_specified ? value : 1);
           value = 0;
         }
index 0ce051a..cd56555 100755 (executable)
@@ -46,6 +46,11 @@ my @Tests =
   # It was treated just like "-2".
   ['zero-2', '-f0-2', {ERR=>$from_1}, {EXIT => 1} ],
 
+  # Up to coreutils-8.20, specifying a range of 0- was not an error.
+  ['zero-3b', '-b0-', {ERR=>$from_1}, {EXIT => 1} ],
+  ['zero-3c', '-c0-', {ERR=>$from_1}, {EXIT => 1} ],
+  ['zero-3f', '-f0-', {ERR=>$from_1}, {EXIT => 1} ],
+
   ['1', '-d:', '-f1,3-', {IN=>"a:b:c\n"}, {OUT=>"a:c\n"}],
   ['2', '-d:', '-f1,3-', {IN=>"a:b:c\n"}, {OUT=>"a:c\n"}],
   ['3', qw(-d: -f2-), {IN=>"a:b:c\n"}, {OUT=>"b:c\n"}],