From: Josef 'Jeff' Sipek Date: Wed, 20 Jun 2018 15:42:28 +0000 (-0400) Subject: lib: mempool - Centralize p_{m,re}alloc() size checks X-Git-Tag: 2.3.9~1619 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2c3991bf486023380efad6ec5c247b62c904bf6;p=thirdparty%2Fdovecot%2Fcore.git lib: mempool - Centralize p_{m,re}alloc() size checks Instead of each mempool implementation having to check the allocation sizes for sanity, we can check that the sizes are within the required bounds in p_malloc() and p_realloc(). Since p_malloc() and p_realloc() are static inlines, some consumers will see a little bit of growth in binary size, but others will be able to optimize the check away at compile time. --- diff --git a/src/lib/mempool-allocfree.c b/src/lib/mempool-allocfree.c index da4b6510db..0ecb29d8e3 100644 --- a/src/lib/mempool-allocfree.c +++ b/src/lib/mempool-allocfree.c @@ -255,9 +255,6 @@ static void *pool_allocfree_malloc(pool_t pool, size_t size) struct allocfree_pool *apool = container_of(pool, struct allocfree_pool, pool); - if (unlikely(size == 0 || size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); - struct pool_block *block = calloc(1, SIZEOF_POOLBLOCK + size); if (block == NULL) i_fatal_status(FATAL_OUTOFMEM, "calloc(1, %"PRIuSIZE_T"): Out of memory", @@ -285,9 +282,6 @@ static void *pool_allocfree_realloc(pool_t pool, void *mem, container_of(pool, struct allocfree_pool, pool); unsigned char *new_mem; - if (unlikely(new_size == 0 || new_size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); - if (mem == NULL) return pool_allocfree_malloc(pool, new_size); diff --git a/src/lib/mempool-alloconly.c b/src/lib/mempool-alloconly.c index 999ebd9e6e..2f4845b57a 100644 --- a/src/lib/mempool-alloconly.c +++ b/src/lib/mempool-alloconly.c @@ -384,9 +384,6 @@ static void *pool_alloconly_malloc(pool_t pool, size_t size) void *mem; size_t alloc_size; - if (unlikely(size == 0 || size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); - #ifndef DEBUG alloc_size = MEM_ALIGN(size); #else @@ -454,9 +451,6 @@ static void *pool_alloconly_realloc(pool_t pool, void *mem, container_of(pool, struct alloconly_pool, pool); unsigned char *new_mem; - if (unlikely(new_size == 0 || new_size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); - if (mem == NULL) return pool_alloconly_malloc(pool, new_size); diff --git a/src/lib/mempool-datastack.c b/src/lib/mempool-datastack.c index f645fd2792..12d31f14ef 100644 --- a/src/lib/mempool-datastack.c +++ b/src/lib/mempool-datastack.c @@ -140,9 +140,6 @@ static void *pool_data_stack_malloc(pool_t pool ATTR_UNUSED, size_t size) struct datastack_pool *dpool = container_of(pool, struct datastack_pool, pool); - if (unlikely(size == 0 || size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); - if (unlikely(dpool->data_stack_frame != data_stack_frame_id)) i_panic("pool_data_stack_malloc(): stack frame changed"); @@ -166,9 +163,6 @@ static void *pool_data_stack_realloc(pool_t pool, void *mem, void *new_mem; /* @UNSAFE */ - if (unlikely(new_size == 0 || new_size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); - if (unlikely(dpool->data_stack_frame != data_stack_frame_id)) i_panic("pool_data_stack_realloc(): stack frame changed"); diff --git a/src/lib/mempool-system.c b/src/lib/mempool-system.c index ab8373142b..fcd8636848 100644 --- a/src/lib/mempool-system.c +++ b/src/lib/mempool-system.c @@ -102,9 +102,6 @@ static void *pool_system_malloc(pool_t pool ATTR_UNUSED, size_t size) int old_errno = errno; #endif - if (unlikely(size == 0 || size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); - mem = calloc(size, 1); if (unlikely(mem == NULL)) { i_fatal_status(FATAL_OUTOFMEM, "pool_system_malloc(%"PRIuSIZE_T @@ -135,9 +132,6 @@ void pool_system_free(pool_t pool ATTR_UNUSED, void *mem ATTR_UNUSED) static void *pool_system_realloc(pool_t pool ATTR_UNUSED, void *mem, size_t old_size, size_t new_size) { - if (unlikely(new_size == 0 || new_size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); - if (mem == NULL) { i_assert(old_size == 0); return pool_system_malloc(pool, new_size); diff --git a/src/lib/mempool-unsafe-datastack.c b/src/lib/mempool-unsafe-datastack.c index 6bc612927d..db5d39e9b1 100644 --- a/src/lib/mempool-unsafe-datastack.c +++ b/src/lib/mempool-unsafe-datastack.c @@ -96,9 +96,6 @@ static void pool_unsafe_data_stack_unref(pool_t *pool ATTR_UNUSED) static void *pool_unsafe_data_stack_malloc(pool_t pool ATTR_UNUSED, size_t size) { - if (unlikely(size == 0 || size > POOL_MAX_ALLOC_SIZE)) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size); - return t_malloc0(size); } @@ -114,9 +111,6 @@ static void *pool_unsafe_data_stack_realloc(pool_t pool ATTR_UNUSED, void *new_mem; /* @UNSAFE */ - if (new_size == 0 || new_size > POOL_MAX_ALLOC_SIZE) - i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size); - if (mem == NULL) return pool_unsafe_data_stack_malloc(pool, new_size); diff --git a/src/lib/mempool.h b/src/lib/mempool.h index 529121b56b..f5a613ece3 100644 --- a/src/lib/mempool.h +++ b/src/lib/mempool.h @@ -101,12 +101,18 @@ size_t pool_get_exp_grown_size(pool_t pool, size_t old_size, size_t min_size); static inline void * ATTR_MALLOC ATTR_RETURNS_NONNULL p_malloc(pool_t pool, size_t size) { + if (unlikely(size == 0 || size > POOL_MAX_ALLOC_SIZE)) + i_panic("Trying to allocate %" PRIuSIZE_T " bytes", size); + return pool->v->malloc(pool, size); } static inline void * ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL p_realloc(pool_t pool, void *mem, size_t old_size, size_t new_size) { + if (unlikely(new_size == 0 || new_size > POOL_MAX_ALLOC_SIZE)) + i_panic("Trying to allocate %" PRIuSIZE_T " bytes", new_size); + return pool->v->realloc(pool, mem, old_size, new_size); }