Skip to content

Commit

Permalink
lib: mempool - Centralize p_{m,re}alloc() size checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Josef 'Jeff' Sipek authored and villesavolainen committed Feb 6, 2019
1 parent f4cac7d commit 3611195
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 30 deletions.
6 changes: 0 additions & 6 deletions src/lib/mempool-allocfree.c
Expand Up @@ -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",
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions src/lib/mempool-alloconly.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions src/lib/mempool-datastack.c
Expand Up @@ -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");

Expand All @@ -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");

Expand Down
6 changes: 0 additions & 6 deletions src/lib/mempool-system.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/lib/mempool-unsafe-datastack.c
Expand Up @@ -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);
}

Expand All @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/lib/mempool.h
Expand Up @@ -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);
}

Expand Down

0 comments on commit 3611195

Please sign in to comment.