diff mbox series

[ethtool] netlink: fix -Walloc-size

Message ID 20231124230810.1656050-1-sam@gentoo.org (mailing list archive)
State Accepted, archived
Delegated to: Michal Kubecek
Headers show
Series [ethtool] netlink: fix -Walloc-size | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Sam James Nov. 24, 2023, 11:08 p.m. UTC
GCC 14 introduces a new -Walloc-size included in -Wextra which gives:
```
netlink/strset.c: In function ‘get_perdev_by_ifindex’:
netlink/strset.c:121:16: warning: allocation of insufficient size ‘1’ for type ‘struct perdev_strings’ with size ‘648’ [-Walloc-size]
  121 |         perdev = calloc(sizeof(*perdev), 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(*perdev)`. GCC then sees we're not
doing anything wrong. This is consistent with other use in the codebase too.

Signed-off-by: Sam James <sam@gentoo.org>
---
 netlink/strset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Przemek Kitszel Nov. 27, 2023, 3:46 p.m. UTC | #1
On 11/25/23 00:08, Sam James wrote:
> GCC 14 introduces a new -Walloc-size included in -Wextra which gives:

Nice :)

> ```
> netlink/strset.c: In function ‘get_perdev_by_ifindex’:
> netlink/strset.c:121:16: warning: allocation of insufficient size ‘1’ for type ‘struct perdev_strings’ with size ‘648’ [-Walloc-size]
>    121 |         perdev = calloc(sizeof(*perdev), 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(*perdev)`. GCC then sees we're not
> doing anything wrong. This is consistent with other use in the codebase too.
> 
> Signed-off-by: Sam James <sam@gentoo.org>
> ---
>   netlink/strset.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/netlink/strset.c b/netlink/strset.c
> index fbc9c17..949d597 100644
> --- a/netlink/strset.c
> +++ b/netlink/strset.c
> @@ -118,7 +118,7 @@ static struct perdev_strings *get_perdev_by_ifindex(int ifindex)
>   		return perdev;
>   
>   	/* not found, allocate and insert into list */
> -	perdev = calloc(sizeof(*perdev), 1);
> +	perdev = calloc(1, sizeof(*perdev));
>   	if (!perdev)
>   		return NULL;
>   	perdev->ifindex = ifindex;

Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
diff mbox series

Patch

diff --git a/netlink/strset.c b/netlink/strset.c
index fbc9c17..949d597 100644
--- a/netlink/strset.c
+++ b/netlink/strset.c
@@ -118,7 +118,7 @@  static struct perdev_strings *get_perdev_by_ifindex(int ifindex)
 		return perdev;
 
 	/* not found, allocate and insert into list */
-	perdev = calloc(sizeof(*perdev), 1);
+	perdev = calloc(1, sizeof(*perdev));
 	if (!perdev)
 		return NULL;
 	perdev->ifindex = ifindex;