]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Add check_mul_overflow_size_t
authorFlorian Weimer <fweimer@redhat.com>
Thu, 4 Jan 2018 11:51:48 +0000 (12:51 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 4 Jan 2018 11:51:48 +0000 (12:51 +0100)
Backported from commit 2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da.

ChangeLog
malloc/malloc-internal.h

index 4c11b729ce205b9861244613e0bd405d11d4b195..fff166c11edc156c9a9bb60aa3564c7265fa10ac 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2018-01-04  Florian Weimer  <fweimer@redhat.com>
+
+       * malloc/malloc-internal.h (check_mul_overflow_size_t): New.
+
 2018-01-04  Florian Weimer  <fweimer@redhat.com>
 
        * include/libc-pointer-arith.h: New file.
index de6103d7e1e06799fe5472134475c3812c6ee8d0..dbd801a58eeceabf126a4e4dbca71bcf058b4bb2 100644 (file)
@@ -81,5 +81,24 @@ void __malloc_fork_unlock_parent (void) internal_function attribute_hidden;
 /* Called in the child process after a fork.  */
 void __malloc_fork_unlock_child (void) internal_function attribute_hidden;
 
+/* Set *RESULT to LEFT * RIGHT.  Return true if the multiplication
+   overflowed.  */
+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
+}
 
 #endif /* _MALLOC_INTERNAL_H */