Skip to contentSkip to conversation

USB: keyspan: add a sanity test on "len"

PATCH In review Dan Carpenter 2 participants 2 messages Apr 5, 2013

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

PATCH v2 2/2 USB: keyspan: add a sanity test on "len"

"len" comes from the USB transfer and it's probably correct. The thing is that we already have similar checks like:

        if (data[i] >= serial->num_ports) {

So adding a sanity test here matches the rest of the code and is a good idea.

---
v2: No change. Rebased.

drivers/usb/serial/keyspan.c +3 -2
index 3d303f0..eb30d7b 100644
@@ -727,14 +727,15 @@ static void usa49wg_indat_callback(struct urb *urb)
 		if ((data[i] & 0x80) = 0) {
 			/* no error on any byte */
 			i++;
-			for (x = 1; x < len ; ++x)
+			for (x = 1; x < len && i < urb->actual_length; ++x)
 				tty_insert_flip_char(&port->port,
 						data[i++], 0);
 		} else {
 			/*
 			 * some bytes had errors, every byte has status
 			 */
-			for (x = 0; x + 1 < len; x += 2) {
+			for (x = 0; x + 1 < len &&
+				    i + 1 < urb->actual_length; x += 2) {
 				int stat = data[i], flag = 0;
 
 				if (stat & RXERROR_OVERRUN)
#1 lore raw

Am 05.04.2013 07:43, schrieb Dan Carpenter:

Quoted 17 lines

"len" comes from the USB transfer and it's probably correct. The thing is that we already have similar checks like:

        if (data[i] >= serial->num_ports) {

So adding a sanity test here matches the rest of the code and is a good idea.

---
v2: No change. Rebased.

drivers/usb/serial/keyspan.c +0 -0
index 3d303f0..eb30d7b 100644

drivers/usb/serial/keyspan.c static void usa49wg_indat_callback(struct urb *urb) · line 732

Quoted 7 lines
drivers/usb/serial/keyspan.c +1 -1
@@ -727,14 +727,15 @@ static void usa49wg_indat_callback(struct urb *urb)
 		if ((data[i] & 0x80) = 0) {
 			/* no error on any byte */
 			i++;
-			for (x = 1; x < len ; ++x)
+			for (x = 1; x < len && i < urb->actual_length; ++x)
 				tty_insert_flip_char(&port->port,
 						data[i++], 0);

maybe is is a bit late ...
mixing i,x here is prone to error, the casual reader will be confused. i suggest the following variation.
for (x = 1; x < len && i < urb->actual_length; ++x)

	if (i < urb->actual_length)

tty_insert_flip_char(&port->port,ata[i++], 0);

drivers/usb/serial/keyspan.c static void usa49wg_indat_callback(struct urb *urb) · line 741

Quoted 10 lines
drivers/usb/serial/keyspan.c +2 -1
 		} else {
 			/*
 			 * some bytes had errors, every byte has status
 			 */
-			for (x = 0; x + 1 < len; x += 2) {
+			for (x = 0; x + 1 < len &&
+				    i + 1 < urb->actual_length; x += 2) {
 				int stat = data[i], flag = 0;
 
 				if (stat & RXERROR_OVERRUN)

same as above.

re,
wh

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html