[PATCH] net/switchdev: use struct_size over open coded arithmetic

From: cgel . zte
Date: Fri Jan 28 2022 - 02:58:20 EST


From: "Minghao Chi (CGEL ZTE)" <chi.minghao@xxxxxxxxxx>

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.

Reported-by: Zeal Robot <zealci@xxxxxxxxxx>
Signed-off-by: Minghao Chi (CGEL ZTE) <chi.minghao@xxxxxxxxxx>
---
net/switchdev/switchdev.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/switchdev/switchdev.c b/net/switchdev/switchdev.c
index b62565278fac..b3aedd0ef4d0 100644
--- a/net/switchdev/switchdev.c
+++ b/net/switchdev/switchdev.c
@@ -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;
--
2.25.1