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
#include <string.h>
#include <stdbool.h>
+#include <intprops.h>
+
#ifndef FALLTHROUGH
# if __GNUC__ < 7
# define FALLTHROUGH ((void) 0)
? (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
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));