From: David Laight <david.laight.linux@gmail.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: Thorsten Blum <blum@kernel.org>, Jaroslav Kysela <perex@perex.cz>,
Takashi Iwai <tiwai@suse.com>, Cen Zhang <zzzccc427@gmail.com>,
Cezary Rojewski <cezary.rojewski@intel.com>,
bui duc phuc <phucduc.bui@gmail.com>,
linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ALSA: info: Use snprintf() in snd_info_card_create()
Date: Sun, 8 Nov 2026 21:17:42 +0000 [thread overview]
Message-ID: <20261108211742.18e4fcf9@pumpkin> (raw)
In-Reply-To: <87ld74yc19.wl-tiwai@suse.de>
On Sat, 07 Nov 2026 15:02:58 +0100
Takashi Iwai <tiwai@suse.de> wrote:
> 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().
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
>
>
> thanks,
>
> Takashi
>
prev parent reply other threads:[~2026-11-08 21:17 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-07 0:43 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
2026-11-08 21:17 ` David Laight [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261108211742.18e4fcf9@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=blum@kernel.org \
--cc=cezary.rojewski@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=phucduc.bui@gmail.com \
--cc=tiwai@suse.com \
--cc=tiwai@suse.de \
--cc=zzzccc427@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®