about summary refs log tree commit diff
path: root/support/blob_repeat.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2018-12-15 18:58:09 +0100
committerFlorian Weimer <fweimer@redhat.com>2018-12-15 18:58:56 +0100
commit0c1719e65b2a5a80331d4f635612799f853b0479 (patch)
treef5937edb28119d13dfd7b8bca7f19021b1743e28 /support/blob_repeat.c
parent033a2c0a20393c7e48000d83c2ca7ac8ea292d9e (diff)
downloadglibc-0c1719e65b2a5a80331d4f635612799f853b0479.tar.gz
glibc-0c1719e65b2a5a80331d4f635612799f853b0479.tar.xz
glibc-0c1719e65b2a5a80331d4f635612799f853b0479.zip
support: Do not require overflow builtin in support/blob_repeat.c
It is only available in GCC 5 and later.

Tested-by: Romain Naour <romain.naour@gmail.com>
Diffstat (limited to 'support/blob_repeat.c')
-rw-r--r--support/blob_repeat.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/support/blob_repeat.c b/support/blob_repeat.c
index 16c1e448b9..718846d81d 100644
--- a/support/blob_repeat.c
+++ b/support/blob_repeat.c
@@ -34,6 +34,26 @@
    optimization because mappings carry a lot of overhead.  */
 static const size_t maximum_small_size = 4 * 1024 * 1024;
 
+/* Set *RESULT to LEFT * RIGHT.  Return true if the multiplication
+   overflowed.  See <malloc/malloc-internal.h>.  */
+static inline bool
+check_mul_overflow_size_t (size_t left, size_t right, size_t *result)
+{
+#if __GNUC__ >= 5
+  return __builtin_mul_overflow (left, right, result);
+#else
+  /* size_t is unsigned so the behavior on overflow is defined.  */
+  *result = left * right;
+  size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2);
+  if (__glibc_unlikely ((left | right) >= half_size_t))
+    {
+      if (__glibc_unlikely (right != 0 && *result / right != left))
+        return true;
+    }
+  return false;
+#endif
+}
+
 /* Internal helper for fill.  */
 static void
 fill0 (char *target, const char *element, size_t element_size,
@@ -118,8 +138,8 @@ minimum_stride_size (size_t page_size, size_t element_size)
      common multiple, it appears only once.  Therefore, shift one
      factor.  */
   size_t multiple;
-  if (__builtin_mul_overflow (page_size >> common_zeros, element_size,
-                              &multiple))
+  if (check_mul_overflow_size_t (page_size >> common_zeros, element_size,
+                                 &multiple))
     return 0;
   return multiple;
 }
@@ -255,7 +275,7 @@ support_blob_repeat_allocate (const void *element, size_t element_size,
                               size_t count)
 {
   size_t total_size;
-  if (__builtin_mul_overflow (element_size, count, &total_size))
+  if (check_mul_overflow_size_t (element_size, count, &total_size))
     {
       errno = EOVERFLOW;
       return (struct support_blob_repeat) { 0 };