Remove some arbitrary restrictions on size fields,
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 13 Dec 2006 21:03:54 +0000 (22:03 +0100)
committerJim Meyering <jim@meyering.net>
Wed, 13 Dec 2006 21:03:54 +0000 (22:03 +0100)
so that commands like "sort -k 18446744073709551616" no longer fail merely
because 18446744073709551616 doesn't fit in uintmax_t.  The trick is that
these fields can all be treated as effectively infinity;  their exact
values don't matter, since no internal buffer can be that long.
* src/join.c (string_to_join_field): Verify that SIZE_MAX <= ULONG_MAX
if the code assumes this.  Silently truncate too-large values to SIZE_MAX,
as the remaining code will do the right thing in this case.
* src/sort.c (parse_field_count): Likewise.
* src/uniq.c (size_opt, main): Likewise.
* tests/join/Test.pm (bigfield): New test.
* tests/sort/Test.pm (bigfield): New test.
* tests/uniq/Test.pm (121): New test.
Signed-off-by: Jim Meyering <jim@meyering.net>
ChangeLog
src/join.c
src/sort.c
src/uniq.c
tests/join/Makefile.am
tests/join/Test.pm
tests/sort/Makefile.am
tests/sort/Test.pm
tests/uniq/Makefile.am
tests/uniq/Test.pm

index 22db000..3f227d2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2006-12-13  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Remove some arbitrary restrictions on size fields, so that
+       commands like "sort -k 18446744073709551616" no longer fail merely
+       because 18446744073709551616 doesn't fit in uintmax_t.  The trick
+       is that these fields can all be treated as effectively infinity;
+       their exact values don't matter, since no internal buffer can be
+       that long.
+       * src/join.c (string_to_join_field): Verify that SIZE_MAX <=
+       ULONG_MAX if the code assumes this.  Silently truncate too-large
+       values to SIZE_MAX, as the remaining code will do the right thing
+       in this case.
+       * src/sort.c (parse_field_count): Likewise.
+       * src/uniq.c (size_opt, main): Likewise.
+       * tests/join/Test.pm (bigfield): New test.
+       * tests/sort/Test.pm (bigfield): New test.
+       * tests/uniq/Test.pm (121): New test.
+
 2006-12-13  Jim Meyering  <jim@meyering.net>
 
        * tests/chgrp/default-no-deref: New test.
index 77a3896..b113c54 100644 (file)
@@ -599,7 +599,8 @@ add_field (int file, size_t field)
 
 /* Convert a string of decimal digits, STR (the 1-based join field number),
    to an integral value.  Upon successful conversion, return one less
-   (the zero-based field number).  If it cannot be converted, give a
+   (the zero-based field number).  Silently convert too-large values
+   to SIZE_MAX - 1.  Otherwise, if a value cannot be converted, give a
    diagnostic and exit.  */
 
 static size_t
@@ -607,16 +608,12 @@ string_to_join_field (char const *str)
 {
   size_t result;
   unsigned long int val;
+  verify (SIZE_MAX <= ULONG_MAX);
 
   strtol_error s_err = xstrtoul (str, NULL, 10, &val, "");
   if (s_err == LONGINT_OVERFLOW || (s_err == LONGINT_OK && SIZE_MAX < val))
-    {
-      error (EXIT_FAILURE, 0,
-            _("value %s is so large that it is not representable"),
-            quote (str));
-    }
-
-  if (s_err != LONGINT_OK || val == 0)
+    val = SIZE_MAX;
+  else if (s_err != LONGINT_OK || val == 0)
     error (EXIT_FAILURE, 0, _("invalid field number: %s"), quote (str));
 
   result = val - 1;
index feaf5a5..f03237c 100644 (file)
@@ -2170,7 +2170,8 @@ check_ordering_compatibility (void)
 
 /* Parse the leading integer in STRING and store the resulting value
    (which must fit into size_t) into *VAL.  Return the address of the
-   suffix after the integer.  If MSGID is NULL, return NULL after
+   suffix after the integer.  If the value is too large, silently
+   substitute SIZE_MAX.  If MSGID is NULL, return NULL after
    failure; otherwise, report MSGID and exit on failure.  */
 
 static char const *
@@ -2189,10 +2190,8 @@ parse_field_count (char const *string, size_t *val, char const *msgid)
       /* Fall through.  */
     case LONGINT_OVERFLOW:
     case LONGINT_OVERFLOW | LONGINT_INVALID_SUFFIX_CHAR:
-      if (msgid)
-       error (SORT_FAILURE, 0, _("%s: count `%.*s' too large"),
-              _(msgid), (int) (suffix - string), string);
-      return NULL;
+      *val = SIZE_MAX;
+      break;
 
     case LONGINT_INVALID:
       if (msgid)
index 0d60961..6c38ed8 100644 (file)
@@ -172,17 +172,26 @@ Fields are skipped before chars.\n\
   exit (status);
 }
 
-/* Convert OPT to size_t, reporting an error using MSGID if it does
-   not fit.  */
+/* Convert OPT to size_t, reporting an error using MSGID if OPT is
+   invalid.  Silently convert too-large values to SIZE_MAX.  */
 
 static size_t
 size_opt (char const *opt, char const *msgid)
 {
   unsigned long int size;
-  if (xstrtoul (opt, NULL, 10, &size, "") != LONGINT_OK
-      || SIZE_MAX < size)
-    error (EXIT_FAILURE, 0, "%s: %s", opt, _(msgid));
-  return size;
+  verify (SIZE_MAX <= ULONG_MAX);
+
+  switch (xstrtoul (opt, NULL, 10, &size, ""))
+    {
+    case LONGINT_OK:
+    case LONGINT_OVERFLOW:
+      break;
+
+    default:
+      error (EXIT_FAILURE, 0, "%s: %s", opt, _(msgid));
+    }
+
+  return MIN (size, SIZE_MAX);
 }
 
 /* Given a linebuffer LINE,
@@ -472,8 +481,8 @@ main (int argc, char **argv)
              skip_fields = 0;
 
            if (!DECIMAL_DIGIT_ACCUMULATE (skip_fields, optc - '0', size_t))
-             error (EXIT_FAILURE, 0, "%s",
-                    _("invalid number of fields to skip"));
+             skip_fields = SIZE_MAX;
+
            skip_field_option_type = SFO_OBSOLETE;
          }
          break;
index 54ce115..151d2fe 100644 (file)
@@ -30,14 +30,15 @@ maint_gen = 1a.I1 1a.I2 1a.X 1b.I1 1b.I2 1b.X 1c.I1 1c.I2 1c.X 1d.I1 1d.I2 \
 5k.I2 5k.X 5l.I1 5l.I2 5l.X 5m.I1 5m.I2 5m.X 6a.I1 6a.I2 6a.X 6b.I1 6b.I2 \
 6b.X 6c.I1 6c.I2 6c.X 7a.I1 7a.I2 7a.X 8a.I1 8a.I2 8a.X 8b.I1 8b.I2 8b.X \
 9a.I1 9a.I2 9a.X trailing-sp.I1 trailing-sp.I2 trailing-sp.X sp-vs-blank.I1 \
-sp-vs-blank.I2 sp-vs-blank.X 8-bit-t.I1 8-bit-t.I2 8-bit-t.X invalid-j.X
+sp-vs-blank.I2 sp-vs-blank.X 8-bit-t.I1 8-bit-t.I2 8-bit-t.X bigfield.I1 \
+bigfield.I2 bigfield.X invalid-j.X
 run_gen = 1a.O 1a.E 1b.O 1b.E 1c.O 1c.E 1d.O 1d.E 1e.O 1e.E 1f.O 1f.E 2a.O \
 2a.E 2b.O 2b.E 2c.O 2c.E 3a.O 3a.E 4a.O 4a.E 4b.O 4b.E 4c.O 4c.E 4d.O 4d.E \
 4e.O 4e.E 5a.O 5a.E 5b.O 5b.E 5c.O 5c.E 5d.O 5d.E 5e.O 5e.E 5f.O 5f.E 5g.O \
 5g.E 5h.O 5h.E 5i.O 5i.E 5j.O 5j.E 5k.O 5k.E 5l.O 5l.E 5m.O 5m.E 6a.O 6a.E \
 6b.O 6b.E 6c.O 6c.E 7a.O 7a.E 8a.O 8a.E 8b.O 8b.E 9a.O 9a.E trailing-sp.O \
-trailing-sp.E sp-vs-blank.O sp-vs-blank.E 8-bit-t.O 8-bit-t.E invalid-j.O \
-invalid-j.E
+trailing-sp.E sp-vs-blank.O sp-vs-blank.E 8-bit-t.O 8-bit-t.E bigfield.O \
+bigfield.E invalid-j.O invalid-j.E
 ##test-files-end
 
 EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen)
index 37b1449..3d60ce3 100644 (file)
@@ -136,6 +136,9 @@ my @tv = (
  [t_subst "a:1\nb:1\n", t_subst "a:2:\nb:2:\n"],
  t_subst "a:1:2:\nb:1:2:\n", 0],
 
+['bigfield', '-1 340282366920938463463374607431768211456 -2 2',
+ ["a\n", "b\n"], " a b\n", 0],
+
 # FIXME: change this to ensure the diagnostic makes sense
 ['invalid-j', '-j x', {}, "", 1],
 
index 5bb2fa7..ead0972 100644 (file)
@@ -40,7 +40,7 @@ n10b.X n11a.I n11a.X n11b.I n11b.X 01a.I 01a.X 02a.I 02a.X 02b.I 02b.X 02c.I \
 21g.I 21g.X 22a.I 22a.X 22b.I 22b.X no-file1.X o-no-file1.X create-empty.X \
 neg-nls.I neg-nls.X nul-nls.I nul-nls.X use-nl.I use-nl.X o2.I o2.X \
 incompat1.I incompat1.X incompat2.I incompat2.X incompat3.I incompat3.X \
-incompat4.I incompat4.X nul-tab.I nul-tab.X
+incompat4.I incompat4.X nul-tab.I nul-tab.X bigfield.I bigfield.X
 run_gen = n1.O n1.E n2.O n2.E n3.O n3.E n4.O n4.E n5.O n5.E n6.O n6.E n7.O \
 n7.E n8a.O n8a.E n8b.O n8b.E n9a.O n9a.E n9b.O n9b.E n10a.O n10a.E n10b.O \
 n10b.E n11a.O n11a.E n11b.O n11b.E 01a.O 01a.E 02a.O 02a.E 02b.O 02b.E 02c.O \
@@ -61,7 +61,7 @@ n10b.E n11a.O n11a.E n11b.O n11b.E 01a.O 01a.E 02a.O 02a.E 02b.O 02b.E 02c.O \
 o-no-file1.E create-empty.O create-empty.E neg-nls.O neg-nls.E nul-nls.O \
 nul-nls.E use-nl.O use-nl.E o2.O o2.E incompat1.O incompat1.E incompat2.O \
 incompat2.E incompat3.O incompat3.E incompat4.O incompat4.E nul-tab.O \
-nul-tab.E
+nul-tab.E bigfield.O bigfield.E
 ##test-files-end
 
 EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen)
index fbc6c49..6bed61b 100755 (executable)
@@ -275,6 +275,9 @@ my @tv = (
 
 # -t '\0' is accepted, as of coreutils-5.0.91
 ['nul-tab', "-k2,2 -t '\\0'", "a\0z\01\nb\0y\02\n", "b\0y\02\na\0z\01\n", 0],
+
+["bigfield", '-k 340282366920938463463374607431768211456',
+ "2\n1\n", "1\n2\n", 0],
 );
 
 sub test_vector
index b3bb3d1..9654332 100644 (file)
@@ -30,7 +30,7 @@ obs-plus44.I obs-plus44.X obs-plus45.I obs-plus45.X 50.I 50.X 51.I 51.X 52.I \
 62.I 62.X 63.I 63.X 64.I 64.X 65.I 65.X 90.I 90.X 91.I 91.X 92.I 92.X 93.I \
 93.X 94.I 94.X 101.I 101.X 102.I 102.X 110.I 110.X 111.I 111.X 112.I 112.X \
 113.I 113.X 114.I 114.X 115.I 115.X 116.I 116.X 117.I 117.X 118.I 118.X 119.I \
-119.X 120.I 120.X
+119.X 120.I 120.X 121.I 121.X
 run_gen = 1.O 1.E 2.O 2.E 3.O 3.E 4.O 4.E 5.O 5.E 6.O 6.E 7.O 7.E 8.O 8.E 9.O \
 9.E 10.O 10.E 11.O 11.E 12.O 12.E 13.O 13.E 20.O 20.E 21.O 21.E 22.O 22.E \
 23.O 23.E obs30.O obs30.E 31.O 31.E 32.O 32.E 33.O 33.E 34.O 34.E 35.O 35.E \
@@ -40,7 +40,7 @@ obs-plus44.O obs-plus44.E obs-plus45.O obs-plus45.E 50.O 50.E 51.O 51.E 52.O \
 62.O 62.E 63.O 63.E 64.O 64.E 65.O 65.E 90.O 90.E 91.O 91.E 92.O 92.E 93.O \
 93.E 94.O 94.E 101.O 101.E 102.O 102.E 110.O 110.E 111.O 111.E 112.O 112.E \
 113.O 113.E 114.O 114.E 115.O 115.E 116.O 116.E 117.O 117.E 118.O 118.E 119.O \
-119.E 120.O 120.E
+119.E 120.O 120.E 121.O 121.E
 ##test-files-end
 
 EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen)
index fe0fc68..1eda7e2 100644 (file)
@@ -105,6 +105,8 @@ my @tv = (
 ['119', '--all-repeated=badoption', "a\n",           "",                 1],
 # Check that -d and -u suppress all output, as POSIX requires.
 ['120', '-d -u', "a\na\n\b",        "",                         0],
+['121', '-d -u -w340282366920938463463374607431768211456',
+                "a\na\n\b",        "",                         0],
 );
 
 sub test_vector