]> Savannah Git Hosting - gnulib.git/commitdiff
xstrtol: simplify integer overflow checking
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 17 Jan 2022 06:45:47 +0000 (22:45 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 17 Jan 2022 06:47:07 +0000 (22:47 -0800)
* lib/xstrtol.c: Include intprops.h.
(TYPE_SIGNED): Remove, as intprops.h defines that for us now.
(bkm_scale): Use INT_MULTIPLY_WRAPV instead of checking for
overflow by hand.

ChangeLog
lib/xstrtol.c

index a1896fa7d3deb89d92e100bf02de4ec80ee219dd..2ea372b0e3ed97b1b272ec9aeb0ab2e9660d41bc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2022-01-16  Paul Eggert  <eggert@cs.ucla.edu>
 
+       xstrtol: simplify integer overflow checking
+       * lib/xstrtol.c: Include intprops.h.
+       (TYPE_SIGNED): Remove, as intprops.h defines that for us now.
+       (bkm_scale): Use INT_MULTIPLY_WRAPV instead of checking for
+       overflow by hand.
+
        xstrtoll-tests: use %lld for long long
        * tests/test-xstrtoll.c, tests/test-xstrtoull.c (__spec):
        Do not assume long long is 64 bits, or that exact-width
index c9776fb80cdfe35461ccd1ff8dd61b4600b11378..6f5a8bef60d0596fbe370e528b4a487606dba514 100644 (file)
 #include <string.h>
 
 #include "assure.h"
-
-#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
+#include "intprops.h"
 
 static strtol_error
 bkm_scale (__strtol_t *x, int scale_factor)
 {
-  if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
+  __strtol_t scaled;
+  if (INT_MULTIPLY_WRAPV (*x, scale_factor, &scaled))
     {
-      *x = STRTOL_T_MINIMUM;
+      *x = *x < 0 ? TYPE_MINIMUM (__strtol_t) : TYPE_MAXIMUM (__strtol_t);
       return LONGINT_OVERFLOW;
     }
-  if (STRTOL_T_MAXIMUM / scale_factor < *x)
-    {
-      *x = STRTOL_T_MAXIMUM;
-      return LONGINT_OVERFLOW;
-    }
-  *x *= scale_factor;
+  else
+    *x = scaled;
+
   return LONGINT_OK;
 }