Skip to contentSkip to conversation

ALSA: info: Use snprintf() in snd_info_card_create()

PATCH Superseded Thorsten Blum 3 participants 7 messages Nov 7 – Nov 8 Sound

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 023b4736aa6d..b0ed66d0336d 100644
@@ -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;
#1 lore raw
Quoted 5 lines · Thorsten Blum

While the current code works correctly, replace the unbounded sprintf() with snprintf() to bound the formatted string to the str buffer.

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
index 023b4736aa6d..b0ed66d0336d 100644
@@ -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;
#2 lore raw
Quoted 10 lines · Takashi Iwai
Quoted 5 lines · Thorsten Blum

While the current code works correctly, replace the unbounded sprintf() with snprintf() to bound the formatted string to the str buffer.

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.

#3 lore raw
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() with snprintf() to bound the formatted string to the str buffer.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

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.

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;
#4 lore raw
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() with snprintf() to bound the formatted string to the str buffer.

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() and scnprintf() 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.

#5 lore raw
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() with snprintf() to bound the formatted string to the str buffer.

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 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

#6 lore raw
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() with snprintf() to bound the formatted string to the str buffer.

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 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