can: ucan: use strscpy() to instead of strncpy()
authorXu Panda <xu.panda@zte.com.cn>
Wed, 7 Dec 2022 01:09:09 +0000 (09:09 +0800)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Mon, 12 Dec 2022 10:41:24 +0000 (11:41 +0100)
The implementation of strscpy() is more robust and safer.
That's now the recommended way to copy NUL terminated strings.

Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
Signed-off-by: Yang Yang <yang.yang29@zte.com>
Link: https://lore.kernel.org/all/202212070909095189693@zte.com.cn
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/usb/ucan.c

index ffa38f5..159e25f 100644 (file)
@@ -1534,9 +1534,8 @@ static int ucan_probe(struct usb_interface *intf,
                                     sizeof(union ucan_ctl_payload));
        if (ret > 0) {
                /* copy string while ensuring zero termination */
-               strncpy(firmware_str, up->ctl_msg_buffer->raw,
-                       sizeof(union ucan_ctl_payload));
-               firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
+               strscpy(firmware_str, up->ctl_msg_buffer->raw,
+                       sizeof(union ucan_ctl_payload) + 1);
        } else {
                strcpy(firmware_str, "unknown");
        }