Re: [PATCH] objtool: Fix calloc call for new -Walloc-size

From: Josh Poimboeuf
Date: Thu Nov 09 2023 - 16:40:36 EST


On Tue, Nov 07, 2023 at 08:55:00PM +0000, Sam James wrote:
> GCC 14 introduces a new -Walloc-size included in -Wextra which errors out
> like:
> ```
> check.c: In function ‘cfi_alloc’:
> check.c:294:33: error: allocation of insufficient size ‘1’ for type ‘struct cfi_state’ with size ‘320’ [-Werror=alloc-size]
> 294 | struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
> | ^~~~~~
> ```
>
> 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 <sam@xxxxxxxxxx>
> ---
> tools/objtool/check.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index e94756e09ca9..548ec3cd7c00 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -291,7 +291,7 @@ static void init_insn_state(struct objtool_file *file, struct insn_state *state,
>
> static struct cfi_state *cfi_alloc(void)
> {
> - struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
> + struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
> if (!cfi) {
> WARN("calloc failed");
> exit(1);

Thanks!

Acked-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>

Peter are you able to grab this or should I put it in my tree first?

--
Josh