Skip to contentSkip to conversation

scsi: sd: Avoid -Wflex-array-member-not-at-end warning

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Apr 26, 2025 – May 1, 2025 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: sd: 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.

Also, there is no need to use the DECLARE_FLEX_ARRAY() helper. Replace it with a regular flexible-array member declaration instead.

So, with these changes, fix the following warning:

drivers/scsi/sd.c:3195:50: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
 drivers/scsi/sd.c         | 13 +++++--------
 include/scsi/scsi_proto.h |  2 +-
 2 files changed, 6 insertions(+), 9 deletions(-)
drivers/scsi/sd.c +5 -8
index 950d8c9fb884..aa15b1085235 100644
@@ -3191,10 +3191,7 @@ sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
 static bool sd_is_perm_stream(struct scsi_disk *sdkp, unsigned int stream_id)
 {
 	u8 cdb[16] = { SERVICE_ACTION_IN_16, SAI_GET_STREAM_STATUS };
-	struct {
-		struct scsi_stream_status_header h;
-		struct scsi_stream_status s;
-	} buf;
+	DEFINE_RAW_FLEX(struct scsi_stream_status_header, buf, stream_status, 1);
 	struct scsi_device *sdev = sdkp->device;
 	struct scsi_sense_hdr sshdr;
 	const struct scsi_exec_args exec_args = {
@@ -3203,9 +3200,9 @@ static bool sd_is_perm_stream(struct scsi_disk *sdkp, unsigned int stream_id)
 	int res;
 
 	put_unaligned_be16(stream_id, &cdb[4]);
-	put_unaligned_be32(sizeof(buf), &cdb[10]);
+	put_unaligned_be32(__struct_size(buf), &cdb[10]);
 
-	res = scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, &buf, sizeof(buf),
+	res = scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, buf, __struct_size(buf),
 			       SD_TIMEOUT, sdkp->max_retries, &exec_args);
 	if (res < 0)
 		return false;
@@ -3213,9 +3210,9 @@ static bool sd_is_perm_stream(struct scsi_disk *sdkp, unsigned int stream_id)
 		sd_print_sense_hdr(sdkp, &sshdr);
 	if (res)
 		return false;
-	if (get_unaligned_be32(&buf.h.len) < sizeof(struct scsi_stream_status))
+	if (get_unaligned_be32(&buf->len) < sizeof(struct scsi_stream_status))
 		return false;
-	return buf.h.stream_status[0].perm;
+	return buf->stream_status[0].perm;
 }
 
 static void sd_read_io_hints(struct scsi_disk *sdkp, unsigned char *buffer)
include/scsi/scsi_proto.h +1 -1
index aeca37816506..0ae7adc8a5db 100644
@@ -349,7 +349,7 @@ struct scsi_stream_status_header {
 	__be32 len;	/* length in bytes of stream_status[] array. */
 	u16 reserved;
 	__be16 number_of_open_streams;
-	DECLARE_FLEX_ARRAY(struct scsi_stream_status, stream_status);
+	struct scsi_stream_status stream_status[];
 };
 
 static_assert(sizeof(struct scsi_stream_status_header) == 8);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 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.

Also, there is no need to use the DECLARE_FLEX_ARRAY() helper. Replace it with a regular flexible-array member declaration instead.

Quoted 5 lines

So, with these changes, fix the following warning:

drivers/scsi/sd.c:3195:50: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

Looks good; thanks!

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

Yikes. This is way to ugly to live. Just remove the silly flex array entirely and everyone is better off:

drivers/scsi/scsi_proto_test.c +0 -8
index c093389edabb..2fc5c6523184 100644
@@ -30,14 +30,6 @@ static void test_scsi_proto(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, get_unaligned_be16(&ss.s.stream_identifier),
 			0x1234);
 	KUNIT_EXPECT_EQ(test, ss.s.rel_lifetime + 0, 0x3f);
-
-	static const union {
-		struct scsi_stream_status_header h;
-		u8 arr[sizeof(struct scsi_stream_status_header)];
-	} sh = { .arr = { 1, 2, 3, 4, 0, 0, 5, 6 } };
-	KUNIT_EXPECT_EQ(test, get_unaligned_be32(&sh.h.len), 0x1020304);
-	KUNIT_EXPECT_EQ(test, get_unaligned_be16(&sh.h.number_of_open_streams),
-			0x506);
 }
 
 static struct kunit_case scsi_proto_test_cases[] = {
drivers/scsi/sd.c +1 -1
index 950d8c9fb884..3f6e87705b62 100644
@@ -3215,7 +3215,7 @@ static bool sd_is_perm_stream(struct scsi_disk *sdkp, unsigned int stream_id)
 		return false;
 	if (get_unaligned_be32(&buf.h.len) < sizeof(struct scsi_stream_status))
 		return false;
-	return buf.h.stream_status[0].perm;
+	return buf.s.perm;
 }
 
 static void sd_read_io_hints(struct scsi_disk *sdkp, unsigned char *buffer)
include/scsi/scsi_proto.h +0 -1
index aeca37816506..bc8f2b2226be 100644
@@ -349,7 +349,6 @@ struct scsi_stream_status_header {
 	__be32 len;	/* length in bytes of stream_status[] array. */
 	u16 reserved;
 	__be16 number_of_open_streams;
-	DECLARE_FLEX_ARRAY(struct scsi_stream_status, stream_status);
 };
 
 static_assert(sizeof(struct scsi_stream_status_header) == 8);