From f9f43fe44f4a013aac94d5787cf827ec04b4c861 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 23 Nov 2023 13:03:49 -0500 Subject: alloc: New ALLOC_MAX macro --- src/alloc.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/alloc.c b/src/alloc.c index 97f90bc..8c88813 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -8,8 +8,16 @@ #include "sanity.h" #include #include +#include #include +/** The largest possible allocation size. */ +#if PTRDIFF_MAX < SIZE_MAX / 2 +# define ALLOC_MAX ((size_t)PTRDIFF_MAX) +#else +# define ALLOC_MAX (SIZE_MAX / 2) +#endif + /** Portable aligned_alloc()/posix_memalign(). */ static void *xmemalign(size_t align, size_t size) { bfs_assert(has_single_bit(align)); @@ -29,7 +37,7 @@ void *alloc(size_t align, size_t size) { bfs_assert(has_single_bit(align)); bfs_assert(is_aligned(align, size)); - if (size >> (SIZE_WIDTH - 1)) { + if (size > ALLOC_MAX) { errno = EOVERFLOW; return NULL; } @@ -45,7 +53,7 @@ void *zalloc(size_t align, size_t size) { bfs_assert(has_single_bit(align)); bfs_assert(is_aligned(align, size)); - if (size >> (SIZE_WIDTH - 1)) { + if (size > ALLOC_MAX) { errno = EOVERFLOW; return NULL; } -- cgit v1.2.3