]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Replace check_mul_overflow_size_t with __builtin_mul_overflow
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 21 Dec 2018 11:49:37 +0000 (09:49 -0200)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 28 Dec 2018 17:39:45 +0000 (15:39 -0200)
Checked on x86_64-linux-gnu and i686-linux-gnu.

* malloc/alloc_buffer_alloc_array.c (__libc_alloc_buffer_alloc_array):
Use __builtin_mul_overflow in place of check_mul_overflow_size_t.
* malloc/dynarray_emplace_enlarge.c (__libc_dynarray_emplace_enlarge):
Likewise.
* malloc/dynarray_resize.c (__libc_dynarray_resize): Likewise.
* malloc/reallocarray.c (__libc_reallocarray): Likewise.
* malloc/malloc-internal.h (check_mul_overflow_size_t): Remove
function.
* support/blob_repeat.c (check_mul_overflow_size_t,
(minimum_stride_size, support_blob_repeat_allocate): Likewise.

ChangeLog
malloc/alloc_buffer_alloc_array.c
malloc/dynarray_emplace_enlarge.c
malloc/dynarray_resize.c
malloc/malloc-internal.h
malloc/reallocarray.c
support/blob_repeat.c

index 241f62157c40ac065170a9ff588befb85bf678a0..86d978c06d31dd497121f5bd71d0fe7bc018365f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2018-12-28  Adhemerval Zanella  <adhemerval.zanella@linaro.org>
+
+       * malloc/alloc_buffer_alloc_array.c (__libc_alloc_buffer_alloc_array):
+       Use __builtin_mul_overflow in place of check_mul_overflow_size_t.
+       * malloc/dynarray_emplace_enlarge.c (__libc_dynarray_emplace_enlarge):
+       Likewise.
+       * malloc/dynarray_resize.c (__libc_dynarray_resize): Likewise.
+       * malloc/reallocarray.c (__libc_reallocarray): Likewise.
+       * malloc/malloc-internal.h (check_mul_overflow_size_t): Remove
+       function.
+       * support/blob_repeat.c (check_mul_overflow_size_t,
+       (minimum_stride_size, support_blob_repeat_allocate): Likewise.
+
 2018-12-28  Aurelien Jarno  <aurelien@aurel32.net>
 
        * sysdeps/alpha/fpu/libm-test-ulps: Regenerated.
index 1dd098a8fcc84dfa891f956bf76e20973691e89d..7505422b43ef652b52b1dd2ab2fb0ff111b3fec9 100644 (file)
@@ -17,7 +17,6 @@
    <http://www.gnu.org/licenses/>.  */
 
 #include <alloc_buffer.h>
-#include <malloc-internal.h>
 #include <libc-pointer-arith.h>
 
 void *
@@ -28,7 +27,7 @@ __libc_alloc_buffer_alloc_array (struct alloc_buffer *buf, size_t element_size,
   /* The caller asserts that align is a power of two.  */
   size_t aligned = ALIGN_UP (current, align);
   size_t size;
-  bool overflow = check_mul_overflow_size_t (element_size, count, &size);
+  bool overflow = __builtin_mul_overflow (element_size, count, &size);
   size_t new_current = aligned + size;
   if (!overflow                /* Multiplication did not overflow.  */
       && aligned >= current    /* No overflow in align step.  */
index 0408271e274edae8bbc8460ca938c4c11b3c4b2a..aa8f5fae3f041155335e1e5f632cbaace624595d 100644 (file)
@@ -18,7 +18,6 @@
 
 #include <dynarray.h>
 #include <errno.h>
-#include <malloc-internal.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -52,7 +51,7 @@ __libc_dynarray_emplace_enlarge (struct dynarray_header *list,
     }
 
   size_t new_size;
-  if (check_mul_overflow_size_t (new_allocated, element_size, &new_size))
+  if (__builtin_mul_overflow (new_allocated, element_size, &new_size))
     return false;
   void *new_array;
   if (list->array == scratch)
index 0bfca1ba4b78d8673e5d96d70c66b6bc784f6501..0205cf7ab280b6032825d68c235fc07ea5ab69d5 100644 (file)
@@ -18,7 +18,6 @@
 
 #include <dynarray.h>
 #include <errno.h>
-#include <malloc-internal.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -38,7 +37,7 @@ __libc_dynarray_resize (struct dynarray_header *list, size_t size,
      over-allocation here.  */
 
   size_t new_size_bytes;
-  if (check_mul_overflow_size_t (size, element_size, &new_size_bytes))
+  if (__builtin_mul_overflow (size, element_size, &new_size_bytes))
     {
       /* Overflow.  */
       __set_errno (ENOMEM);
index 9cee0fb2d7e0994b065869a570c1d6526c5a8bbc..70d5b385044bbd0bb7447e1c615ac271fd0138c2 100644 (file)
@@ -74,24 +74,4 @@ void __malloc_fork_unlock_child (void) attribute_hidden;
 /* Called as part of the thread shutdown sequence.  */
 void __malloc_arena_thread_freeres (void) 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 */
index 319eccd21f74bffd3730647ef0714c1923662897..32642303a35892b561a468bf1ae11e992b961c01 100644 (file)
 
 #include <errno.h>
 #include <malloc.h>
-#include <malloc/malloc-internal.h>
 
 void *
 __libc_reallocarray (void *optr, size_t nmemb, size_t elem_size)
 {
   size_t bytes;
-  if (check_mul_overflow_size_t (nmemb, elem_size, &bytes))
+  if (__builtin_mul_overflow (nmemb, elem_size, &bytes))
     {
       __set_errno (ENOMEM);
       return 0;
     }
-  else
-    return realloc (optr, bytes);
+  return realloc (optr, bytes);
 }
 libc_hidden_def (__libc_reallocarray)
 
index 718846d81d75b70aef9bfa1f9955e2d584b1af3b..daa1b7fd96ffdc9f541ba9f6adbb6671df48bffc 100644 (file)
    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,
@@ -138,8 +118,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 (check_mul_overflow_size_t (page_size >> common_zeros, element_size,
-                                 &multiple))
+  if (__builtin_mul_overflow (page_size >> common_zeros, element_size,
+                             &multiple))
     return 0;
   return multiple;
 }
@@ -275,7 +255,7 @@ support_blob_repeat_allocate (const void *element, size_t element_size,
                               size_t count)
 {
   size_t total_size;
-  if (check_mul_overflow_size_t (element_size, count, &total_size))
+  if (__builtin_mul_overflow (element_size, count, &total_size))
     {
       errno = EOVERFLOW;
       return (struct support_blob_repeat) { 0 };