ALSA: info: Use snprintf() in snd_info_card_create()
PATCH 1/1 ALSA: info: Use snprintf() in snd_info_card_create()
While the current code works correctly, replace the unbounded sprintf() with snprintf() to bound the formatted string to the str buffer.
--- sound/core/info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/info.c +1 -1
@@ -484,7 +484,7 @@ int snd_info_card_create(struct snd_card *card)if (snd_BUG_ON (!card))return -ENXIO ; -sprintf (str,"card%i" , card->number); +snprintf (str,sizeof (str),"card%i" , card->number); entry =create_subdir (card->module, str);if (!entry)return -ENOMEM ;
Quoted 5 lines · Thorsten Blum
While the current code works correctly, replace the unbounded
sprintf()withsnprintf()to bound the formatted string to the str buffer.Signed-off-by Thorsten Blum <[email protected]>
Shouldn't we use scnprintf() instead? Otherwise it may lead to a compiler warning.
thanks,
Takashi
Quoted 17 lines
--- sound/core/info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)sound/core/info.c +1
-1@@ -484,7 +484,7 @@ int snd_info_card_create(struct snd_card *card)if (snd_BUG_ON (!card))return -ENXIO ; -sprintf (str,"card%i" , card->number); +snprintf (str,sizeof (str),"card%i" , card->number); entry =create_subdir (card->module, str);if (!entry)return -ENOMEM ;
Quoted 10 lines · Takashi Iwai
Quoted 5 lines · Thorsten Blum
While the current code works correctly, replace the unbounded
sprintf()withsnprintf()to bound the formatted string to the str buffer.Signed-off-by Thorsten Blum <[email protected]>Shouldn't we use
scnprintf()instead? Otherwise it may lead to a compiler warning.
The only difference between snprintf() and scnprintf() is the return value, which is unused here. Both are correct and shouldn't trigger a warning.
Quoted 8 lines · Thorsten Blum
Quoted 6 lines · Takashi Iwai
Quoted 4 lines · Thorsten Blum
While the current code works correctly, replace the unbounded
sprintf()withsnprintf()to bound the formatted string to the str buffer.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Thorsten Blum <[email protected]>Shouldn't we use
scnprintf()instead? Otherwise it may lead to a compiler warning.The only difference between
snprintf()andscnprintf()is the return value, which is unused here. Both are correct and shouldn't trigger a warning.
Well, not really. For this particular patch, it doesn't make difference, but in some cases (like the use with %s), snprintf() causes a spurious compile warning with W=1. e.g. try below.
Takashi
sound/core/ump.c +1 -1
@@ -1369,7 +1369,7 @@ static void update_legacy_substreams(struct snd_ump_endpoint *ump, name = ump->groups[idx].name;if (!*name) name = ump->core.name; -scnprintf (s->name,sizeof (s->name),"Group %d (%.16s)%s" , +snprintf (s->name,sizeof (s->name),"Group %d (%.16s)%s" , idx +1 , name, ump->groups[idx].active ?"" :" [Inactive]" ); s->inactive = !ump->groups[idx].active;
Quoted 15 lines · Takashi Iwai
Quoted 13 lines · Thorsten Blum
Quoted 10 lines · Takashi Iwai
Quoted 5 lines · Thorsten Blum
While the current code works correctly, replace the unbounded
sprintf()withsnprintf()to bound the formatted string to the str buffer.Signed-off-by Thorsten Blum <[email protected]>Shouldn't we use
scnprintf()instead? Otherwise it may lead to a compiler warning.
Quoted 7 lines
Quoted 3 lines
The only difference between
snprintf()andscnprintf()is the return value, which is unused here. Both are correct and shouldn't trigger a warning.Well, not really. For this particular patch, it doesn't make difference, but in some cases (like the use with %s),
snprintf()causes a spurious compile warning with W=1. e.g. try below.
Agreed, although that's a different use case from my patch.
I don't see a warning when compiling my patch with W=1, but I'm happy to switch to scnprintf() if you prefer.
Quoted 21 lines · Thorsten Blum
Quoted 19 lines · Takashi Iwai
Quoted 16 lines · Thorsten Blum
Quoted 10 lines · Takashi Iwai
Quoted 5 lines · Thorsten Blum
While the current code works correctly, replace the unbounded
sprintf()withsnprintf()to bound the formatted string to the str buffer.Signed-off-by Thorsten Blum <[email protected]>Shouldn't we use
scnprintf()instead? Otherwise it may lead to a compiler warning.The only difference between
snprintf()andscnprintf()is the return value, which is unused here. Both are correct and shouldn't trigger a warning.
Quoted 5 lines
Quoted 3 lines
Well, not really. For this particular patch, it doesn't make difference, but in some cases (like the use with %s),
snprintf()causes a spurious compile warning with W=1. e.g. try below.Agreed, although that's a different use case from my patch.
Yep. I guess a warning is triggered only when filling a substring that can be large into the fixed size buffer for now, but it's a compiler's thing, that can change at any time in future...
Quoted 2 lines
I don't see a warning when compiling my patch with W=1, but I'm happy to switch to
scnprintf()if you prefer.
I'd like to bet for a safer way, so let's go with scnprintf().
thanks,
Takashi
Quoted 29 lines · On Sat, 07 Nov 2026 15:02:58 +0100 Takashi Iwai <[email protected]> wrote:
Quoted 21 lines · Thorsten Blum
Quoted 19 lines · Takashi Iwai
Quoted 16 lines · Thorsten Blum
Quoted 10 lines · Takashi Iwai
Quoted 5 lines · Thorsten Blum
While the current code works correctly, replace the unbounded
sprintf()withsnprintf()to bound the formatted string to the str buffer.Signed-off-by Thorsten Blum <[email protected]>Shouldn't we use
scnprintf()instead? Otherwise it may lead to a compiler warning.The only difference between
snprintf()andscnprintf()is the return value, which is unused here. Both are correct and shouldn't trigger a warning.Quoted 5 lines
Quoted 3 lines
Well, not really. For this particular patch, it doesn't make difference, but in some cases (like the use with %s),
snprintf()causes a spurious compile warning with W=1. e.g. try below.Agreed, although that's a different use case from my patch.
Quoted 8 lines
Yep. I guess a warning is triggered only when filling a substring that can be large into the fixed size buffer for now, but it's a compiler's thing, that can change at any time in future...
Quoted 2 lines
I don't see a warning when compiling my patch with W=1, but I'm happy to switch to
scnprintf()if you prefer.I'd like to bet for a safer way, so let's go with
scnprintf().
At some point someone will work out why scnprintf() doesn't generate the error.
OTOH there really ought to be a way of telling gcc that the overflow is a false positive or you really don't care.
Doing some checks with the result stops the warning, but a (void) cast doesn't - much like it doesn't stop a 'must_check' error.
The real this is it that it is somewhat pointless.
It basically assumes that any char[] might be its maximum length and any integer its maximum value (%d needs 10 characters).
But it can't report for 'char *' because it doesn't know the length. I can really imagine people breaking code in all sorts of ways while trying to stop the warning.
In particular code might start trying to 'exactly size' strings instead of using ones that are 'really far too big' (an on-stack char[4096] really doesn't matter in userspace) and then getting actual overflows (or memory leaks) for normal data.
David
Quoted 6 lines
thanks,
Takashi