]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: mempool-alloconly - Cleanly fail allocations just below SSIZE_T_MAX
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 27 Nov 2017 13:17:52 +0000 (15:17 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 27 Nov 2017 17:37:10 +0000 (19:37 +0200)
Previously it would have assert-crashed in nearest_power().

src/lib/mempool-alloconly.c

index 66ba6bfcc266ff7625d06d599b1fffd1d98a0376..ab8c37b3c68c9ad9aae3f9c804d7294202f92c88 100644 (file)
@@ -5,8 +5,12 @@
 #include "safe-memset.h"
 #include "mempool.h"
 
-
-#define MAX_ALLOC_SIZE SSIZE_T_MAX
+#ifndef DEBUG
+#  define POOL_ALLOCONLY_MAX_EXTRA MEM_ALIGN(1)
+#else
+#  define POOL_ALLOCONLY_MAX_EXTRA \
+       (MEM_ALIGN(sizeof(size_t)) + MEM_ALIGN(1) + MEM_ALIGN(SENTRY_COUNT))
+#endif
 
 struct alloconly_pool {
        struct pool pool;
@@ -260,7 +264,7 @@ static void *pool_alloconly_malloc(pool_t pool, size_t size)
        void *mem;
        size_t alloc_size;
 
-       if (unlikely(size == 0 || size > SSIZE_T_MAX))
+       if (unlikely(size == 0 || size > SSIZE_T_MAX - POOL_ALLOCONLY_MAX_EXTRA))
                i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
 
 #ifndef DEBUG
@@ -328,7 +332,7 @@ static void *pool_alloconly_realloc(pool_t pool, void *mem,
        struct alloconly_pool *apool = (struct alloconly_pool *)pool;
        unsigned char *new_mem;
 
-       if (unlikely(new_size == 0 || new_size > SSIZE_T_MAX))
+       if (unlikely(new_size == 0 || new_size > SSIZE_T_MAX - POOL_ALLOCONLY_MAX_EXTRA))
                i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);
 
        if (mem == NULL)