Skip to content

Commit 60034e0

Browse files
lag-linarogregkh
authored andcommitted
usb: gadget: f_uac2: 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 Cc: James Gruber <[email protected]> Cc: Yadwinder Singh <[email protected]> Cc: Jaswinder Singh <[email protected]> Cc: Ruslan Bilovol <[email protected]> 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 c1a3718 commit 60034e0

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/usb/gadget/function/f_uac2.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
20452045
int result; \
20462046
\
20472047
mutex_lock(&opts->lock); \
2048-
result = snprintf(page, sizeof(opts->name), "%s", opts->name); \
2048+
result = scnprintf(page, sizeof(opts->name), "%s", opts->name); \
20492049
mutex_unlock(&opts->lock); \
20502050
\
20512051
return result; \
@@ -2063,7 +2063,7 @@ static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
20632063
goto end; \
20642064
} \
20652065
\
2066-
ret = snprintf(opts->name, min(sizeof(opts->name), len), \
2066+
ret = scnprintf(opts->name, min(sizeof(opts->name), len), \
20672067
"%s", page); \
20682068
\
20692069
end: \
@@ -2187,7 +2187,7 @@ static struct usb_function_instance *afunc_alloc_inst(void)
21872187
opts->req_number = UAC2_DEF_REQ_NUM;
21882188
opts->fb_max = FBACK_FAST_MAX;
21892189

2190-
snprintf(opts->function_name, sizeof(opts->function_name), "Source/Sink");
2190+
scnprintf(opts->function_name, sizeof(opts->function_name), "Source/Sink");
21912191

21922192
opts->p_terminal_type = UAC2_DEF_P_TERM_TYPE;
21932193
opts->c_terminal_type = UAC2_DEF_C_TERM_TYPE;

0 commit comments

Comments
 (0)