Skip to contentSkip to conversation

USB: gadget: Replace deprecated strncpy() with strscpy()

PATCH In review Thorsten Blum 2 participants 2 messages Mar 20, 2025 – Apr 7, 2025 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: gadget: Replace deprecated strncpy() with strscpy()

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead. Since kzalloc() already zeroes out the destination buffer, the potential NUL-padding by strncpy() is unnecessary. strscpy() copies only the required characters and guarantees NUL-termination.

Since the destination buffer has a fixed length, strscpy() automatically determines its size using sizeof() when the argument is omitted. This makes an explicit sizeof() call unnecessary.

The source string is also NUL-terminated and meets the __must_be_cstr() requirement of strscpy().

No functional changes intended.

---
 drivers/usb/gadget/legacy/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/legacy/inode.c +1 -1
index b6a30d88a800..fcce84a726f2 100644
@@ -1615,7 +1615,7 @@ static int activate_ep_files (struct dev_data *dev)
 		mutex_init(&data->lock);
 		init_waitqueue_head (&data->wait);
 
-		strncpy (data->name, ep->name, sizeof (data->name) - 1);
+		strscpy(data->name, ep->name);
 		refcount_set (&data->count, 1);
 		data->dev = dev;
 		get_dev (dev);
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead. Since kzalloc() already zeroes out the destination buffer, the potential NUL-padding by strncpy() is unnecessary. strscpy() copies only the required characters and guarantees NUL-termination.

Looks right to me; thanks for the details!

Quoted 5 lines

Since the destination buffer has a fixed length, strscpy() automatically determines its size using sizeof() when the argument is omitted. This makes an explicit sizeof() call unnecessary.

Quoted 8 lines

The source string is also NUL-terminated and meets the __must_be_cstr() requirement of strscpy().

No functional changes intended.

Quoted 17 lines
---
 drivers/usb/gadget/legacy/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/legacy/inode.c +1 -1
index b6a30d88a800..fcce84a726f2 100644
@@ -1615,7 +1615,7 @@ static int activate_ep_files (struct dev_data *dev)
 		mutex_init(&data->lock);
 		init_waitqueue_head (&data->wait);
 
-		strncpy (data->name, ep->name, sizeof (data->name) - 1);
+		strscpy(data->name, ep->name);
 		refcount_set (&data->count, 1);
 		data->dev = dev;
 		get_dev (dev);
Signature · 1 line
Kees Cook