]> arthur.barton.de Git - bup.git/commitdiff
Fall back to calloc when __builtin_mul_overflow isn't available
authorRob Browning <rlb@defaultvalue.org>
Sun, 19 Apr 2020 20:47:25 +0000 (15:47 -0500)
committerRob Browning <rlb@defaultvalue.org>
Mon, 11 May 2020 01:59:38 +0000 (20:59 -0500)
Check for __builtin_mul_overflow at configure time, and fall back to
calloc when it's not found.

Thanks to Greg Troxel for reporting the problem.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
config/configure
lib/bup/_helpers.c

index 33e9ba26a00da52d3c769e536635483d7f3a7b17..fbf174fd8c95df5e0c591c917699af805102f445 100755 (executable)
@@ -97,6 +97,25 @@ AC_CHECK_FUNCS utimes
 AC_CHECK_FUNCS lutimes
 
 
+builtin_mul_overflow_code="
+#include <stddef.h>
+int main(int argc, char **argv)
+{
+    size_t n = 0, size = 0, total;
+    __builtin_mul_overflow(n, size, &total);
+    return 0;
+}
+"
+
+TLOGN "checking for __builtin_mul_overflow"
+if bup_try_c_code "$builtin_mul_overflow_code"; then
+    AC_DEFINE BUP_HAVE_BUILTIN_MUL_OVERFLOW 1
+    TLOG ' (found)'
+else
+    TLOG ' (not found)'
+fi
+
+
 AC_CHECK_FUNCS mincore
 
 mincore_incore_code="
index 5f13d6a1c4f932792cc05e2fdb1e3960e2136bdb..0ea5dff622e0e2cc937bb21f3bf2568e306e22fe 100644 (file)
@@ -89,6 +89,20 @@ static state_t state;
 #endif // PY_MAJOR_VERSION >= 3
 
 
+static void *checked_calloc(size_t n, size_t size)
+{
+    void *result = calloc(n, size);
+    if (!result)
+        PyErr_NoMemory();
+    return result;
+}
+
+#ifndef BUP_HAVE_BUILTIN_MUL_OVERFLOW
+
+#define checked_malloc checked_calloc
+
+#else // defined BUP_HAVE_BUILTIN_MUL_OVERFLOW
+
 static void *checked_malloc(size_t n, size_t size)
 {
     size_t total;
@@ -105,13 +119,7 @@ static void *checked_malloc(size_t n, size_t size)
     return result;
 }
 
-static void *checked_calloc(size_t n, size_t size)
-{
-    void *result = calloc(n, size);
-    if (!result)
-        PyErr_NoMemory();
-    return result;
-}
+#endif // defined BUP_HAVE_BUILTIN_MUL_OVERFLOW
 
 
 #ifndef htonll