From 7c0763eca3f8cea364fb1ead3fb66722be6fe6af Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Fri, 20 Jul 2018 01:30:24 +0100 Subject: [PATCH] usbip: Fix misuse of strncpy() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit gcc 8 reports: usbip_device_driver.c: In function ‘read_usb_vudc_device’: usbip_device_driver.c:106:2: error: ‘strncpy’ specified bound 256 equals destination size [-Werror=stringop-truncation] strncpy(dev->path, path, SYSFS_PATH_MAX); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ usbip_device_driver.c:125:2: error: ‘strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation] strncpy(dev->busid, name, SYSFS_BUS_ID_SIZE); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I'm not convinced it makes sense to truncate the copied strings here, but since we're already doing so let's ensure they're still null- terminated. We can't easily use strlcpy() here, so use snprintf(). usbip_common.c has the same problem. Signed-off-by: Ben Hutchings Gbp-Pq: Topic bugfix/all Gbp-Pq: Name usbip-fix-misuse-of-strncpy.patch --- tools/usb/usbip/libsrc/usbip_common.c | 4 ++-- tools/usb/usbip/libsrc/usbip_device_driver.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/usb/usbip/libsrc/usbip_common.c b/tools/usb/usbip/libsrc/usbip_common.c index bb424638d75..03246c9aea2 100644 --- a/tools/usb/usbip/libsrc/usbip_common.c +++ b/tools/usb/usbip/libsrc/usbip_common.c @@ -226,8 +226,8 @@ int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev) path = udev_device_get_syspath(sdev); name = udev_device_get_sysname(sdev); - strncpy(udev->path, path, SYSFS_PATH_MAX); - strncpy(udev->busid, name, SYSFS_BUS_ID_SIZE); + snprintf(udev->path, SYSFS_PATH_MAX, "%s", path); + snprintf(udev->busid, SYSFS_BUS_ID_SIZE, "%s", name); sscanf(name, "%u-%u", &busnum, &devnum); udev->busnum = busnum; diff --git a/tools/usb/usbip/libsrc/usbip_device_driver.c b/tools/usb/usbip/libsrc/usbip_device_driver.c index 5a3726eb44a..5e234cd0570 100644 --- a/tools/usb/usbip/libsrc/usbip_device_driver.c +++ b/tools/usb/usbip/libsrc/usbip_device_driver.c @@ -91,7 +91,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) copy_descr_attr16(dev, &descr, idProduct); copy_descr_attr16(dev, &descr, bcdDevice); - strncpy(dev->path, path, SYSFS_PATH_MAX); + snprintf(dev->path, SYSFS_PATH_MAX, "%s", path); dev->speed = USB_SPEED_UNKNOWN; speed = udev_device_get_sysattr_value(sdev, "current_speed"); @@ -110,7 +110,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev) dev->busnum = 0; name = udev_device_get_sysname(plat); - strncpy(dev->busid, name, SYSFS_BUS_ID_SIZE); + snprintf(dev->busid, SYSFS_BUS_ID_SIZE, "%s", name); return 0; err: fclose(fd); -- 2.30.2