[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
'head -n 100k@' doesn't report an error
From: |
Paul Eggert |
Subject: |
'head -n 100k@' doesn't report an error |
Date: |
14 Oct 2003 13:27:02 -0700 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 |
I noticed that some combinations of suffixes aren't properly diagnosed
in coreutils, e.g., 'head -n 100k@' should print an error message but
doesn't. Here's a patch.
2003-10-14 Paul Eggert <address@hidden>
Fix some number-parsing bugs, e.g., "head -n 100k@" wasn't
properly diagnosted.
* lib/human.c, lib/xstrtoimax.c, lib/xstrtol.c, lib/xstrtol.h,
lib/xstrtoul.c, lib/xstrtoumax.c: Sync with gnulib.
* src/sort.c (parse_field_count): Hande the case where overflow
and invalid suffix char are both reported.
Index: lib/human.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/lib/human.c,v
retrieving revision 1.23
diff -p -u -r1.23 human.c
--- lib/human.c 10 Sep 2003 08:49:12 -0000 1.23
+++ lib/human.c 14 Oct 2003 20:14:44 -0000
@@ -450,8 +450,6 @@ humblock (char const *spec, uintmax_t *b
"eEgGkKmMpPtTyYzZ0");
if (e != LONGINT_OK)
return e;
- if (*ptr)
- return LONGINT_INVALID_SUFFIX_CHAR;
for (; ! ('0' <= *spec && *spec <= '9'); spec++)
if (spec == ptr)
{
Index: lib/xstrtoimax.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/lib/xstrtoimax.c,v
retrieving revision 1.3
diff -p -u -r1.3 xstrtoimax.c
--- lib/xstrtoimax.c 18 Aug 2003 09:44:49 -0000 1.3
+++ lib/xstrtoimax.c 14 Oct 2003 20:14:44 -0000
@@ -30,4 +30,8 @@
#define __strtol strtoimax
#define __strtol_t intmax_t
#define __xstrtol xstrtoimax
+#ifdef INTMAX_MAX
+# define STRTOL_T_MINIMUM INTMAX_MIN
+# define STRTOL_T_MAXIMUM INTMAX_MAX
+#endif
#include "xstrtol.c"
Index: lib/xstrtol.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/lib/xstrtol.c,v
retrieving revision 1.32
diff -p -u -r1.32 xstrtol.c
--- lib/xstrtol.c 13 Sep 2003 19:53:18 -0000 1.32
+++ lib/xstrtol.c 14 Oct 2003 20:14:44 -0000
@@ -27,6 +27,8 @@
# define __strtol strtol
# define __strtol_t long int
# define __xstrtol xstrtol
+# define STRTOL_T_MINIMUM LONG_MIN
+# define STRTOL_T_MAXIMUM LONG_MAX
#endif
/* Some pre-ANSI implementations (e.g. SunOS 4)
@@ -47,6 +49,15 @@ extern int errno;
/* The extra casts work around common compiler bugs. */
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
+#define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
+ ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
+ : (t) 0))
+#define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
+
+#ifndef STRTOL_T_MINIMUM
+# define STRTOL_T_MINIMUM TYPE_MINIMUM (__strtol_t)
+# define STRTOL_T_MAXIMUM TYPE_MAXIMUM (__strtol_t)
+#endif
#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
# define IN_CTYPE_DOMAIN(c) 1
@@ -66,24 +77,30 @@ intmax_t strtoimax ();
uintmax_t strtoumax ();
#endif
-static int
+static strtol_error
bkm_scale (__strtol_t *x, int scale_factor)
{
- __strtol_t product = *x * scale_factor;
- if (*x != product / scale_factor)
- return 1;
- *x = product;
- return 0;
+ if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
+ {
+ *x = STRTOL_T_MINIMUM;
+ return LONGINT_OVERFLOW;
+ }
+ if (STRTOL_T_MAXIMUM / scale_factor < *x)
+ {
+ *x = STRTOL_T_MAXIMUM;
+ return LONGINT_OVERFLOW;
+ }
+ *x *= scale_factor;
+ return LONGINT_OK;
}
-static int
+static strtol_error
bkm_scale_by_power (__strtol_t *x, int base, int power)
{
+ strtol_error err = LONGINT_OK;
while (power--)
- if (bkm_scale (x, base))
- return 1;
-
- return 0;
+ err |= bkm_scale (x, base);
+ return err;
}
/* FIXME: comment. */
@@ -95,6 +112,7 @@ __xstrtol (const char *s, char **ptr, in
char *t_ptr;
char **p;
__strtol_t tmp;
+ strtol_error err = LONGINT_OK;
assert (0 <= strtol_base && strtol_base <= 36);
@@ -111,8 +129,6 @@ __xstrtol (const char *s, char **ptr, in
errno = 0;
tmp = __strtol (s, p, strtol_base);
- if (errno != 0)
- return LONGINT_OVERFLOW;
if (*p == s)
{
@@ -123,6 +139,12 @@ __xstrtol (const char *s, char **ptr, in
else
return LONGINT_INVALID;
}
+ else if (errno != 0)
+ {
+ if (errno != ERANGE)
+ return LONGINT_INVALID;
+ err = LONGINT_OVERFLOW;
+ }
/* Let valid_suffixes == NULL mean `allow any suffix'. */
/* FIXME: update all callers except the ones that allow suffixes
@@ -130,19 +152,19 @@ __xstrtol (const char *s, char **ptr, in
if (!valid_suffixes)
{
*val = tmp;
- return LONGINT_OK;
+ return err;
}
if (**p != '\0')
{
int base = 1024;
int suffixes = 1;
- int overflow;
+ strtol_error overflow;
if (!strchr (valid_suffixes, **p))
{
*val = tmp;
- return LONGINT_INVALID_SUFFIX_CHAR;
+ return err | LONGINT_INVALID_SUFFIX_CHAR;
}
if (strchr (valid_suffixes, '0'))
@@ -225,18 +247,18 @@ __xstrtol (const char *s, char **ptr, in
default:
*val = tmp;
- return LONGINT_INVALID_SUFFIX_CHAR;
+ return err | LONGINT_INVALID_SUFFIX_CHAR;
break;
}
- if (overflow)
- return LONGINT_OVERFLOW;
-
- (*p) += suffixes;
+ err |= overflow;
+ *p += suffixes;
+ if (**p)
+ err |= LONGINT_INVALID_SUFFIX_CHAR;
}
*val = tmp;
- return LONGINT_OK;
+ return err;
}
#ifdef TESTING_XSTRTO
Index: lib/xstrtol.h
===================================================================
RCS file: /cvsroot/coreutils/coreutils/lib/xstrtol.h,v
retrieving revision 1.16
diff -p -u -r1.16 xstrtol.h
--- lib/xstrtol.h 18 Jun 2003 06:11:45 -0000 1.16
+++ lib/xstrtol.h 14 Oct 2003 20:14:44 -0000
@@ -32,7 +32,16 @@
# ifndef _STRTOL_ERROR
enum strtol_error
{
- LONGINT_OK, LONGINT_INVALID, LONGINT_INVALID_SUFFIX_CHAR, LONGINT_OVERFLOW
+ LONGINT_OK = 0,
+
+ /* These two values can be ORed together, to indicate that both
+ errors occurred. */
+ LONGINT_OVERFLOW = 1,
+ LONGINT_INVALID_SUFFIX_CHAR = 2,
+
+ LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW = (LONGINT_INVALID_SUFFIX_CHAR
+ | LONGINT_OVERFLOW),
+ LONGINT_INVALID = 4
};
typedef enum strtol_error strtol_error;
# endif
@@ -49,7 +58,7 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
{ \
switch ((Err)) \
{ \
- case LONGINT_OK: \
+ default: \
abort (); \
\
case LONGINT_INVALID: \
@@ -58,6 +67,7 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
break; \
\
case LONGINT_INVALID_SUFFIX_CHAR: \
+ case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW: \
error ((Exit_code), 0, "invalid character following %s in `%s'", \
(Argument_type_string), (Str)); \
break; \
Index: lib/xstrtoul.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/lib/xstrtoul.c,v
retrieving revision 1.2
diff -p -u -r1.2 xstrtoul.c
--- lib/xstrtoul.c 18 Apr 1999 23:15:02 -0000 1.2
+++ lib/xstrtoul.c 14 Oct 2003 20:14:44 -0000
@@ -1,4 +1,6 @@
#define __strtol strtoul
#define __strtol_t unsigned long int
#define __xstrtol xstrtoul
+#define STRTOL_T_MINIMUM 0
+#define STRTOL_T_MAXIMUM ULONG_MAX
#include "xstrtol.c"
Index: lib/xstrtoumax.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/lib/xstrtoumax.c,v
retrieving revision 1.7
diff -p -u -r1.7 xstrtoumax.c
--- lib/xstrtoumax.c 18 Aug 2003 09:44:49 -0000 1.7
+++ lib/xstrtoumax.c 14 Oct 2003 20:14:44 -0000
@@ -30,4 +30,8 @@
#define __strtol strtoumax
#define __strtol_t uintmax_t
#define __xstrtol xstrtoumax
+#ifdef UINTMAX_MAX
+# define STRTOL_T_MINIMUM 0
+# define STRTOL_T_MAXIMUM UINTMAX_MAX
+#endif
#include "xstrtol.c"
Index: src/sort.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/src/sort.c,v
retrieving revision 1.274
diff -p -u -r1.274 sort.c
--- src/sort.c 28 Sep 2003 08:21:14 -0000 1.274
+++ src/sort.c 14 Oct 2003 20:18:58 -0000
@@ -2112,6 +2112,7 @@ parse_field_count (char const *string, s
break;
/* 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);
- 'head -n 100k@' doesn't report an error,
Paul Eggert <=