Skip to contentSkip to conversation

jbd2: Avoid dozens of -Wflex-array-member-not-at-end warnings

PATCH Applied Gustavo A. R. Silva 3 participants 3 messages Nov 1, 2024 – Nov 14, 2024 Journalling layer for block devices (JBD2)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 jbd2: Avoid dozens of -Wflex-array-member-not-at-end warnings

-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 (struct shash_desc) where the size of the flexible-array member (__ctx) is known at compile-time, and refactor the rest of the code, accordingly.

So, with this, fix 77 of the following warnings:

include/linux/jbd2.h:1800:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes in v2:
- Use DIV_ROUND_UP() to compute the number of elements for the flex array. (Jan Kara)

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

 include/linux/jbd2.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
include/linux/jbd2.h +7 -8
index 8aef9bb6ad57..50f7ea8714bf 100644
@@ -1796,22 +1796,21 @@ static inline unsigned long jbd2_log_space_left(journal_t *journal)
 static inline u32 jbd2_chksum(journal_t *journal, u32 crc,
 			      const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[JBD_MAX_CHECKSUM_SIZE];
-	} desc;
+	DEFINE_RAW_FLEX(struct shash_desc, desc, __ctx,
+		DIV_ROUND_UP(JBD_MAX_CHECKSUM_SIZE,
+			     sizeof(*((struct shash_desc *)0)->__ctx)));
 	int err;
 
 	BUG_ON(crypto_shash_descsize(journal->j_chksum_driver) >
 		JBD_MAX_CHECKSUM_SIZE);
 
-	desc.shash.tfm = journal->j_chksum_driver;
-	*(u32 *)desc.ctx = crc;
+	desc->tfm = journal->j_chksum_driver;
+	*(u32 *)desc->__ctx = crc;
 
-	err = crypto_shash_update(&desc.shash, address, length);
+	err = crypto_shash_update(desc, address, length);
 	BUG_ON(err);
 
-	return *(u32 *)desc.ctx;
+	return *(u32 *)desc->__ctx;
 }
 
 /* Return most recent uncommitted transaction */
Signature · 1 line
2.43.0
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 an on-stack definition of a flexible structure (struct shash_desc) where the size of the flexible-array member (__ctx) is known at compile-time, and refactor the rest of the code, accordingly.

Quoted 5 lines

So, with this, fix 77 of the following warnings:

include/linux/jbd2.h:1800:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

Looks good. Feel free to add:

Honza

Quoted 48 lines

---
Changes in v2:
- Use DIV_ROUND_UP() to compute the number of elements for the flex array. (Jan Kara)

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

 include/linux/jbd2.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
include/linux/jbd2.h +7 -8
index 8aef9bb6ad57..50f7ea8714bf 100644
@@ -1796,22 +1796,21 @@ static inline unsigned long jbd2_log_space_left(journal_t *journal)
 static inline u32 jbd2_chksum(journal_t *journal, u32 crc,
 			      const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[JBD_MAX_CHECKSUM_SIZE];
-	} desc;
+	DEFINE_RAW_FLEX(struct shash_desc, desc, __ctx,
+		DIV_ROUND_UP(JBD_MAX_CHECKSUM_SIZE,
+			     sizeof(*((struct shash_desc *)0)->__ctx)));
 	int err;
 
 	BUG_ON(crypto_shash_descsize(journal->j_chksum_driver) >
 		JBD_MAX_CHECKSUM_SIZE);
 
-	desc.shash.tfm = journal->j_chksum_driver;
-	*(u32 *)desc.ctx = crc;
+	desc->tfm = journal->j_chksum_driver;
+	*(u32 *)desc->__ctx = crc;
 
-	err = crypto_shash_update(&desc.shash, address, length);
+	err = crypto_shash_update(desc, address, length);
 	BUG_ON(err);
 
-	return *(u32 *)desc.ctx;
+	return *(u32 *)desc->__ctx;
 }
 
 /* Return most recent uncommitted transaction */
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 9 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 (struct shash_desc) where the size of the flexible-array member (__ctx) is known at compile-time, and refactor the rest of the code, accordingly.

[...]

Applied, thanks!

[1/1] jbd2: Avoid dozens of -Wflex-array-member-not-at-end warnings

      commit: 2bd9077b6261b8f1281d0fa74d51afe090319263

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>