Skip to contentSkip to conversation

extcon: usbc-cros-ec: Use struct_size() helper in kzalloc()

PATCH Applied Gustavo A. R. Silva 2 participants 3 messages Jan 25, 2022 – Feb 16, 2022

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

PATCH 1/1 extcon: usbc-cros-ec: Use struct_size() helper in kzalloc()

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/extcon/extcon-usbc-cros-ec.c:71:23: warning: using sizeof on a flexible structure

---
 drivers/extcon/extcon-usbc-cros-ec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/extcon/extcon-usbc-cros-ec.c +1 -1
index 5290cc2d19d9..fde1db62be0d 100644
@@ -68,7 +68,7 @@ static int cros_ec_pd_command(struct cros_ec_extcon_info *info,
 	struct cros_ec_command *msg;
 	int ret;
 
-	msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
+	msg = kzalloc(struct_size(msg, data, max(outsize, insize)), GFP_KERNEL);
 	if (!msg)
 		return -ENOMEM;
 
Signature · 1 line
2.27.0
maintainer Applied
#1 lore raw
Quoted 19 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/extcon/extcon-usbc-cros-ec.c:71:23: warning: using sizeof on a flexible structure

---
 drivers/extcon/extcon-usbc-cros-ec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/extcon/extcon-usbc-cros-ec.c +0 -0
index 5290cc2d19d9..fde1db62be0d 100644

drivers/extcon/extcon-usbc-cros-ec.c static int cros_ec_pd_command(struct cros_ec_extcon_info *info, · line 74

Quoted 8 lines
drivers/extcon/extcon-usbc-cros-ec.c +1 -1
@@ -68,7 +68,7 @@ static int cros_ec_pd_command(struct cros_ec_extcon_info *info,
 	struct cros_ec_command *msg;
 	int ret;
 
-	msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
+	msg = kzalloc(struct_size(msg, data, max(outsize, insize)), GFP_KERNEL);
 	if (!msg)
 		return -ENOMEM;
 

Applied it. Thanks.

Signature · 3 lines
Best Regards,
Chanwoo Choi
Samsung Electronics
#2 lore raw
Quoted 3 lines · Chanwoo Choi

On 1/26/22 4:26 AM, Gustavo A. R. Silva wrote:

Applied it. Thanks.

Great. :)

Thanks, Chanwoo.
--
Gustavo