summaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-11-23 13:03:49 -0500
committerTavian Barnes <tavianator@tavianator.com>2023-11-23 13:56:03 -0500
commitf9f43fe44f4a013aac94d5787cf827ec04b4c861 (patch)
treec74db56c97a9aac553de11f34ff3ddee301c1c90 /src/alloc.c
parent3fe1e7ca64bcd3724b00cf399ed9ae6f60e0a008 (diff)
downloadbfs-f9f43fe44f4a013aac94d5787cf827ec04b4c861.tar.xz
alloc: New ALLOC_MAX macro
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c12
1 files changed, 10 insertions, 2 deletions
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 <errno.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
+/** 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;
}