From 81270292c23ff28aba1abd9a65f0624b657de82b Mon Sep 17 00:00:00 2001 From: Sam James Date: Sun, 5 Nov 2023 22:27:17 +0000 Subject: [PATCH] Fix -Walloc-size MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit GCC 14 introduces a new -Walloc-size included in -Wextra which gives: ``` urcu-call-rcu-impl.h:912:20: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:927:22: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:912:20: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:927:22: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:912:20: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:927:22: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:912:20: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:927:22: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:912:20: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:927:22: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] workqueue.c:401:20: warning: allocation of insufficient size '1' for type 'struct urcu_workqueue_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] workqueue.c:432:14: warning: allocation of insufficient size '1' for type 'struct urcu_workqueue_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:912:20: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion' with size '16' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] urcu-call-rcu-impl.h:927:22: warning: allocation of insufficient size '1' for type 'struct call_rcu_completion_work' with size '24' [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] qsbr.c:49:14: warning: allocation of insufficient size ‘1’ for type ‘struct mynode’ with size ‘40’ [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] mb.c:50:14: warning: allocation of insufficient size ‘1’ for type ‘struct mynode’ with size ‘40’ [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] membarrier.c:50:14: warning: allocation of insufficient size ‘1’ for type ‘struct mynode’ with size ‘40’ [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] signal.c:49:14: warning: allocation of insufficient size ‘1’ for type ‘struct mynode’ with size ‘40’ [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] bp.c:49:14: warning: allocation of insufficient size ‘1’ for type ‘struct mynode’ with size ‘40’ [-Walloc-size[https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Walloc-size]] ``` The calloc prototype is: ``` void *calloc(size_t nmemb, size_t size); ``` So, just swap the number of members and size arguments to match the prototype, as we're initialising 1 struct of size `sizeof(struct ...)`. GCC then sees we're not doing anything wrong. Signed-off-by: Sam James Signed-off-by: Mathieu Desnoyers Change-Id: Id84ce5cf9a1b97bfa942597aa188ef6e27e7c10d --- doc/examples/urcu-flavors/bp.c | 2 +- doc/examples/urcu-flavors/mb.c | 2 +- doc/examples/urcu-flavors/membarrier.c | 2 +- doc/examples/urcu-flavors/qsbr.c | 2 +- src/urcu-call-rcu-impl.h | 4 ++-- src/workqueue.c | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/examples/urcu-flavors/bp.c b/doc/examples/urcu-flavors/bp.c index 0bbc303..d423733 100644 --- a/doc/examples/urcu-flavors/bp.c +++ b/doc/examples/urcu-flavors/bp.c @@ -32,7 +32,7 @@ int add_node(uint64_t v) { struct mynode *node; - node = calloc(sizeof(*node), 1); + node = calloc(1, sizeof(*node)); if (!node) return -1; node->value = v; diff --git a/doc/examples/urcu-flavors/mb.c b/doc/examples/urcu-flavors/mb.c index 9c46f2f..7069c24 100644 --- a/doc/examples/urcu-flavors/mb.c +++ b/doc/examples/urcu-flavors/mb.c @@ -33,7 +33,7 @@ int add_node(uint64_t v) { struct mynode *node; - node = calloc(sizeof(*node), 1); + node = calloc(1, sizeof(*node)); if (!node) return -1; node->value = v; diff --git a/doc/examples/urcu-flavors/membarrier.c b/doc/examples/urcu-flavors/membarrier.c index dad39cb..73b9bd8 100644 --- a/doc/examples/urcu-flavors/membarrier.c +++ b/doc/examples/urcu-flavors/membarrier.c @@ -33,7 +33,7 @@ int add_node(uint64_t v) { struct mynode *node; - node = calloc(sizeof(*node), 1); + node = calloc(1, sizeof(*node)); if (!node) return -1; node->value = v; diff --git a/doc/examples/urcu-flavors/qsbr.c b/doc/examples/urcu-flavors/qsbr.c index 0b5b1a9..ca00933 100644 --- a/doc/examples/urcu-flavors/qsbr.c +++ b/doc/examples/urcu-flavors/qsbr.c @@ -32,7 +32,7 @@ int add_node(uint64_t v) { struct mynode *node; - node = calloc(sizeof(*node), 1); + node = calloc(1, sizeof(*node)); if (!node) return -1; node->value = v; diff --git a/src/urcu-call-rcu-impl.h b/src/urcu-call-rcu-impl.h index 22d8570..2ea1efc 100644 --- a/src/urcu-call-rcu-impl.h +++ b/src/urcu-call-rcu-impl.h @@ -895,7 +895,7 @@ void rcu_barrier(void) goto online; } - completion = calloc(sizeof(*completion), 1); + completion = calloc(1, sizeof(*completion)); if (!completion) urcu_die(errno); @@ -910,7 +910,7 @@ void rcu_barrier(void) cds_list_for_each_entry(crdp, &call_rcu_data_list, list) { struct call_rcu_completion_work *work; - work = calloc(sizeof(*work), 1); + work = calloc(1, sizeof(*work)); if (!work) urcu_die(errno); work->completion = completion; diff --git a/src/workqueue.c b/src/workqueue.c index a28d09d..10b9fde 100644 --- a/src/workqueue.c +++ b/src/workqueue.c @@ -384,7 +384,7 @@ struct urcu_workqueue_completion *urcu_workqueue_create_completion(void) { struct urcu_workqueue_completion *completion; - completion = calloc(sizeof(*completion), 1); + completion = calloc(1, sizeof(*completion)); if (!completion) urcu_die(errno); urcu_ref_set(&completion->ref, 1); @@ -415,7 +415,7 @@ void urcu_workqueue_queue_completion(struct urcu_workqueue *workqueue, { struct urcu_workqueue_completion_work *work; - work = calloc(sizeof(*work), 1); + work = calloc(1, sizeof(*work)); if (!work) urcu_die(errno); work->completion = completion; -- 2.34.1