Skip to content

Commit 0466e7e

Browse files
lag-linarogregkh
authored andcommitted
usb: gadget: configfs: Replace snprintf() with the safer scnprintf() variant
There is a general misunderstanding amongst engineers that {v}snprintf() returns the length of the data *actually* encoded into the destination array. However, as per the C99 standard {v}snprintf() really returns the length of the data that *would have been* written if there were enough space for it. This misunderstanding has led to buffer-overruns in the past. It's generally considered safer to use the {v}scnprintf() variants in their place (or even sprintf() in simple cases). So let's do that. Link: https://lwn.net/Articles/69419/ Link: KSPP/linux#105 Signed-off-by: Lee Jones <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b8fb6db commit 0466e7e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

drivers/usb/gadget/configfs.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,11 @@ static struct config_group *function_make(
606606
char *instance_name;
607607
int ret;
608608

609-
ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
610-
if (ret >= MAX_NAME_LEN)
609+
if (strlen(name) >= MAX_NAME_LEN)
611610
return ERR_PTR(-ENAMETOOLONG);
612611

612+
scnprintf(buf, MAX_NAME_LEN, "%s", name);
613+
613614
func_name = buf;
614615
instance_name = strchr(func_name, '.');
615616
if (!instance_name) {
@@ -701,10 +702,12 @@ static struct config_group *config_desc_make(
701702
int ret;
702703

703704
gi = container_of(group, struct gadget_info, configs_group);
704-
ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
705-
if (ret >= MAX_NAME_LEN)
705+
706+
if (strlen(name) >= MAX_NAME_LEN)
706707
return ERR_PTR(-ENAMETOOLONG);
707708

709+
scnprintf(buf, MAX_NAME_LEN, "%s", name);
710+
708711
num_str = strchr(buf, '.');
709712
if (!num_str) {
710713
pr_err("Unable to locate . in name.bConfigurationValue\n");

0 commit comments

Comments
 (0)