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