]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Check for working __builtin_mul_overflow() implementation
authorOndřej Surý <ondrej@isc.org>
Fri, 23 Sep 2022 13:35:58 +0000 (15:35 +0200)
committerOndřej Surý <ondrej@isc.org>
Tue, 27 Sep 2022 15:10:42 +0000 (17:10 +0200)
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

configure.ac
lib/isc/time.c

index 609f138efe06d5ffbe23329576524a8afc05519a..fc643e57e642daf031d26e5e5445bb85c6544ffe 100644 (file)
@@ -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 <limits.h>]],
-     [[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 <limits.h>]],
+     [[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 <limits.h>]],
+     [[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])
   ])
index 5690160aae5727c1c21617135e456f3cd13b6703..26077befe87ae7b902cb662597aecda3701d424d 100644 (file)
@@ -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