Blame SOURCES/glibc-rh677316-check_mul_overflow_size_t.patch

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