Skip to content

Commit a6eef67

Browse files
lag-linarogregkh
authored andcommitted
usb: host: max3421-hcd: Replace snprintf() with the safer scnprintf() variant
There is a general misunderstanding amongst engineers that {v}snprintf() returns the length of the data *actually* encoded into the destination array. However, as per the C99 standard {v}snprintf() really returns the length of the data that *would have been* written if there were enough space for it. This misunderstanding has led to buffer-overruns in the past. It's generally considered safer to use the {v}scnprintf() variants in their place (or even sprintf() in simple cases). So let's do that. Link: https://lwn.net/Articles/69419/ Link: KSPP/linux#105 Cc: Cristian Birsan <[email protected]> Cc: Nicolas Ferre <[email protected]> Cc: Alexandre Belloni <[email protected]> Cc: Claudiu Beznea <[email protected]> Cc: <[email protected]> Signed-off-by: Lee Jones <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 01dc7f7 commit a6eef67

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

drivers/usb/host/max3421-hcd.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,12 +1158,12 @@ dump_eps(struct usb_hcd *hcd)
11581158
end = dp + sizeof(ubuf);
11591159
*dp = '\0';
11601160
list_for_each_entry(urb, &ep->urb_list, urb_list) {
1161-
ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1162-
usb_pipetype(urb->pipe),
1163-
usb_urb_dir_in(urb) ? "IN" : "OUT",
1164-
urb->actual_length,
1165-
urb->transfer_buffer_length);
1166-
if (ret < 0 || ret >= end - dp)
1161+
ret = scnprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1162+
usb_pipetype(urb->pipe),
1163+
usb_urb_dir_in(urb) ? "IN" : "OUT",
1164+
urb->actual_length,
1165+
urb->transfer_buffer_length);
1166+
if (ret == end - dp - 1)
11671167
break; /* error or buffer full */
11681168
dp += ret;
11691169
}
@@ -1255,9 +1255,9 @@ max3421_handle_irqs(struct usb_hcd *hcd)
12551255
end = sbuf + sizeof(sbuf);
12561256
*dp = '\0';
12571257
for (i = 0; i < 16; ++i) {
1258-
int ret = snprintf(dp, end - dp, " %lu",
1259-
max3421_hcd->err_stat[i]);
1260-
if (ret < 0 || ret >= end - dp)
1258+
int ret = scnprintf(dp, end - dp, " %lu",
1259+
max3421_hcd->err_stat[i]);
1260+
if (ret == end - dp - 1)
12611261
break; /* error or buffer full */
12621262
dp += ret;
12631263
}

0 commit comments

Comments
 (0)