Skip to contentSkip to conversation

watchdog: cros-ec: Avoid -Wflex-array-member-not-at-end warning

PATCH In review Gustavo A. R. Silva 4 participants 6 messages Mar 27, 2025 – Apr 16, 2025

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

PATCH v2 1/1 watchdog: cros-ec: 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 on-stack definitions 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/watchdog/cros_ec_wdt.c:29:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes v2:
- Fix truncated line in changelog text. (Tzung-Bi)
- Update variable name: s/buf/msg (Tzung-Bi)
- Cast to structs instead of union. (Tzung-Bi)
- Add RB tag.

v1:
- Link: https://lore.kernel.org/linux-hardening/Z-SBITmMfwjocYwL@kspp/

 drivers/watchdog/cros_ec_wdt.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
drivers/watchdog/cros_ec_wdt.c +13 -17
index 716c23f4388c..9ffe7f505645 100644
@@ -25,26 +25,22 @@ static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec,
 				union cros_ec_wdt_data *arg)
 {
 	int ret;
-	struct {
-		struct cros_ec_command msg;
-		union cros_ec_wdt_data data;
-	} __packed buf = {
-		.msg = {
-			.version = 0,
-			.command = EC_CMD_HANG_DETECT,
-			.insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
-				   sizeof(struct ec_response_hang_detect) :
-				   0,
-			.outsize = sizeof(struct ec_params_hang_detect),
-		},
-		.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_wdt_data));
+
+	msg->version = 0;
+	msg->command = EC_CMD_HANG_DETECT;
+	msg->insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
+		   sizeof(struct ec_response_hang_detect) :
+		   0;
+	msg->outsize = sizeof(struct ec_params_hang_detect);
+	*(struct ec_params_hang_detect *)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_hang_detect *)msg->data;
 
 	return 0;
 }
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 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 on-stack definitions 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.

Quoted 6 lines

So, with these changes, fix the following warning:

drivers/watchdog/cros_ec_wdt.c:29:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 45 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 on-stack definitions 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/watchdog/cros_ec_wdt.c:29:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes v2:
- Fix truncated line in changelog text. (Tzung-Bi)
- Update variable name: s/buf/msg (Tzung-Bi)
- Cast to structs instead of union. (Tzung-Bi)
- Add RB tag.

v1:
- Link: https://lore.kernel.org/linux-hardening/Z-SBITmMfwjocYwL@kspp/

 drivers/watchdog/cros_ec_wdt.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
drivers/watchdog/cros_ec_wdt.c +0 -10
index 716c23f4388c..9ffe7f505645 100644
@@ -25,26 +25,22 @@ static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec,
 				union cros_ec_wdt_data *arg)
 {
 	int ret;
-	struct {
-		struct cros_ec_command msg;
-		union cros_ec_wdt_data data;
-	} __packed buf = {
-		.msg = {
-			.version = 0,
-			.command = EC_CMD_HANG_DETECT,
-			.insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
-				   sizeof(struct ec_response_hang_detect) :
-				   0,

drivers/watchdog/cros_ec_wdt.c static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec, · line 31

Quoted 10 lines
drivers/watchdog/cros_ec_wdt.c +4 -6
-			.outsize = sizeof(struct ec_params_hang_detect),
-		},
-		.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_wdt_data));
+
+	msg->version = 0;

Technically redundant, but it was redundant before, so keeping style is okay.

drivers/watchdog/cros_ec_wdt.c static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec, · line 43

Quoted 13 lines
drivers/watchdog/cros_ec_wdt.c +9 -1
+	msg->command = EC_CMD_HANG_DETECT;
+	msg->insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
+		   sizeof(struct ec_response_hang_detect) :
+		   0;
+	msg->outsize = sizeof(struct ec_params_hang_detect);
+	*(struct ec_params_hang_detect *)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_hang_detect *)msg->data;

msg->data used twice and a "sizeof()" earlier... might be nicer to have an explicit pointer?

-Kees

Quoted 6 lines
drivers/watchdog/cros_ec_wdt.c +0 -0
 	return 0;
 }
Signature · 1 line
2.43.0
Signature · 1 line
Kees Cook
#3 lore raw
Quoted 13 lines
Quoted 13 lines
drivers/watchdog/cros_ec_wdt.c +8 -0
+	msg->command = EC_CMD_HANG_DETECT;
+	msg->insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
+		   sizeof(struct ec_response_hang_detect) :
+		   0;
+	msg->outsize = sizeof(struct ec_params_hang_detect);
+	*(struct ec_params_hang_detect *)msg->data = arg->req;
+
+	ret = cros_ec_cmd_xfer_status(cros_ec, msg);
  	if (ret < 0)
  		return ret;
Quoted 5 lines

drivers/watchdog/cros_ec_wdt.c static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec, · line 43

Quoted 2 lines
drivers/watchdog/cros_ec_wdt.c +1 -1
-	arg->resp = buf.data.resp;
+	arg->resp = *(struct ec_response_hang_detect *)msg->data;

msg->data used twice and a "sizeof()" earlier... might be nicer to have an explicit pointer?

Those are two different pointers:

*(struct ec_params_hang_detect *)msg->data = arg->req; arg->resp = *(struct ec_response_hang_detect *)msg->data;

--
Gustavo

Reviewed-by
#4 lore raw
Quoted 18 lines · Gustavo A. R. Silva
Quoted 17 lines
Quoted 4 lines
drivers/watchdog/cros_ec_wdt.c +2 -0
+	msg->command = EC_CMD_HANG_DETECT;
+	msg->insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?

drivers/watchdog/cros_ec_wdt.c static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec, · line 43

Quoted 10 lines
drivers/watchdog/cros_ec_wdt.c +7 -1
+		   sizeof(struct ec_response_hang_detect) :
+		   0;
+	msg->outsize = sizeof(struct ec_params_hang_detect);
+	*(struct ec_params_hang_detect *)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_hang_detect *)msg->data;

msg->data used twice and a "sizeof()" earlier... might be nicer to have an explicit pointer?

Quoted 4 lines

Those are two different pointers:

*(struct ec_params_hang_detect *)msg->data = arg->req; arg->resp = *(struct ec_response_hang_detect *)msg->data;

Argh. I did it again. I can't see "params" vs "response". q_q

Sorry for the noise!

Signature · 1 line
Kees Cook
#5 lore raw
In reply to Gustavo A. R. Silva

Hi all,

Friendly ping: who can take this patch, please? 🙂

Thanks!
-Gustavo

Quoted 71 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 on-stack definitions 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/watchdog/cros_ec_wdt.c:29:40: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes v2:
- Fix truncated line in changelog text. (Tzung-Bi)
- Update variable name: s/buf/msg (Tzung-Bi)
- Cast to structs instead of union. (Tzung-Bi)
- Add RB tag.

v1:
- Link: https://lore.kernel.org/linux-hardening/Z-SBITmMfwjocYwL@kspp/

  drivers/watchdog/cros_ec_wdt.c | 30 +++++++++++++-----------------
  1 file changed, 13 insertions(+), 17 deletions(-)
drivers/watchdog/cros_ec_wdt.c +13 -17
index 716c23f4388c..9ffe7f505645 100644
@@ -25,26 +25,22 @@ static int cros_ec_wdt_send_cmd(struct cros_ec_device *cros_ec,
  				union cros_ec_wdt_data *arg)
  {
  	int ret;
-	struct {
-		struct cros_ec_command msg;
-		union cros_ec_wdt_data data;
-	} __packed buf = {
-		.msg = {
-			.version = 0,
-			.command = EC_CMD_HANG_DETECT,
-			.insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
-				   sizeof(struct ec_response_hang_detect) :
-				   0,
-			.outsize = sizeof(struct ec_params_hang_detect),
-		},
-		.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_wdt_data));
+
+	msg->version = 0;
+	msg->command = EC_CMD_HANG_DETECT;
+	msg->insize  = (arg->req.command == EC_HANG_DETECT_CMD_GET_STATUS) ?
+		   sizeof(struct ec_response_hang_detect) :
+		   0;
+	msg->outsize = sizeof(struct ec_params_hang_detect);
+	*(struct ec_params_hang_detect *)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_hang_detect *)msg->data;
 
  	return 0;
  }