pool_create: do not fail if structure is too small to handle list pointers

This commit is contained in:
2022-01-16 21:10:41 +01:00
parent 38c6c978a9
commit 60923f49cd

View File

@@ -15,6 +15,8 @@
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "list.h"
#include "pool.h"
#include "debug.h"
@@ -46,9 +48,15 @@ pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
log_f(1, "name=[%s] growsize=%u eltsize=%lu\n",
name, growsize, eltsize);
# endif
/* we need at least this space in struct */
if (eltsize < sizeof (struct list_head))
return NULL;
/* we need at least sizeof(struct list_head) space in pool elements
*/
if (eltsize < sizeof (struct list_head)) {
# ifdef DEBUG_POOL
log_f(1, "[%s]: structure size too small (%lu < %lu), adjusting to %lu.\n",
name, eltsize, sizeof(struct list_head), sizeof(struct list_head));
# endif
eltsize = sizeof(struct list_head);
}
if ((pool = malloc(sizeof (*pool)))) {
strncpy(pool->name, name, POOL_NAME_LENGTH - 1);
pool->name[POOL_NAME_LENGTH - 1] = 0;
@@ -102,8 +110,9 @@ void *pool_get(pool_t *pool)
if (!block) {
# ifdef DEBUG_POOL
log_f(1, "[%s]: failed block allocation\n");
log_f(1, "[%s]: failed block allocation\n", pool->name);
# endif
errno = ENOMEM;
return NULL;
}