public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
To: "Thomas Weißschuh" <thomas@weissschuh.net>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Lee Jones <lee@kernel.org>, Pavel Machek <pavel@kernel.org>,
	Benson Leung <bleung@chromium.org>,
	Guenter Roeck <groeck@chromium.org>,
	linux-leds@vger.kernel.org, chrome-platform@lists.linux.dev,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH][next] leds: Avoid -Wflex-array-member-not-at-end warning
Date: Fri, 28 Mar 2025 12:51:02 -0600	[thread overview]
Message-ID: <9a0401f3-fa85-4308-a022-c4205fd7b50f@embeddedor.com> (raw)
In-Reply-To: <c051fd45-7eb0-465d-9e97-af294c453755@t-8ch.de>

Hi!

On 28/03/25 12:31, Thomas Weißschuh wrote:
> Hi Gustavo,
> 
> On 2025-03-28 08:33:22-0600, Gustavo A. R. Silva wrote:
>> -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
>> getting ready to enable it, globally.
>>
>> Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
>> a flexible structure where the size of the flexible-array member
>> is known at compile-time, and refactor the rest of the code,
>> accordingly.
>>
>> So, with these changes, fix the following warning:
>>
>> drivers/leds/leds-cros_ec.c:70:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
>>
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>> ---
>>   drivers/leds/leds-cros_ec.c | 26 +++++++++++---------------
>>   1 file changed, 11 insertions(+), 15 deletions(-)
>>
>> diff --git a/drivers/leds/leds-cros_ec.c b/drivers/leds/leds-cros_ec.c
>> index 275522b81ea5..6eab0474f52d 100644
>> --- a/drivers/leds/leds-cros_ec.c
>> +++ b/drivers/leds/leds-cros_ec.c
>> @@ -66,24 +66,20 @@ static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec,
>>   				union cros_ec_led_cmd_data *arg)
>>   {
>>   	int ret;
>> -	struct {
>> -		struct cros_ec_command msg;
>> -		union cros_ec_led_cmd_data data;
>> -	} __packed buf = {
>> -		.msg = {
>> -			.version = 1,
>> -			.command = EC_CMD_LED_CONTROL,
>> -			.insize  = sizeof(arg->resp),
>> -			.outsize = sizeof(arg->req),
>> -		},
>> -		.data.req = arg->req
>> -	};
>> -
>> -	ret = cros_ec_cmd_xfer_status(cros_ec, &buf.msg);
>> +	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
>> +			sizeof(union cros_ec_led_cmd_data));
>> +
>> +	msg->version = 1;
>> +	msg->command = EC_CMD_LED_CONTROL;
>> +	msg->insize  = sizeof(arg->resp);
>> +	msg->outsize = sizeof(arg->req);
>> +	*(struct ec_params_led_control *)msg->data = arg->req;
> 
> To be honest this looks really ugly and it's not at all obvious what is

We can do something like this, instead:

diff --git a/drivers/leds/leds-cros_ec.c b/drivers/leds/leds-cros_ec.c
index 275522b81ea5..c7235f4e577b 100644
--- a/drivers/leds/leds-cros_ec.c
+++ b/drivers/leds/leds-cros_ec.c
@@ -66,24 +66,24 @@ static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec,
                                 union cros_ec_led_cmd_data *arg)
  {
         int ret;
-       struct {
-               struct cros_ec_command msg;
-               union cros_ec_led_cmd_data data;
-       } __packed buf = {
-               .msg = {
-                       .version = 1,
-                       .command = EC_CMD_LED_CONTROL,
-                       .insize  = sizeof(arg->resp),
-                       .outsize = sizeof(arg->req),
-               },
-               .data.req = arg->req
-       };
-
-       ret = cros_ec_cmd_xfer_status(cros_ec, &buf.msg);
+       DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
+                       sizeof(union cros_ec_led_cmd_data));
+       struct ec_params_led_control *req =
+                               (struct ec_params_led_control *)msg->data;
+       struct ec_response_led_control *resp =
+                               (struct ec_response_led_control *)msg->data;
+
+       msg->version = 1;
+       msg->command = EC_CMD_LED_CONTROL;
+       msg->insize  = sizeof(arg->resp);
+       msg->outsize = sizeof(arg->req);
+       *req = arg->req;
+
+       ret = cros_ec_cmd_xfer_status(cros_ec, msg);
         if (ret < 0)
                 return ret;

-       arg->resp = buf.data.resp;
+       arg->resp = *resp;

         return 0;
  }

as in other cases:

https://lore.kernel.org/linux-hardening/Z-a4meHAy-t58bcE@kspp/

-Gustavo

> going on. We have the utility function cros_ec_cmd() which would be the
> nicer alternative. (Without having verified that it avoids the warning).
> While it is slightly more expensive, I don't think it matters.
> And if it does, the helper can be optimized.
> 
> (The same goes for my other cros_ec drivers)
> 
>> +
>> +	ret = cros_ec_cmd_xfer_status(cros_ec, msg);
>>   	if (ret < 0)
>>   		return ret;
>>   
>> -	arg->resp = buf.data.resp;
>> +	arg->resp = *(struct ec_response_led_control *)msg->data;
>>   
>>   	return 0;
>>   }
> 
> 
> Thomas


  reply	other threads:[~2025-03-28 18:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-28 14:33 [PATCH][next] leds: Avoid -Wflex-array-member-not-at-end warning Gustavo A. R. Silva
2025-03-28 18:31 ` Thomas Weißschuh
2025-03-28 18:51   ` Gustavo A. R. Silva [this message]
2025-03-29  9:32     ` Thomas Weißschuh
2025-03-31 17:04       ` 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=9a0401f3-fa85-4308-a022-c4205fd7b50f@embeddedor.com \
    --to=gustavo@embeddedor.com \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=groeck@chromium.org \
    --cc=gustavoars@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@kernel.org \
    --cc=thomas@weissschuh.net \
    /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