From f7ab89768d113083f14e0c84b8cd81755baacc82 Mon Sep 17 00:00:00 2001 From: Aaron Jones Date: Fri, 18 Jan 2019 04:22:15 +0000 Subject: [PATCH] core/alloc.c, core/allocator.c: fix calloc(3) and free(3) usage calloc(3)'s 2 arguments are, in order: number of members, size of member. -> Swap the arguments to the correct order. free(3) is well-documented to do nothing when passed NULL. -> Remove conditional check for non-NULL. Also add malloc and alloc_size function attributes where necessary. --- src/libmowgli/core/alloc.c | 5 ++--- src/libmowgli/core/allocator.c | 11 +++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/libmowgli/core/alloc.c b/src/libmowgli/core/alloc.c index 638d476..4450a75 100644 --- a/src/libmowgli/core/alloc.c +++ b/src/libmowgli/core/alloc.c @@ -35,14 +35,13 @@ typedef struct static void * MOWGLI_FATTR_MALLOC MOWGLI_FATTR_ALLOC_SIZE(1) _mowgli_bootstrap_alloc(size_t size) { - return calloc(size, 1); + return calloc(1, size); } static void _mowgli_bootstrap_free(void *ptr) { - if (ptr) - free(ptr); + (void) free(ptr); } static mowgli_allocation_policy_t _mowgli_allocator_bootstrap = diff --git a/src/libmowgli/core/allocator.c b/src/libmowgli/core/allocator.c index d4a7dac..104b0d4 100644 --- a/src/libmowgli/core/allocator.c +++ b/src/libmowgli/core/allocator.c @@ -26,22 +26,21 @@ mowgli_allocation_policy_t *mowgli_allocator_malloc = NULL; -static void * +static void * MOWGLI_FATTR_MALLOC MOWGLI_FATTR_ALLOC_SIZE(1) mowgli_allocator_func_malloc(size_t size) { - return calloc(size, 1); + return calloc(1, size); } static void mowgli_allocator_func_free(void *ptr) { - if (ptr) - free(ptr); + (void) free(ptr); } void mowgli_allocator_bootstrap(void) { - mowgli_allocator_malloc = mowgli_allocation_policy_create("malloc", mowgli_allocator_func_malloc, - mowgli_allocator_func_free); + mowgli_allocator_malloc = \ + mowgli_allocation_policy_create("malloc", &mowgli_allocator_func_malloc, &mowgli_allocator_func_free); }