]> Savannah Git Hosting - gnulib.git/commitdiff
nstrftime: speed up integer overflow checking
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 23 Oct 2019 20:33:37 +0000 (13:33 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 23 Oct 2019 20:33:57 +0000 (13:33 -0700)
* lib/nstrftime.c: Include intprops.h.
(INT_STRLEN_BOUND): Remove, as we can use intprops.h’s defn.
(__strftime_internal): Use INT_MULTIPLY_WRAPV and INT_ADD_WRAPV
instead of doing it by hand.
* modules/nstrftime (Depends-on): Add intprops.

ChangeLog
lib/nstrftime.c
modules/nstrftime

index 103befc64a3c997b34e0481c4d9d67ecb88caac4..804294150cec42a8bc19c7751aa76d786eab5768 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2019-10-23  Paul Eggert  <eggert@cs.ucla.edu>
 
+       nstrftime: speed up integer overflow checking
+       * lib/nstrftime.c: Include intprops.h.
+       (INT_STRLEN_BOUND): Remove, as we can use intprops.h’s defn.
+       (__strftime_internal): Use INT_MULTIPLY_WRAPV and INT_ADD_WRAPV
+       instead of doing it by hand.
+       * modules/nstrftime (Depends-on): Add intprops.
+
        Port better to GCC under macOS
        Work around macOS header that has ‘#define __has_builtin(x) 0’
        when compiled by GCC.  Apple really, really doesn’t want you to
index 01db02bfe26f5f1ad946290ed67df834b434362f..11ad00b10db83334ca53ea36a4c33d4fa2d5c93e 100644 (file)
@@ -68,6 +68,8 @@ extern char *tzname[];
 #include <string.h>
 #include <stdbool.h>
 
+#include <intprops.h>
+
 #ifndef FALLTHROUGH
 # if __GNUC__ < 7
 #  define FALLTHROUGH ((void) 0)
@@ -113,13 +115,6 @@ extern char *tzname[];
    ? (a) >> (b)         \
    : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
 
-/* Bound on length of the string representing an integer type or expression T.
-   Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
-   add 1 for integer division truncation; add 1 more for a minus sign
-   if needed.  */
-#define INT_STRLEN_BOUND(t) \
-  ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
-
 #define TM_YEAR_BASE 1900
 
 #ifndef __isleap
@@ -704,15 +699,9 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
           width = 0;
           do
             {
-              if (width > INT_MAX / 10
-                  || (width == INT_MAX / 10 && *f - L_('0') > INT_MAX % 10))
-                /* Avoid overflow.  */
+              if (INT_MULTIPLY_WRAPV (width, 10, &width)
+                  || INT_ADD_WRAPV (width, *f - L_('0'), &width))
                 width = INT_MAX;
-              else
-                {
-                  width *= 10;
-                  width += *f - L_('0');
-                }
               ++f;
             }
           while (ISDIGIT (*f));
index b559b5e2057da17e7d49df820f34607e6c2da888..a1ce1b33f647ce53f47ba6aef793430b96bff24d 100644 (file)
@@ -9,6 +9,7 @@ m4/nstrftime.m4
 
 Depends-on:
 extensions
+intprops
 stdbool
 time_rz