Re: drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:489:65: warning: array subscript 'struct kvaser_cmd_ext[0]' is partly outside array bounds of 'unsigned char[32]'

From: Marc Kleine-Budde
Date: Mon Oct 31 2022 - 04:41:51 EST


On 31.10.2022 07:35:36, kernel test robot wrote:
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: 30a0b95b1335e12efef89dd78518ed3e4a71a763
> commit: 00784da3e6b8c1f0e58d813272503d014de8f64b can: kvaser_usb: kvaser_usb_hydra: Use kzalloc for allocating only one element
> date: 8 weeks ago
> config: i386-randconfig-r021-20221031
> compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
> reproduce (this is a W=1 build):
> # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=00784da3e6b8c1f0e58d813272503d014de8f64b
> git remote add linus https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> git fetch --no-tags linus master
> git checkout 00784da3e6b8c1f0e58d813272503d014de8f64b
> # save the config file
> mkdir build_dir && cp config build_dir/.config
> make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
>
> If you fix the issue, kindly add following tag where applicable
> | Reported-by: kernel test robot <lkp@xxxxxxxxx>
>
> All warnings (new ones prefixed by >>):
>
> In file included from include/linux/byteorder/little_endian.h:5,
> from arch/x86/include/uapi/asm/byteorder.h:5,
> from include/asm-generic/bitops/le.h:6,
> from arch/x86/include/asm/bitops.h:416,
> from include/linux/bitops.h:68,
> from include/linux/kernel.h:22,
> from arch/x86/include/asm/percpu.h:27,
> from arch/x86/include/asm/preempt.h:6,
> from include/linux/preempt.h:78,
> from include/linux/spinlock.h:55,
> from include/linux/swait.h:7,
> from include/linux/completion.h:12,
> from drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:16:
> drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c: In function 'kvaser_usb_hydra_send_simple_cmd':
> >> drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:489:65: warning: array subscript 'struct kvaser_cmd_ext[0]' is partly outside array bounds of 'unsigned char[32]' [-Warray-bounds]
> 489 | ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
> include/uapi/linux/byteorder/little_endian.h:37:51: note: in definition of macro '__le16_to_cpu'
> 37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
> | ^
> drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:489:23: note: in expansion of macro 'le16_to_cpu'
> 489 | ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
> | ^~~~~~~~~~~
> drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:537:15: note: referencing an object of size 32 allocated by 'kzalloc'
> 537 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I think this is a false positive.

The commit you're building 00784da3e6b8 ("can: kvaser_usb:
kvaser_usb_hydra: Use kzalloc for allocating only one element") contains
3e88445a3a5a ("can: kvaser_usb: silence a GCC 12 -Warray-bounds
warning").

In the latter commit, the -Warray-bounds warning was disabled, as gcc-12
has some problem with it. However you're using gcc-11 (Debian 11.3.0-8)
11.3.0 here. Don't know the status of gcc-11 with regards to that
warning.

In the mean time in 26117d92d001 ("can: kvaser_usb: Remove
-Warray-bounds exception") that was removed, as gcc-13 should be fixed.

regards,
Marc

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |

Attachment: signature.asc
Description: PGP signature