mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: David Laight <david.laight.linux@gmail.com>
Cc: kernel test robot <lkp@intel.com>,
	Eddie James <eajames@linux.ibm.com>,
	oe-kbuild-all@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: drivers/hwmon/pmbus/ibm-cffps.c:184:60: warning: '%04X' directive output may be truncated writing between 4 and 8 bytes into a region of size 5
Date: Fri, 20 Dec 2024 09:56:26 -0800	[thread overview]
Message-ID: <ddf5066a-8bf3-4686-a39d-a921452c41fb@roeck-us.net> (raw)
In-Reply-To: <20241220172920.7a34eb46@dsl-u17-10>

On 12/20/24 09:29, David Laight wrote:
> On Fri, 20 Dec 2024 09:03:51 -0800
> Guenter Roeck <linux@roeck-us.net> wrote:
> 
>> On 12/20/24 08:30, David Laight wrote:
> ...
>>>>   
>>>>>       drivers/hwmon/pmbus/ibm-cffps.c:171:60: warning: '%02X' directive output may be truncated writing between 2 and 8 bytes into a region of size 3 [-Wformat-truncation=]
>>>>>         171 |                                 snprintf(&data[i * 2], 3, "%02X", rc);
>>>
>>> The only ways I know are to bound check the value or to use
>>> OPTIMISER_HIDE_VAR() on the length.

Actually, that doesn't work.

-                               snprintf(&data[i * 2], 3, "%02X", rc);
+                               snprintf(&data[i * 2], OPTIMIZER_HIDE_VAR(3), "%02X", rc);

triggers:

drivers/hwmon/pmbus/ibm-cffps.c: In function ‘ibm_cffps_debugfs_read’:
./include/linux/compiler.h:186:9: error: expected expression before ‘__asm__’

>>>    
>>
>> -                               snprintf(&data[i * 2], 3, "%02X", rc);
>> +                               snprintf(&data[i * 2], 3, "%02X", rc & 0xff);
>>
>> works as well, at least with gcc 11 and 12, but I really dislike that kind of nonsense.
> 
> Ditto.
> Using the result in some ways can also remove the warning.
> But you have to try quite hard, a simple (void) really ought to be
> enough to show you 'just don't care' but even 'if (snprintf(...)) {}'
> isn't enough (it does silence 'warn-unused-result').
> 
> I mean, the whole point of snprintf() is that it truncates.
> 

Using scnprintf() "fixes" the problem as well, but there are other patches
suggesting that snprintf() should be used if the return value is not
needed/checked because it is slightly simpler.

Guenter


  reply	other threads:[~2024-12-20 17:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-20 13:54 kernel test robot
2024-12-20 15:21 ` Guenter Roeck
2024-12-20 16:30   ` David Laight
2024-12-20 17:03     ` Guenter Roeck
2024-12-20 17:29       ` David Laight
2024-12-20 17:56         ` Guenter Roeck [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-01-25  3:52 kernel test robot
2023-12-25 21:18 kernel test robot

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=ddf5066a-8bf3-4686-a39d-a921452c41fb@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=david.laight.linux@gmail.com \
    --cc=eajames@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=oe-kbuild-all@lists.linux.dev \
    /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®