fa3bfd
Backport the check_mul_overflow_size_t function from this upstream commit:
fa3bfd
fa3bfd
commit 2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da
fa3bfd
Author: Dennis Wölfing <denniswoelfing@gmx.de>
fa3bfd
Date:   Tue May 30 18:26:19 2017 -0300
fa3bfd
fa3bfd
    Add reallocarray function
fa3bfd
fa3bfd
diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h
fa3bfd
index b830d3f58fe74ca3..6ffa091ba3292901 100644
fa3bfd
--- a/malloc/malloc-internal.h
fa3bfd
+++ b/malloc/malloc-internal.h
fa3bfd
@@ -28,5 +28,24 @@ void __malloc_fork_unlock_parent (void) internal_function attribute_hidden;
fa3bfd
 /* Called in the child process after a fork.  */
fa3bfd
 void __malloc_fork_unlock_child (void) internal_function attribute_hidden;
fa3bfd
 
fa3bfd
+/* Set *RESULT to LEFT * RIGHT.  Return true if the multiplication
fa3bfd
+   overflowed.  */
fa3bfd
+static inline bool
fa3bfd
+check_mul_overflow_size_t (size_t left, size_t right, size_t *result)
fa3bfd
+{
fa3bfd
+#if __GNUC__ >= 5
fa3bfd
+  return __builtin_mul_overflow (left, right, result);
fa3bfd
+#else
fa3bfd
+  /* size_t is unsigned so the behavior on overflow is defined.  */
fa3bfd
+  *result = left * right;
fa3bfd
+  size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2);
fa3bfd
+  if (__glibc_unlikely ((left | right) >= half_size_t))
fa3bfd
+    {
fa3bfd
+      if (__glibc_unlikely (right != 0 && *result / right != left))
fa3bfd
+        return true;
fa3bfd
+    }
fa3bfd
+  return false;
fa3bfd
+#endif
fa3bfd
+}
fa3bfd
 
fa3bfd
 #endif /* _MALLOC_PRIVATE_H */