Skip to contentSkip to conversation

usb: misc: adutux: replace kmalloc() with kmalloc_array()

PATCH In review Ethan Carter Edwards 2 participants 2 messages May 3, 2025 – May 21, 2025 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: misc: adutux: replace kmalloc() with kmalloc_array()

Replace kmalloc with internal multiplication with kmalloc_array to improve code readability and prevent potential overflows.

---
 drivers/usb/misc/adutux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/misc/adutux.c +2 -2
index ed6a19254d2ff9fead898adad0b3996822e10167..000a3ade743258f381d85397395a43c28a8481cc 100644
@@ -680,7 +680,7 @@ static int adu_probe(struct usb_interface *interface,
 	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
 	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
 
-	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
+	dev->read_buffer_primary = kmalloc_array(4, in_end_size, GFP_KERNEL);
 	if (!dev->read_buffer_primary)
 		goto error;
 
@@ -690,7 +690,7 @@ static int adu_probe(struct usb_interface *interface,
 	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
 	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
 
-	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
+	dev->read_buffer_secondary = kmalloc_array(4, in_end_size, GFP_KERNEL);
 	if (!dev->read_buffer_secondary)
 		goto error;
 

---
base-commit: 37ff6e9a2ce321b7932d3987701757fb4d87b0e6
change-id: 20250503-adutux_kmalloc_array-c81b5d7c3b37

Best regards,

Signature · 1 line
Ethan Carter Edwards <[email protected]>
#1 lore raw
Quoted 2 lines · Ethan Carter Edwards

Replace kmalloc with internal multiplication with kmalloc_array to improve code readability and prevent potential overflows.

But this is not an array of a structure size.

Quoted 11 lines
---
 drivers/usb/misc/adutux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/misc/adutux.c +0 -0
index ed6a19254d2ff9fead898adad0b3996822e10167..000a3ade743258f381d85397395a43c28a8481cc 100644

drivers/usb/misc/adutux.c static int adu_probe(struct usb_interface *interface, · line 683

Quoted 5 lines
drivers/usb/misc/adutux.c +1 -1
@@ -680,7 +680,7 @@ static int adu_probe(struct usb_interface *interface,
 	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
 	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
 
-	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
+	dev->read_buffer_primary = kmalloc_array(4, in_end_size, GFP_KERNEL);

This is a buffer and you need the size to be correct based on the commands, right? It's not an array of a structure, but rather a stream of bytes.

drivers/usb/misc/adutux.c static int adu_probe(struct usb_interface *interface, · line 693

Quoted 9 lines
drivers/usb/misc/adutux.c +1 -1
 	if (!dev->read_buffer_primary)
 		goto error;
 
@@ -690,7 +690,7 @@ static int adu_probe(struct usb_interface *interface,
 	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
 	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
 
-	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
+	dev->read_buffer_secondary = kmalloc_array(4, in_end_size, GFP_KERNEL);

Same here.

I think the original code is just fine as there's no bug here or way it can overflow, right?

thanks,

greg k-h