Chrome platform driver development
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Benson Leung <bleung@chromium.org>,
	Guenter Roeck <groeck@chromium.org>
Cc: linux-rtc@vger.kernel.org, chrome-platform@lists.linux.dev,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: [RFC] rtc: Avoid a couple of -Wflex-array-member-not-at-end warnings
Date: Mon, 10 Mar 2025 15:46:26 +1030	[thread overview]
Message-ID: <Z851qvkycepdNlBd@kspp> (raw)

Hi all,

As part of the efforts to globally enable -Wflex-array-member-not-at-end,
I'm currently trying to fix the following warnings:

drivers/rtc/rtc-cros-ec.c:62:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
drivers/rtc/rtc-cros-ec.c:40:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

The issue is that `struct cros_ec_command` is a flexible structure (which
means that it contains a flexible-array member), and there is an object
of this type (msg) declared within another structure but at the end.

It seems that the following patch would suffice, as long as the flex-array
member in `struct cros_ec_command` is not expected to be accessed and
overlap with `struct ec_response_rtc data` in a "controlled manner":

diff --git a/drivers/rtc/rtc-cros-ec.c b/drivers/rtc/rtc-cros-ec.c
index 865c2e82c7a5..7e9bbab47e4c 100644
--- a/drivers/rtc/rtc-cros-ec.c
+++ b/drivers/rtc/rtc-cros-ec.c
@@ -37,8 +37,8 @@ static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
 {
        int ret;
        struct {
-               struct cros_ec_command msg;
                struct ec_response_rtc data;
+               struct cros_ec_command msg;
        } __packed msg;
 
        memset(&msg, 0, sizeof(msg));
@@ -59,8 +59,8 @@ static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
 {
        int ret;
        struct {
-               struct cros_ec_command msg;
                struct ec_response_rtc data;
+               struct cros_ec_command msg;
        } __packed msg;
 
        memset(&msg, 0, sizeof(msg));

Otherwise, we probably need to use struct_group_tagged() as follows:

diff --git a/drivers/rtc/rtc-cros-ec.c b/drivers/rtc/rtc-cros-ec.c
index 865c2e82c7a5..6dc815bdbcd9 100644
--- a/drivers/rtc/rtc-cros-ec.c
+++ b/drivers/rtc/rtc-cros-ec.c
@@ -37,7 +37,7 @@ static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
 {
        int ret;
        struct {
-               struct cros_ec_command msg;
+               struct cros_ec_command_hdr msg;
                struct ec_response_rtc data;
        } __packed msg;
 
@@ -45,7 +45,10 @@ static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
        msg.msg.command = command;
        msg.msg.insize = sizeof(msg.data);
 
-       ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
+       ret = cros_ec_cmd_xfer_status(cros_ec,
+                                     container_of(&msg.msg,
+                                                  struct cros_ec_command,
+                                                  __hdr));
        if (ret < 0)
                return ret;
 
@@ -59,7 +62,7 @@ static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
 {
        int ret;
        struct {
-               struct cros_ec_command msg;
+               struct cros_ec_command_hdr msg;
                struct ec_response_rtc data;
        } __packed msg;
 
@@ -68,7 +71,10 @@ static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
        msg.msg.outsize = sizeof(msg.data);
        msg.data.time = param;
 
-       ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
+       ret = cros_ec_cmd_xfer_status(cros_ec,
+                                     container_of(&msg.msg,
+                                                  struct cros_ec_command,
+                                                  __hdr));
        if (ret < 0)
                return ret;
        return 0;
diff --git a/include/linux/platform_data/cros_ec_proto.h b/include/linux/platform_data/cros_ec_proto.h
index 3ec24f445c29..2a638c8c5ec2 100644
--- a/include/linux/platform_data/cros_ec_proto.h
+++ b/include/linux/platform_data/cros_ec_proto.h
@@ -80,11 +80,13 @@ enum {
  * @data: Where to put the incoming data from EC and outgoing data to EC.
  */
 struct cros_ec_command {
-       uint32_t version;
-       uint32_t command;
-       uint32_t outsize;
-       uint32_t insize;
-       uint32_t result;
+       struct_group_tagged(cros_ec_command_hdr, __hdr,
+               uint32_t version;
+               uint32_t command;
+               uint32_t outsize;
+               uint32_t insize;
+               uint32_t result;
+       );
        uint8_t data[];
 };

What do you think?

Thanks!
--
Gustavo

             reply	other threads:[~2025-03-10  5:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-10  5:16 Gustavo A. R. Silva [this message]
2025-03-12  5:17 ` [RFC] rtc: Avoid a couple of -Wflex-array-member-not-at-end warnings Tzung-Bi Shih
2025-03-14  0:47   ` Gustavo A. R. Silva

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=Z851qvkycepdNlBd@kspp \
    --to=gustavoars@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=groeck@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    /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