Re: drivers/platform/mellanox/mlxreg-hotplug.c:87:61: warning: '%d' directive output may be truncated writing 1 byte into a region of size between 0 and 31
From: David Laight
Date: Sat Dec 28 2024 - 08:05:29 EST
On Sat, 28 Dec 2024 10:23:25 +0800
kernel test robot <lkp@xxxxxxxxx> wrote:
(resend with fixed email address)
> Hi Vadim,
>
> FYI, the error/warning still remains.
>
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: fd0584d220fe285dc45be43eede55df89ad6a3d9
> commit: 92d020f97966b1724cfcac93e89176d0eb3aca61 platform/mellanox: mlxreg-hotplug: Add environmental data to uevent
> date: 4 years, 5 months ago
> config: sparc-randconfig-001-20241212 (https://download.01.org/0day-ci/archive/20241228/202412281022.emy8ZDNh-lkp@xxxxxxxxx/config)
> compiler: sparc-linux-gcc (GCC) 12.4.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241228/202412281022.emy8ZDNh-lkp@xxxxxxxxx/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@xxxxxxxxx>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202412281022.emy8ZDNh-lkp@xxxxxxxxx/
>
> All warnings (new ones prefixed by >>):
>
> In file included from include/linux/bits.h:23,
> from include/linux/bitops.h:5,
> from drivers/platform/mellanox/mlxreg-hotplug.c:8:
> drivers/platform/mellanox/mlxreg-hotplug.c: In function 'mlxreg_hotplug_attr_init':
> include/linux/bits.h:26:42: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits]
> 26 | __builtin_constant_p((l) > (h)), (l) > (h), 0)))
> | ^
> include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
> 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
> | ^
> include/linux/bits.h:39:10: note: in expansion of macro 'GENMASK_INPUT_CHECK'
> 39 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
This pretty much always gives a warning when -Wtype-limits is enabled.
I'm sure someone said it was being removed from 'W=1' builds.
But changing the defines to (say):
__builtin_constant_p((hi) - (lo), __GENMASK((hi) - (lo), 0) <<
(lo), __GENMASK(hi, lo)) will remove the type-limits warning and give a
'negative shift' error if (lo) > (hi).
...
> drivers/platform/mellanox/mlxreg-hotplug.c: In function 'mlxreg_hotplug_udev_event_send.isra':
> >> drivers/platform/mellanox/mlxreg-hotplug.c:87:61: warning: '%d' directive output may be truncated writing 1 byte into a region of size between 0 and 31 [-Wformat-truncation=]
> 87 | snprintf(event_str, MLXREG_CORE_LABEL_MAX_SIZE, "%s=%d", label, !!action);
> | ^~
> drivers/platform/mellanox/mlxreg-hotplug.c:87:57: note: directive argument in the range [0, 1]
> 87 | snprintf(event_str, MLXREG_CORE_LABEL_MAX_SIZE, "%s=%d", label, !!action);
> | ^~~~~~~
> drivers/platform/mellanox/mlxreg-hotplug.c:87:9: note: 'snprintf' output between 3 and 34 bytes into a destination of size 32
> 87 | snprintf(event_str, MLXREG_CORE_LABEL_MAX_SIZE, "%s=%d", label, !!action);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
The compiler is failing to note that ! returns 0 or 1!
But maybe 'action != 0' will be ok.
David