Skip to contentSkip to conversation

netlink: fix -Walloc-size

PATCH In review Sam James 2 participants 2 messages Nov 24, 2023 – Nov 27, 2023

Subsystems (2)
lore
Share & tools
lore

Sam James

author
#0 lore raw

PATCH 1/1 netlink: fix -Walloc-size

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.

---
 netlink/strset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/strset.c +1 -1
index fbc9c17..949d597 100644
@@ -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;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Sam James

GCC 14 introduces a new -Walloc-size included in -Wextra which gives:

Nice :)

Quoted 26 lines
```
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.

---
  netlink/strset.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
netlink/strset.c +0 -0
index fbc9c17..949d597 100644

netlink/strset.c static struct perdev_strings *get_perdev_by_ifindex(int ifindex) · line 124

Quoted 8 lines
netlink/strset.c +1 -1
@@ -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;