summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2007-05-22 14:25:19 +0200
committerJim Meyering <jim@meyering.net>2007-05-22 18:47:17 +0200
commita6a447fc58a01598682f0914f978d0a3c1cfc4dc (patch)
tree381e6cc2fecc1d4244daf27a8d2a67eccaf7c0df
parentae5717158f1b9f31b986b0f4416582684039ec55 (diff)
downloadcoreutils-a6a447fc58a01598682f0914f978d0a3c1cfc4dc.tar.xz
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 for 0. Based on a patch from James Youngman. * tests/misc/cut: Add tests for the above.
-rw-r--r--ChangeLog8
-rw-r--r--NEWS3
-rw-r--r--src/cut.c2
-rwxr-xr-xtests/misc/cut25
4 files changed, 32 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index c1fe87205..464266770 100644
--- 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 03266813d..ea08e0a92 100644
--- 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
diff --git a/src/cut.c b/src/cut.c
index ab14abcb2..e89460e1b 100644
--- 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); \
diff --git a/tests/misc/cut b/tests/misc/cut
index 3db4c9bae..803fbc143 100755
--- a/tests/misc/cut
+++ b/tests/misc/cut
@@ -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