Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
From: Takashi Iwai
Date: Sat Nov 07 2026 - 08:45:09 EST
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@xxxxxxxxxx>
> >
> > 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;