From b9c364f02cb7a7485cd063fa2cbcbf82042fa8cc Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 29 Aug 2016 09:27:50 -0700 Subject: [PATCH 1/2] intprops.h: fix missing-backslash problems * lib/intprops.h (_GL_ADD_OVERFLOW): Add backslash. (_GL_SUBTRACT_OVERFLOW,_GL_MULTIPLY_OVERFLOW): Likewise. --- ChangeLog | 6 ++++++ lib/intprops.h | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4a2e641..c5e6ee8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2016-08-29 Jim Meyering + + intprops.h: fix missing-backslash problems + * lib/intprops.h (_GL_ADD_OVERFLOW): Add backslash. + (_GL_SUBTRACT_OVERFLOW,_GL_MULTIPLY_OVERFLOW): Likewise. + 2016-08-24 Paul Eggert intprops: fix paren typo on old platforms diff --git a/lib/intprops.h b/lib/intprops.h index 6030760..0c45550 100644 --- a/lib/intprops.h +++ b/lib/intprops.h @@ -236,11 +236,11 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX); (e.g., A and B) have the same type as MIN and MAX. Instead, they assume that the result (e.g., A + B) has that type. */ #if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL -# define _GL_ADD_OVERFLOW(a, b, min, max) +# define _GL_ADD_OVERFLOW(a, b, min, max) \ __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0) -# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) +# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ __builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0) -# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) +# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ __builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0) #else # define _GL_ADD_OVERFLOW(a, b, min, max) \ -- 2.7.4 From 175b4e22f99e00996b72f822f5ae54dca8243d19 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 29 Aug 2016 09:45:18 -0700 Subject: [PATCH 2/2] intprops.h, xalloc-oversized.h: work with gcc 7 In gcc 6, __builtin_add_overflow, __builtin_sub_overflow and __builtin_mul_overflow each accept a NULL pointer as the third argument. However in gcc 7, that is no longer accepted. Instead, one must use the "_p"-suffixed names, with which, the third parameter is no longer a pointer. * lib/intprops.h (_GL_HAS_BUILTIN_OVERFLOW_WITH_NULL): Correct the definition: not true for gcc 7 and subsequent. (_GL_HAS_BUILTIN_OVERFLOW_P): Define. (_GL_ADD_OVERFLOW, _GL_SUBTRACT_OVERFLOW, _GL_MULTIPLY_OVERFLOW): Provide new definitions for gcc 7 and subsequent. * lib/xalloc-oversized.h (xalloc_oversized): Provide a definition that works with gcc-7. --- ChangeLog | 14 ++++++++++++++ lib/intprops.h | 14 ++++++++++++-- lib/xalloc-oversized.h | 7 +++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index c5e6ee8..8e7ab74 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ 2016-08-29 Jim Meyering + intprops.h, xalloc-oversized.h: work with gcc 7 + In gcc 6, __builtin_add_overflow, __builtin_sub_overflow and + __builtin_mul_overflow each accept a NULL pointer as the third + argument. However in gcc 7, that is no longer accepted. + Instead, one must use the "_p"-suffixed names, with which, the + third parameter is no longer a pointer. + * lib/intprops.h (_GL_HAS_BUILTIN_OVERFLOW_WITH_NULL): Correct + the definition: not true for gcc 7 and subsequent. + (_GL_HAS_BUILTIN_OVERFLOW_P): Define. + (_GL_ADD_OVERFLOW, _GL_SUBTRACT_OVERFLOW, _GL_MULTIPLY_OVERFLOW): + Provide new definitions for gcc 7 and subsequent. + * lib/xalloc-oversized.h (xalloc_oversized): Provide a definition + that works with gcc-7. + intprops.h: fix missing-backslash problems * lib/intprops.h (_GL_ADD_OVERFLOW): Add backslash. (_GL_SUBTRACT_OVERFLOW,_GL_MULTIPLY_OVERFLOW): Likewise. diff --git a/lib/intprops.h b/lib/intprops.h index 0c45550..d2a65cc 100644 --- a/lib/intprops.h +++ b/lib/intprops.h @@ -229,13 +229,23 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX); : (max) >> (b) < (a)) /* True if __builtin_add_overflow (A, B, P) works when P is null. */ -#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__) +#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (6 == __GNUC__) + +/* True if __builtin_add_overflow_p (A, B, C) works. */ +#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__) /* The _GL*_OVERFLOW macros have the same restrictions as the *_RANGE_OVERFLOW macros, except that they do not assume that operands (e.g., A and B) have the same type as MIN and MAX. Instead, they assume that the result (e.g., A + B) has that type. */ -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL +#if _GL_HAS_BUILTIN_OVERFLOW_P +# define _GL_ADD_OVERFLOW(a, b, min, max) \ + __builtin_add_overflow_p (a, b, (a) + (b)) +# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ + __builtin_sub_overflow_p (a, b, (a) - (b)) +# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ + __builtin_mul_overflow_p (a, b, (a) * (b)) +#elif _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL # define _GL_ADD_OVERFLOW(a, b, min, max) \ __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0) # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ diff --git a/lib/xalloc-oversized.h b/lib/xalloc-oversized.h index 44f1644..9954f50 100644 --- a/lib/xalloc-oversized.h +++ b/lib/xalloc-oversized.h @@ -43,9 +43,12 @@ nonnegative. This is a macro, not a function, so that it works correctly even when SIZE_MAX < N. */ -/* GCC 7 __builtin_mul_overflow should easily compute this. See: - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68120 */ #if 7 <= __GNUC__ +# define xalloc_oversized(n, s) __builtin_mul_overflow_p (n, s, (size_t) 1) + +/* GCC 6 __builtin_mul_overflow should easily compute this. See: + https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68120 */ +#elif 6 == __GNUC__ # define xalloc_oversized(n, s) __builtin_mul_overflow (n, s, (size_t *) NULL) /* GCC 5 and Clang __builtin_mul_overflow needs a temporary, and -- 2.7.4