Skip to contentSkip to conversation

usbip: Fix misuse of strncpy()

DISCUSSION Shuah Khan 1 participant 1 message Aug 9, 2018 USB

lore
Share & tools
lore

Shuah Khan

author
#0 lore raw
Quoted 43 lines · Ben Hutchings
Quoted 30 lines · Shuah Khan
Quoted 29 lines · Ben Hutchings

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.

---
 tools/usb/usbip/libsrc/usbip_common.c        |    4 ++--
 tools/usb/usbip/libsrc/usbip_device_driver.c |    4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/usb/usbip/libsrc/usbip_common.c +0 -0
@@ -226,8 +226,8 @@ int read_usb_device(struct udev_device *
 	path = udev_device_get_syspath(sdev);
 	name = udev_device_get_sysname(sdev);
 
Quoted 8 lines

tools/usb/usbip/libsrc/usbip_common.c int read_usb_device(struct udev_device * · line 230

Quoted 4 lines
tools/usb/usbip/libsrc/usbip_common.c +2 -2
-	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);

I am okay with the change to use snprintf(). Please add check for return to avoid GCC 7 -Wformat-overflow wanrs on snprintf instances that don't check return.

[...]

I've tried running:

Quoted 8 lines

./autogen.sh
CC=gcc-7 CFLAGS=-Wformat-overflow ./configure
make

but this didn't produce any warnings. How did you get the warning?

Ben.

Sorry for the delay. It has been a busy few weeks with travel both vacation and business.

Okay. Please check the e5dfa3f902b9a642ae8c6997d57d7c41e384a90b for details. I didn't see this problem myself, likely because I was using older gcc.

thanks,
-- Shuah

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