summaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
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;
}