diff mbox series

[uclibc-ng-devel] malloc/memalign: avoid integer overflow

Message ID 20240513003650.2813176-1-jcmvbkbc@gmail.com
State Accepted
Headers show
Series [uclibc-ng-devel] malloc/memalign: avoid integer overflow | expand

Commit Message

Max Filippov May 13, 2024, 12:36 a.m. UTC
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>
---
 libc/stdlib/malloc/memalign.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Waldemar Brodkorb May 14, 2024, 8:05 p.m. UTC | #1
Hi Max,
Max Filippov wrote,

> 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

Thanks for the patch. I added errno.h include file, which was needed
for me to compile successfully.

Pushed with this change.

best regards
 Waldemar
diff mbox series

Patch

diff --git a/libc/stdlib/malloc/memalign.c b/libc/stdlib/malloc/memalign.c
index 665f20cfbfab..a77b8cebcde7 100644
--- a/libc/stdlib/malloc/memalign.c
+++ b/libc/stdlib/malloc/memalign.c
@@ -38,6 +38,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);