summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Filippov <jcmvbkbc@gmail.com>2024-05-12 17:36:50 -0700
committerWaldemar Brodkorb <wbx@openadk.org>2024-05-14 19:36:17 +0200
commit7343bd6a49c5e3fe622f0bf473c124947d5e1210 (patch)
tree918626406e3a7b0700e00a4de836fdb2343e6d9d
parentad21b95c97cdb8741922d163f132e9c726c2ce5b (diff)
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>
-rw-r--r--libc/stdlib/malloc/memalign.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/libc/stdlib/malloc/memalign.c b/libc/stdlib/malloc/memalign.c
index 665f20cfb..54f6dbc6c 100644
--- a/libc/stdlib/malloc/memalign.c
+++ b/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);