Skip to contentSkip to conversation

leds: Avoid -Wflex-array-member-not-at-end warning

PATCH In review Gustavo A. R. Silva 3 participants 5 messages Mar 28, 2025 – Mar 31, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: Avoid -Wflex-array-member-not-at-end warning

-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]

---
 drivers/leds/leds-cros_ec.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
drivers/leds/leds-cros_ec.c +11 -15
index 275522b81ea5..6eab0474f52d 100644
@@ -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;
+
+	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;
 }
Signature · 1 line
2.43.0
#1 lore raw

Hi Gustavo,

Quoted 37 lines · Gustavo A. R. Silva

-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]

---
 drivers/leds/leds-cros_ec.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
drivers/leds/leds-cros_ec.c +0 -12
index 275522b81ea5..6eab0474f52d 100644
@@ -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
-	};

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 76

Quoted 10 lines
drivers/leds/leds-cros_ec.c +8 -2
-
-	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 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)

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 85

Quoted 10 lines
drivers/leds/leds-cros_ec.c +3 -1
+
+	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

#2 lore raw

Hi!

Quoted 44 lines · Thomas Weißschuh

Hi Gustavo,

Quoted 41 lines · Gustavo A. R. Silva

-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]

---
  drivers/leds/leds-cros_ec.c | 26 +++++++++++---------------
  1 file changed, 11 insertions(+), 15 deletions(-)
drivers/leds/leds-cros_ec.c +2 -14
index 275522b81ea5..6eab0474f52d 100644
@@ -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));
Quoted 8 lines

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 76

Quoted 6 lines
drivers/leds/leds-cros_ec.c +6 -0
+
+	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:

drivers/leds/leds-cros_ec.c +15 -15
index 275522b81ea5..c7235f4e577b 100644
@@ -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

Quoted 20 lines

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)

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 85

Quoted 10 lines
drivers/leds/leds-cros_ec.c +3 -1
+
+	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

#3 lore raw

Hi!

Quoted 88 lines · Gustavo A. R. Silva
Quoted 42 lines · Thomas Weißschuh
Quoted 41 lines · Gustavo A. R. Silva

-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]

---
  drivers/leds/leds-cros_ec.c | 26 +++++++++++---------------
  1 file changed, 11 insertions(+), 15 deletions(-)
drivers/leds/leds-cros_ec.c +2 -14
index 275522b81ea5..6eab0474f52d 100644
@@ -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));
Quoted 8 lines

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 76

Quoted 6 lines
drivers/leds/leds-cros_ec.c +6 -0
+
+	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:

drivers/leds/leds-cros_ec.c +12 -14
index 275522b81ea5..c7235f4e577b 100644
@@ -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;

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 89

Quoted 10 lines
drivers/leds/leds-cros_ec.c +3 -1
+
+       ret = cros_ec_cmd_xfer_status(cros_ec, msg);
        if (ret < 0)
                return ret;
 
-       arg->resp = buf.data.resp;
+       arg->resp = *resp;
 
        return 0;
 }

My issue was with the general usage of DEFINE_RAW_FLEX(), the reply was placed badly. For somebody not knowing this construct it's not clear what is happening under the hood.
It's probably fine in a regular header file with some explanation, but in a random driver it looks off.

The following is what I had in mind. Now actually tested.

drivers/leds/leds-cros_ec.c +4 -17
@@ -60,31 +60,18 @@ static inline struct cros_ec_led_priv *cros_ec_led_cdev_to_priv(struct led_class
 union cros_ec_led_cmd_data {
        struct ec_params_led_control req;
        struct ec_response_led_control resp;
-} __packed;
+};
 
 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);
+
+       ret = cros_ec_cmd(cros_ec, 1, EC_CMD_LED_CONTROL,
+                         &arg->req, sizeof(arg->req), &arg->resp, sizeof(arg->resp));
        if (ret < 0)
                return ret;
 
-       arg->resp = buf.data.resp;
-
        return 0;
 }
 
Quoted 14 lines

as in other cases:

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

-Gustavo

Quoted 7 lines

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)

Quoted 8 lines
Quoted 8 lines

drivers/leds/leds-cros_ec.c static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec, · line 83

Quoted 8 lines
drivers/leds/leds-cros_ec.c +3 -1
+
+	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

#4 lore raw
Quoted 8 lines

My issue was with the general usage of DEFINE_RAW_FLEX(), the reply was placed badly. For somebody not knowing this construct it's not clear what is happening under the hood.
It's probably fine in a regular header file with some explanation, but in a random driver it looks off.

The following is what I had in mind. Now actually tested.

OK, let's go with that :)

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

Thanks!
--
Gustavo