Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: core: Use sysfs_emit_at() when showing dynamic IDs

When formatting the dynamic USB device IDs to show to
the user space, instead of scnprintf() function use
sysfs_emit_at(). The functions are equivalent, but
using the latter is recommended as it ensures that
no buffer overruns occur.

Testing the change can be done by assigning new IDs
to the USB driver's sysfs attribute new_id,
and then checking that the same values are returned.

For example:

echo 4533 7515 > /sys/bus/usb/drivers/usbfs/new_id
cat /sys/bus/usb/drivers/usbfs/new_id

The output should match the assigned IDs (4533 7515).

Signed-off-by: Hanne-Lotta Mäenpää <hannelotta@gmail.com>
Link: https://lore.kernel.org/r/20250621164005.4004-1-hannelotta@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Hanne-Lotta Mäenpää and committed by
Greg Kroah-Hartman
711d41ab 8b4f6faf

+2 -2
+2 -2
drivers/usb/core/driver.c
··· 119 119 guard(mutex)(&usb_dynids_lock); 120 120 list_for_each_entry(dynid, &dynids->list, node) 121 121 if (dynid->id.bInterfaceClass != 0) 122 - count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n", 122 + count += sysfs_emit_at(&buf[count], count, "%04x %04x %02x\n", 123 123 dynid->id.idVendor, dynid->id.idProduct, 124 124 dynid->id.bInterfaceClass); 125 125 else 126 - count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n", 126 + count += sysfs_emit_at(&buf[count], count, "%04x %04x\n", 127 127 dynid->id.idVendor, dynid->id.idProduct); 128 128 return count; 129 129 }