Skip to contentSkip to conversation

net/switchdev: use struct_size over open coded arithmetic

PATCH Applied [email protected] 2 participants 2 messages Feb 10, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net/switchdev: use struct_size over open coded arithmetic

From: Minghao Chi (CGEL ZTE) <[email protected]>

Replace zero-length array with flexible-array member and make use of the struct_size() helper in kmalloc(). For example:

struct switchdev_deferred_item {
    ...
    unsigned long data[];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

---
 net/switchdev/switchdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/switchdev/switchdev.c +1 -1
index b62565278fac..12e6b4146bfb 100644
@@ -85,7 +85,7 @@ static int switchdev_deferred_enqueue(struct net_device *dev,
 {
 	struct switchdev_deferred_item *dfitem;
 
-	dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
+	dfitem = kmalloc(struct_size(dfitem, data, data_len), GFP_ATOMIC);
 	if (!dfitem)
 		return -ENOMEM;
 	dfitem->dev = dev;
Signature · 1 line
2.25.1
#1 lore raw
This patch was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Quoted 11 lines · On Thu, 10 Feb 2022 06:10:08 +0000 you wrote:

From: Minghao Chi (CGEL ZTE) <[email protected]>

Replace zero-length array with flexible-array member and make use of the struct_size() helper in kmalloc(). For example:

struct switchdev_deferred_item {
    ...
    unsigned long data[];
};

[...]

Here is the summary with links:
- [V2] net/switchdev: use struct_size over open coded arithmetic

    https://git.kernel.org/netdev/net-next/c/d8c2858181cc

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html