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

From: cgel . zte
Date: Fri Jan 28 2022 - 03:08:32 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 garp_attr {
struct rb_node node;
enum garp_applicant_state state;
u8 type;
u8 dlen;
unsigned char 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/802/garp.c | 2 +-
net/802/mrp.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/802/garp.c b/net/802/garp.c
index f6012f8e59f0..746763a76f83 100644
--- a/net/802/garp.c
+++ b/net/802/garp.c
@@ -184,7 +184,7 @@ static struct garp_attr *garp_attr_create(struct garp_applicant *app,
return attr;
}
}
- attr = kmalloc(sizeof(*attr) + len, GFP_ATOMIC);
+ attr = kmalloc(struct_size(*attr, data, len), GFP_ATOMIC);
if (!attr)
return attr;
attr->state = GARP_APPLICANT_VO;
diff --git a/net/802/mrp.c b/net/802/mrp.c
index 35e04cc5390c..ce3f1b610a3f 100644
--- a/net/802/mrp.c
+++ b/net/802/mrp.c
@@ -273,7 +273,7 @@ static struct mrp_attr *mrp_attr_create(struct mrp_applicant *app,
return attr;
}
}
- attr = kmalloc(sizeof(*attr) + len, GFP_ATOMIC);
+ attr = kmalloc(struct_size(*attr, value, len), GFP_ATOMIC);
if (!attr)
return attr;
attr->state = MRP_APPLICANT_VO;
--
2.25.1