Skip to contentSkip to conversation

USB: serial: mos7720: improve handling of a kmalloc failure in read_mos_reg()

PATCH In review [email protected] 3 participants 3 messages Jan 11, 2021 – Jan 12, 2021

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

PATCH 1/1 USB: serial: mos7720: improve handling of a kmalloc failure in read_mos_reg()

From: Tom Rix <[email protected]>

clang static analysis reports this problem

mos7720.c:352:2: warning: Undefined or garbage value returned to caller

        return d;
        ^~~~~~~~

In the parport_mos7715_read_data()'s call to read_mos_reg(), 'd' is only set after the alloc block.

	buf = kmalloc(1, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

Although the problem is reported in parport_most7715_read_data(), none of the callee's of read_mos_reg() check the return status.

So move the clearing of data to before the malloc.

Fixes 0d130367abf5 ("USB: serial: mos7720: fix control-message error handling")
---
 drivers/usb/serial/mos7720.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/serial/mos7720.c +1 -1
index 41ee2984a0df..23e8162c768b 100644
@@ -214,6 +214,7 @@ static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 	u8 *buf;
 	int status;
 
+	*data = 0;
 	buf = kmalloc(1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
@@ -227,7 +228,6 @@ static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 			"mos7720: usb_control_msg() failed: %d\n", status);
 		if (status >= 0)
 			status = -EIO;
-		*data = 0;
 	}
 
 	kfree(buf);
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · On Mon, Jan 11, 2021 at 02:09:04PM -0800, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis reports this problem

mos7720.c:352:2: warning: Undefined or garbage value returned to caller

        return d;
        ^~~~~~~~

In the parport_mos7715_read_data()'s call to read_mos_reg(), 'd' is only set after the alloc block.

	buf = kmalloc(1, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
Quoted 7 lines

Although the problem is reported in parport_most7715_read_data(), none of the callee's of read_mos_reg() check the return status.

So move the clearing of data to before the malloc.

Fixes 0d130367abf5 ("USB: serial: mos7720: fix control-message error handling")
Quoted 27 lines
---
 drivers/usb/serial/mos7720.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/serial/mos7720.c +1 -1
index 41ee2984a0df..23e8162c768b 100644
@@ -214,6 +214,7 @@ static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 	u8 *buf;
 	int status;
 
+	*data = 0;
 	buf = kmalloc(1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
@@ -227,7 +228,6 @@ static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 			"mos7720: usb_control_msg() failed: %d\n", status);
 		if (status >= 0)
 			status = -EIO;
-		*data = 0;
 	}
 
 	kfree(buf);
Signature · 1 line
2.27.0
maintainer
#2 lore raw
In reply to [email protected]
Quoted 31 lines · On Mon, Jan 11, 2021 at 02:09:04PM -0800, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis reports this problem

mos7720.c:352:2: warning: Undefined or garbage value returned to caller

        return d;
        ^~~~~~~~

In the parport_mos7715_read_data()'s call to read_mos_reg(), 'd' is only set after the alloc block.

	buf = kmalloc(1, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

Although the problem is reported in parport_most7715_read_data(), none of the callee's of read_mos_reg() check the return status.

So move the clearing of data to before the malloc.

Fixes 0d130367abf5 ("USB: serial: mos7720: fix control-message error handling")
---
 drivers/usb/serial/mos7720.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/serial/mos7720.c +0 -0
index 41ee2984a0df..23e8162c768b 100644

drivers/usb/serial/mos7720.c static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, · line 220

Quoted 7 lines
drivers/usb/serial/mos7720.c +1 -0
@@ -214,6 +214,7 @@ static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 	u8 *buf;
 	int status;
 
+	*data = 0;
 	buf = kmalloc(1, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;

I added a clearing of the buffer to this error path instead to avoid the redundant assignment for every call due to something which will basically never happen.

drivers/usb/serial/mos7720.c static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, · line 233

Quoted 8 lines
drivers/usb/serial/mos7720.c +0 -1
@@ -227,7 +228,6 @@ static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 			"mos7720: usb_control_msg() failed: %d\n", status);
 		if (status >= 0)
 			status = -EIO;
-		*data = 0;
 	}
 
 	kfree(buf);

Johan