linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] can: etas_es58x: avoid -Wzero-length-bounds warning
@ 2021-09-20 12:30 Arnd Bergmann
  2021-09-21  2:53 ` Vincent MAILHOL
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2021-09-20 12:30 UTC (permalink / raw)
  To: Vincent Mailhol, Wolfgang Grandegger, Marc Kleine-Budde,
	David S. Miller, Jakub Kicinski
  Cc: Arnd Bergmann, Arunachalam Santhanam, linux-can, netdev, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

gcc complains when writing into a zero-length array:

drivers/net/can/usb/etas_es58x/es581_4.c: In function 'es581_4_tx_can_msg':
drivers/net/can/usb/etas_es58x/es581_4.c:374:42: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]
  374 |         tx_can_msg = (typeof(tx_can_msg))&es581_4_urb_cmd->raw_msg[msg_len];
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:21,
                 from drivers/net/can/usb/etas_es58x/es581_4.c:15:
drivers/net/can/usb/etas_es58x/es581_4.h:195:20: note: while referencing 'raw_msg'
  195 |                 u8 raw_msg[0];
      |                    ^~~~~~~
  CC [M]  drivers/net/can/usb/etas_es58x/es58x_fd.o
drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':
drivers/net/can/usb/etas_es58x/es58x_fd.c:360:42: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]
  360 |         tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len];
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22,
                 from drivers/net/can/usb/etas_es58x/es58x_fd.c:17:
drivers/net/can/usb/etas_es58x/es58x_fd.h:222:20: note: while referencing 'raw_msg'
  222 |                 u8 raw_msg[0];
      |                    ^~~~~~~

The solution is usually to use a flexible-array member the struct, but
we can't directly have that inside of a union, nor can it be the only
member of a struct, so add a dummy struct with another zero-length
member to get the intended behavior.

If someone has a better workaround, let me know and I can send a new
patch, as this version is rather ugly.

Fixes: f4f5247daa45 ("can: etas_es58x: rewrite the message cast in es58{1,_fd}_tx_can_msg to increase readability")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/net/can/usb/etas_es58x/es581_4.h  | 5 ++++-
 drivers/net/can/usb/etas_es58x/es58x_fd.h | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/usb/etas_es58x/es581_4.h b/drivers/net/can/usb/etas_es58x/es581_4.h
index 4bc60a6df697..ac5ef88db565 100644
--- a/drivers/net/can/usb/etas_es58x/es581_4.h
+++ b/drivers/net/can/usb/etas_es58x/es581_4.h
@@ -192,7 +192,10 @@ struct es581_4_urb_cmd {
 		struct es581_4_rx_cmd_ret rx_cmd_ret;
 		__le64 timestamp;
 		u8 rx_cmd_ret_u8;
-		u8 raw_msg[0];
+		struct {
+			u8 __pad[0];
+			u8 raw_msg[];
+		};
 	} __packed;
 
 	__le16 reserved_for_crc16_do_not_use;
diff --git a/drivers/net/can/usb/etas_es58x/es58x_fd.h b/drivers/net/can/usb/etas_es58x/es58x_fd.h
index a191891b8777..253e7bafd0b6 100644
--- a/drivers/net/can/usb/etas_es58x/es58x_fd.h
+++ b/drivers/net/can/usb/etas_es58x/es58x_fd.h
@@ -219,7 +219,10 @@ struct es58x_fd_urb_cmd {
 		struct es58x_fd_tx_ack_msg tx_ack_msg;
 		__le64 timestamp;
 		__le32 rx_cmd_ret_le32;
-		u8 raw_msg[0];
+		struct {
+			u8 __pad[0];
+			u8 raw_msg[];
+		};
 	} __packed;
 
 	__le16 reserved_for_crc16_do_not_use;
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] can: etas_es58x: avoid -Wzero-length-bounds warning
  2021-09-20 12:30 [PATCH] can: etas_es58x: avoid -Wzero-length-bounds warning Arnd Bergmann
@ 2021-09-21  2:53 ` Vincent MAILHOL
  0 siblings, 0 replies; 2+ messages in thread
From: Vincent MAILHOL @ 2021-09-21  2:53 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Wolfgang Grandegger, Marc Kleine-Budde, David S. Miller,
	Jakub Kicinski, Arnd Bergmann, Arunachalam Santhanam, linux-can,
	netdev, open list, Kees Cook

Hi Arnd,
+CC: Kees Cook

On Mon. 20 Sep 2021 at 21:30, Arnd Bergmann <arnd@kernel.org> wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> gcc complains when writing into a zero-length array:
>
> drivers/net/can/usb/etas_es58x/es581_4.c: In function 'es581_4_tx_can_msg':
> drivers/net/can/usb/etas_es58x/es581_4.c:374:42: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]
>   374 |         tx_can_msg = (typeof(tx_can_msg))&es581_4_urb_cmd->raw_msg[msg_len];
>       |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:21,
>                  from drivers/net/can/usb/etas_es58x/es581_4.c:15:
> drivers/net/can/usb/etas_es58x/es581_4.h:195:20: note: while referencing 'raw_msg'
>   195 |                 u8 raw_msg[0];
>       |                    ^~~~~~~
>   CC [M]  drivers/net/can/usb/etas_es58x/es58x_fd.o
> drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':
> drivers/net/can/usb/etas_es58x/es58x_fd.c:360:42: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]
>   360 |         tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len];
>       |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22,
>                  from drivers/net/can/usb/etas_es58x/es58x_fd.c:17:
> drivers/net/can/usb/etas_es58x/es58x_fd.h:222:20: note: while referencing 'raw_msg'
>   222 |                 u8 raw_msg[0];
>       |                    ^~~~~~~
>
> The solution is usually to use a flexible-array member the struct, but
> we can't directly have that inside of a union, nor can it be the only
> member of a struct, so add a dummy struct with another zero-length
> member to get the intended behavior.
>
> If someone has a better workaround, let me know and I can send a new
> patch, as this version is rather ugly.

Actually, there is one. Kees Cook introduced a new macro,
DECLARE_FLEX_ARRAY(), to do this in a more elegant way:
https://lkml.org/lkml/2021/8/27/524

The same series also fixes the warning in the etas_es58x driver:
https://lkml.org/lkml/2021/8/27/523

So we only need to wait for Kees's series to get merged :)


Yours sincerely,
Vincent Mailhol

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-21  3:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-20 12:30 [PATCH] can: etas_es58x: avoid -Wzero-length-bounds warning Arnd Bergmann
2021-09-21  2:53 ` Vincent MAILHOL

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).