about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2018-01-04 12:51:48 +0100
committerFlorian Weimer <fweimer@redhat.com>2018-01-04 12:51:48 +0100
commitea00a80db7c0cd1098f848eccd5d1f34d89b7faf (patch)
tree7760aa2ece3cdf59fe6257c9c75ada70856763da
parentab8b49432b302237015d0f44e858c17f92fe3a5b (diff)
downloadglibc-ea00a80db7c0cd1098f848eccd5d1f34d89b7faf.tar.gz
glibc-ea00a80db7c0cd1098f848eccd5d1f34d89b7faf.tar.xz
glibc-ea00a80db7c0cd1098f848eccd5d1f34d89b7faf.zip
Add check_mul_overflow_size_t
Backported from commit 2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da.
-rw-r--r--ChangeLog4
-rw-r--r--malloc/malloc-internal.h19
2 files changed, 23 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 4c11b729ce..fff166c11e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 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.
 
 2017-09-01  Florian Weimer  <fweimer@redhat.com>
diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h
index de6103d7e1..dbd801a58e 100644
--- a/malloc/malloc-internal.h
+++ b/malloc/malloc-internal.h
@@ -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 */