mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
@ 2026-11-07  0:43 Thorsten Blum
  2026-11-07  9:14 ` Takashi Iwai
  0 siblings, 1 reply; 6+ messages in thread
From: Thorsten Blum @ 2026-11-07  0:43 UTC (permalink / raw)
  To: Jaroslav Kysela, Takashi Iwai, Cen Zhang, Cezary Rojewski, bui duc phuc
  Cc: Thorsten Blum, Takashi Iwai, linux-sound, linux-kernel

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

Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 sound/core/info.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/core/info.c b/sound/core/info.c
index 023b4736aa6d..b0ed66d0336d 100644
--- a/sound/core/info.c
+++ b/sound/core/info.c
@@ -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;

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
  2026-11-07  0:43 [PATCH] ALSA: info: Use snprintf() in snd_info_card_create() Thorsten Blum
@ 2026-11-07  9:14 ` Takashi Iwai
  2026-11-07 13:04   ` Thorsten Blum
  0 siblings, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-11-07  9:14 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Jaroslav Kysela, Takashi Iwai, Cen Zhang, Cezary Rojewski,
	bui duc phuc, Takashi Iwai, linux-sound, linux-kernel

On Sat, 07 Nov 2026 01:43:37 +0100,
Thorsten Blum wrote:
> 
> While the current code works correctly, replace the unbounded sprintf()
> with snprintf() to bound the formatted string to the str buffer.
> 
> Signed-off-by: Thorsten Blum <blum@kernel.org>

Shouldn't we use scnprintf() instead?  Otherwise it may lead to a
compiler warning.


thanks,

Takashi

> ---
>  sound/core/info.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/sound/core/info.c b/sound/core/info.c
> index 023b4736aa6d..b0ed66d0336d 100644
> --- a/sound/core/info.c
> +++ b/sound/core/info.c
> @@ -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;

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
  2026-11-07  9:14 ` Takashi Iwai
@ 2026-11-07 13:04   ` Thorsten Blum
  2026-11-07 13:44     ` Takashi Iwai
  0 siblings, 1 reply; 6+ messages in thread
From: Thorsten Blum @ 2026-11-07 13:04 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Jaroslav Kysela, Takashi Iwai, Cen Zhang, Cezary Rojewski,
	bui duc phuc, linux-sound, linux-kernel

On Sat, Nov 07, 2026 at 10:14:38AM +0100, Takashi Iwai wrote:
> On Sat, 07 Nov 2026 01:43:37 +0100,
> Thorsten Blum wrote:
> > 
> > While the current code works correctly, replace the unbounded sprintf()
> > with snprintf() to bound the formatted string to the str buffer.
> > 
> > Signed-off-by: Thorsten Blum <blum@kernel.org>
> 
> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
  2026-11-07 13:04   ` Thorsten Blum
@ 2026-11-07 13:44     ` Takashi Iwai
  2026-11-07 13:57       ` Thorsten Blum
  0 siblings, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-11-07 13:44 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Takashi Iwai, Jaroslav Kysela, Takashi Iwai, Cen Zhang,
	Cezary Rojewski, bui duc phuc, linux-sound, linux-kernel

On Sat, 07 Nov 2026 14:04:33 +0100,
Thorsten Blum wrote:
> 
> On Sat, Nov 07, 2026 at 10:14:38AM +0100, Takashi Iwai wrote:
> > On Sat, 07 Nov 2026 01:43:37 +0100,
> > Thorsten Blum wrote:
> > > 
> > > While the current code works correctly, replace the unbounded sprintf()
> > > with snprintf() to bound the formatted string to the str buffer.
> > > 
> > > Signed-off-by: Thorsten Blum <blum@kernel.org>
> > 
> > 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

--- a/sound/core/ump.c
+++ b/sound/core/ump.c
@@ -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;


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
  2026-11-07 13:44     ` Takashi Iwai
@ 2026-11-07 13:57       ` Thorsten Blum
  2026-11-07 14:02         ` Takashi Iwai
  0 siblings, 1 reply; 6+ messages in thread
From: Thorsten Blum @ 2026-11-07 13:57 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Jaroslav Kysela, Takashi Iwai, Cen Zhang, Cezary Rojewski,
	bui duc phuc, linux-sound, linux-kernel

On Sat, Nov 07, 2026 at 02:44:57PM +0100, Takashi Iwai wrote:
> On Sat, 07 Nov 2026 14:04:33 +0100,
> Thorsten Blum wrote:
> > 
> > On Sat, Nov 07, 2026 at 10:14:38AM +0100, Takashi Iwai wrote:
> > > On Sat, 07 Nov 2026 01:43:37 +0100,
> > > Thorsten Blum wrote:
> > > > 
> > > > While the current code works correctly, replace the unbounded sprintf()
> > > > with snprintf() to bound the formatted string to the str buffer.
> > > > 
> > > > Signed-off-by: Thorsten Blum <blum@kernel.org>
> > > 
> > > 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.

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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
  2026-11-07 13:57       ` Thorsten Blum
@ 2026-11-07 14:02         ` Takashi Iwai
  0 siblings, 0 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-07 14:02 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Takashi Iwai, Jaroslav Kysela, Takashi Iwai, Cen Zhang,
	Cezary Rojewski, bui duc phuc, linux-sound, linux-kernel

On Sat, 07 Nov 2026 14:57:09 +0100,
Thorsten Blum wrote:
> 
> On Sat, Nov 07, 2026 at 02:44:57PM +0100, Takashi Iwai wrote:
> > On Sat, 07 Nov 2026 14:04:33 +0100,
> > Thorsten Blum wrote:
> > > 
> > > On Sat, Nov 07, 2026 at 10:14:38AM +0100, Takashi Iwai wrote:
> > > > On Sat, 07 Nov 2026 01:43:37 +0100,
> > > > Thorsten Blum wrote:
> > > > > 
> > > > > While the current code works correctly, replace the unbounded sprintf()
> > > > > with snprintf() to bound the formatted string to the str buffer.
> > > > > 
> > > > > Signed-off-by: Thorsten Blum <blum@kernel.org>
> > > > 
> > > > 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.
> 
> 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...

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-07 14:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-07  0:43 [PATCH] ALSA: info: Use snprintf() in snd_info_card_create() Thorsten Blum
2026-11-07  9:14 ` Takashi Iwai
2026-11-07 13:04   ` Thorsten Blum
2026-11-07 13:44     ` Takashi Iwai
2026-11-07 13:57       ` Thorsten Blum
2026-11-07 14:02         ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®