Handle invalid suffixes and overflow independently, so that
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 14 Oct 2003 20:08:25 +0000 (20:08 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 14 Oct 2003 20:08:25 +0000 (20:08 +0000)
callers can treat them independently as needed.  Fix some bugs in
suffix handling.

lib/ChangeLog
lib/human.c
lib/xstrtoimax.c
lib/xstrtol.c
lib/xstrtol.h
lib/xstrtoul.c
lib/xstrtoumax.c

index c543f10a4c80e0fe4ff9b7db056c988fb4f925e8..fd99ba93b5e814e8dc50d861f78e1bd23bdf8b41 100644 (file)
@@ -1,3 +1,34 @@
+2003-10-14  Paul Eggert  <eggert@twinsun.com>
+
+       Handle invalid suffixes and overflow independently, so that
+       callers can treat them independently as needed.  Fix some bugs in
+       suffix handling, e.g., "100k@" was not diagnosed as an invalid
+       suffix for a human-readable blocksize.  The major caller-visible
+       change is the addition of a new
+       LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW enum value, indicating
+       that both overflow and suffix chars were found.
+
+       * human.c (humblock): Don't check separately for invalid suffix
+       char; that is xstrtoumax's job (now that its bug is fixed).
+       * xstrtoimax.c (STRTOL_T_MINIMUM, STRTOL_T_MAXIMUM) [defined
+       INTMAX_MAX]: New macros.
+       * xstrtol.c (STRTOL_T_MINIMUM, STRTOL_T_MAXIMUM, TYPE_MINIMUM,
+       TYPE_MAXIMUM): New macros.
+       (bkm_scale, bkm_scale_by_power): Return strtol_error, not int.
+       (bkm_scale, bkm_scale_by_power, __xstrtol): Return maximal values
+       if overflow occurs, as it's what __strtol does and it's more useful
+       in practice.
+       (__xstrtol): If __strtol reports some error other than ERANGE,
+       reflect it to the caller as LONGINT_INVALID.  If it reports
+       ERANGE, continue the rest of parsing, and report LONGINT_OVERFLOW
+       | LONGINT_INVALID_SUFFIX_CHAR if both errors occur.
+       * xstrtol.h (LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW): New enum
+       value.
+       (_STRTOL_ERROR): Handle it.  Abort when given unknown error codes.
+       * xstrtoul.c (STRTOL_T_MINIMUM, STRTOL_T_MAXIMUM): New macros.
+       * xstrtoumax.c (STRTOL_T_MINIMUM, STRTOL_T_MAXIMUM)
+       [defined UINTMAX_MAX]: New macros.
+       
 2003-10-14  Bruno Haible  <bruno@clisp.org>
 
        * fatal-signal.h: Improved comments. Suggested by Paul Eggert.
index b342bf178445d2d4f68ad51999ab579be98df988..36cfd7348876047fce07f7d0926eefb82ce278ef 100644 (file)
@@ -450,8 +450,6 @@ humblock (char const *spec, uintmax_t *block_size, int *options)
                                       "eEgGkKmMpPtTyYzZ0");
          if (e != LONGINT_OK)
            return e;
-         if (*ptr)
-           return LONGINT_INVALID_SUFFIX_CHAR;
          for (; ! ('0' <= *spec && *spec <= '9'); spec++)
            if (spec == ptr)
              {
index 94c65778641025587f652b76f5a2262f315cd7ce..2ba136772c861ccd1671e7bef2e5043a444369dd 100644 (file)
@@ -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 4be09f1693f0bb13484bca140878d25c0d4bcab2..32149397c28d9981507362522c3dc0313d308017 100644 (file)
@@ -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, int strtol_base,
   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, int strtol_base,
 
   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, int strtol_base,
       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, int strtol_base,
   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, int strtol_base,
 
        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 9af90aad3533814822662d8694c590b5c803e577..f3b9295f3555e6dacbb78a664f93d8287cb734f4 100644 (file)
 # 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 6140bbee2e441b33b9ea8e2e1ddc5c3531d6b7d6..285f7b96f8500a018eba40a1a8235e9abc230df8 100644 (file)
@@ -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 59d6ecf5cdc1795a81263eee45c751fbd3e7960f..a8858bf6d75e96b843dab2e23f3f9f3060634d09 100644 (file)
@@ -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"