USB: gadget: Replace deprecated strncpy() with strscpy()
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
@@ -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);
Quoted 4 lines · Thorsten Blum
strncpy()is deprecated for NUL-terminated destination buffers; usestrscpy()instead. Sincekzalloc()already zeroes out the destination buffer, the potential NUL-padding bystrncpy()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 usingsizeof()when the argument is omitted. This makes an explicitsizeof()call unnecessary.
Quoted 8 lines
The source string is also NUL-terminated and meets the
__must_be_cstr()requirement ofstrscpy().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@@ -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