[PATCH bpf-next] libbpf: fix warning

Matteo Croce posted 1 patch 2 months, 2 weeks ago
tools/lib/bpf/libbpf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH bpf-next] libbpf: fix warning
Posted by Matteo Croce 2 months, 2 weeks ago
From: Matteo Croce <teknoraver@meta.com>

When compiling libbpf with some compilers, this warning is triggered:

libbpf.c: In function ‘bpf_object__gen_loader’:
libbpf.c:9209:28: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
 9209 |         gen = calloc(sizeof(*gen), 1);
      |                            ^
libbpf.c:9209:28: note: earlier argument should specify number of elements, later size of each element

Fix this by inverting the calloc() arguments.

Signed-off-by: Matteo Croce <teknoraver@meta.com>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index aee36402f0a3..af85989ae2c9 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9210,7 +9210,7 @@ int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
 		return libbpf_err(-EFAULT);
 	if (!OPTS_VALID(opts, gen_loader_opts))
 		return libbpf_err(-EINVAL);
-	gen = calloc(sizeof(*gen), 1);
+	gen = calloc(1, sizeof(*gen));
 	if (!gen)
 		return libbpf_err(-ENOMEM);
 	gen->opts = opts;
-- 
2.39.5 (Apple Git-154)

Re: [PATCH bpf-next] libbpf: fix warning
Posted by Yonghong Song 2 months, 2 weeks ago

On 7/17/25 1:03 PM, Matteo Croce wrote:
> From: Matteo Croce <teknoraver@meta.com>
>
> When compiling libbpf with some compilers, this warning is triggered:
>
> libbpf.c: In function ‘bpf_object__gen_loader’:
> libbpf.c:9209:28: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
>   9209 |         gen = calloc(sizeof(*gen), 1);
>        |                            ^
> libbpf.c:9209:28: note: earlier argument should specify number of elements, later size of each element
>
> Fix this by inverting the calloc() arguments.
>
> Signed-off-by: Matteo Croce <teknoraver@meta.com>

Acked-by: Yonghong Song <yonghong.song@linux.dev>