Browse Source

malloc/memalign: avoid integer overflow

Check that the size passed to memalign() is not greater than PTRDIFF_MAX
before adjusting it, otherwise it may wrap around in the adjustment.
This fixes gcc testsuite test gcc.dg/torture/pr60092.c

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
Max Filippov 1 month ago
parent
commit
7343bd6a49
1 changed files with 6 additions and 0 deletions
  1. 6 0
      libc/stdlib/malloc/memalign.c

+ 6 - 0
libc/stdlib/malloc/memalign.c

@@ -11,6 +11,7 @@
  * Written by Miles Bader <miles@gnu.org>
  */
 
+#include <errno.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/mman.h>
@@ -38,6 +39,11 @@ memalign (size_t alignment, size_t size)
   unsigned long tot_addr, tot_end_addr, addr, end_addr;
   struct heap_free_area **heap = &__malloc_heap;
 
+  if (unlikely(size > PTRDIFF_MAX)) {
+    __set_errno(ENOMEM);
+    return NULL;
+  }
+
   /* Make SIZE something we like.  */
   size = HEAP_ADJUST_SIZE (size);