Re: [PATCH v1 1/1] thermal: sysfs: switch to use scnprintf() to suppress truncation warning

From: Andy Shevchenko

Date: Mon Sep 07 2026 - 00:24:06 EST


On Sat, Sep 05, 2026 at 10:58:32AM +0100, David Laight wrote:
> On Mon, 17 Aug 2026 12:33:24 +0200
> Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> > Switch the sysfs code to use scnprintf() to avoid warnings about potential
> > truncation of the names of the sysfs attributes.
>
> It has to be a 'bug' that 'format overflow' warnings are generated for
> snprintf() but not for scnprintf().
> They really shouldn't behave differently.
>
> I did some tests a while back and the warning goes away if you do
> 'some things' with the result (like comparing against the buffer size),
> but even explicitly ignoring it with a (void) cast isn't enough.
>
> Using OPTIMIZER_HIDE_VAR() (or an _VAL variant) on the buffer size does
> stop the error, but I expect gcc to start checking sizeof(buf) as well.
> So both arguments might need hiding to get the warning ignored.

Yeah, but in a few subsystems (mostly visible in 'input') the technique of
using scnprintf() is used when either it's and ABI or we don't care about
possible truncation.

--
With Best Regards,
Andy Shevchenko