]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: mempool - Centralize p_{m,re}alloc() size checks
authorJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Wed, 20 Jun 2018 15:42:28 +0000 (11:42 -0400)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 4 Jul 2018 08:28:51 +0000 (08:28 +0000)
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.

src/lib/mempool-allocfree.c
src/lib/mempool-alloconly.c
src/lib/mempool-datastack.c
src/lib/mempool-system.c
src/lib/mempool-unsafe-datastack.c
src/lib/mempool.h

index da4b6510db69aa4f83714d471482dd5c57832c58..0ecb29d8e36096a801c7290335d23122a06827b5 100644 (file)
@@ -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);
 
index 999ebd9e6ef27bad34fbb5839c0e8439749a5b93..2f4845b57ab69469827e522c6ff34afb1a794f48 100644 (file)
@@ -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);
 
index f645fd27922c711828fa18b4329976a7e30e2a1b..12d31f14efd944dc15e854a24f6cf0b4585cd3a9 100644 (file)
@@ -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");
 
index ab8373142b8f50b2302f5a31178477043489bb1a..fcd863684847e96138caf97256a72f73d1a0beb9 100644 (file)
@@ -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);
index 6bc612927d2f9c4eaf182ae3e75db408ec5ad0b4..db5d39e9b19a8a62d7b99f89b52f6b3a1acb42b5 100644 (file)
@@ -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);
 
index 529121b56bb2159e64159fa7e96284cf35a1efa3..f5a613ece313935ff3c601d580a2fa1af332ace4 100644 (file)
@@ -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);
 }