From: Ondřej Surý Date: Fri, 23 Sep 2022 13:35:58 +0000 (+0200) Subject: Check for working __builtin_mul_overflow() implementation X-Git-Tag: v9.19.6~27^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a30e75db86f408fec1c727d6a02c054420e1ee48;p=thirdparty%2Fbind9.git Check for working __builtin_mul_overflow() implementation Instead of using generic HAVE_BUILTIN_OVERFLOW, we need to check whether the overflow functions actually work as there was a bug in GCC that it would not detect mul overflow when compiled with `-m32` option without optimizations and the bug was fixed only for GCC 6.5+ and 7.3+/8+. For further details see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82274 --- diff --git a/configure.ac b/configure.ac index 609f138efe0..fc643e57e64 100644 --- a/configure.ac +++ b/configure.ac @@ -1220,16 +1220,40 @@ AC_LINK_IFELSE( ) # -# Check for __builtin_uadd_overflow +# Check for __builtin_*_overflow # -AC_MSG_CHECKING([compiler support for __builtin_*_overflow()]) +AC_MSG_CHECKING([compiler support for __builtin_add_overflow()]) AC_LINK_IFELSE( [AC_LANG_PROGRAM( [[#include ]], - [[return (__builtin_uadd_overflow(UINT_MAX, UINT_MAX, &(unsigned int){ 0 }));]] + [[return (__builtin_add_overflow((unsigned int)UINT_MAX, (unsigned int)UINT_MAX, &(unsigned int){ 0 }));]] )], [AC_MSG_RESULT([yes]) - AC_DEFINE([HAVE_BUILTIN_OVERFLOW], [1], [define if the compiler supports __builtin_*_overflow().]) + AC_DEFINE([HAVE_BUILTIN_ADD_OVERFLOW], [1], [define if the compiler supports __builtin_add_overflow().]) + ], + [AC_MSG_RESULT([no]) + ]) + +AC_MSG_CHECKING([compiler support for __builtin_sub_overflow()]) +AC_LINK_IFELSE( + [AC_LANG_PROGRAM( + [[#include ]], + [[return (__builtin_sub_overflow((unsigned int)0, (unsigned int)UINT_MAX, &(unsigned int){ 0 }));]] + )], + [AC_MSG_RESULT([yes]) + AC_DEFINE([HAVE_BUILTIN_SUB_OVERFLOW], [1], [define if the compiler supports __builtin_sub_overflow().]) + ], + [AC_MSG_RESULT([no]) + ]) + +AC_MSG_CHECKING([compiler support for __builtin_mul_overflow()]) +AC_LINK_IFELSE( + [AC_LANG_PROGRAM( + [[#include ]], + [[return (__builtin_mul_overflow(UINT64_C(UINT64_MAX), UINT64_C(UINT64_MAX), &(uint64_t){ 0 }));]] + )], + [AC_MSG_RESULT([yes]) + AC_DEFINE([HAVE_BUILTIN_MUL_OVERFLOW], [1], [define if the compiler supports __builtin_mul_overflow().]) ], [AC_MSG_RESULT([no]) ]) diff --git a/lib/isc/time.c b/lib/isc/time.c index 5690160aae5..26077befe87 100644 --- a/lib/isc/time.c +++ b/lib/isc/time.c @@ -194,8 +194,8 @@ isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result) { REQUIRE(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S); /* Seconds */ -#if HAVE_BUILTIN_OVERFLOW - if (__builtin_uadd_overflow(t->seconds, i->seconds, &result->seconds)) { +#if HAVE_BUILTIN_ADD_OVERFLOW + if (__builtin_add_overflow(t->seconds, i->seconds, &result->seconds)) { return (ISC_R_RANGE); } #else @@ -225,8 +225,8 @@ isc_time_subtract(const isc_time_t *t, const isc_interval_t *i, REQUIRE(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S); /* Seconds */ -#if HAVE_BUILTIN_OVERFLOW - if (__builtin_usub_overflow(t->seconds, i->seconds, &result->seconds)) { +#if HAVE_BUILTIN_SUB_OVERFLOW + if (__builtin_sub_overflow(t->seconds, i->seconds, &result->seconds)) { return (ISC_R_RANGE); } #else