diff mbox series

[next] crypto: nx - Use static_assert() to check struct sizes

Message ID ZrVCAqGrl+5prW1Y@cute
State New
Headers show
Series [next] crypto: nx - Use static_assert() to check struct sizes | expand

Commit Message

Gustavo A. R. Silva Aug. 8, 2024, 10:09 p.m. UTC
Commit 1e6b251ce175 ("crypto: nx - Avoid -Wflex-array-member-not-at-end
warning") introduced tagged `struct nx842_crypto_header_hdr`. We want
to ensure that when new members need to be added to the flexible
structure, they are always included within this tagged struct.

So, we use `static_assert()` to ensure that the memory layout for
both the flexible structure and the tagged struct is the same after
any changes.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/crypto/nx/nx-842.h | 3 +++
 1 file changed, 3 insertions(+)

Comments

Herbert Xu Aug. 17, 2024, 7:08 a.m. UTC | #1
On Thu, Aug 08, 2024 at 04:09:06PM -0600, Gustavo A. R. Silva wrote:
> Commit 1e6b251ce175 ("crypto: nx - Avoid -Wflex-array-member-not-at-end
> warning") introduced tagged `struct nx842_crypto_header_hdr`. We want
> to ensure that when new members need to be added to the flexible
> structure, they are always included within this tagged struct.
> 
> So, we use `static_assert()` to ensure that the memory layout for
> both the flexible structure and the tagged struct is the same after
> any changes.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/crypto/nx/nx-842.h | 3 +++
>  1 file changed, 3 insertions(+)

Patch applied.  Thanks.
diff mbox series

Patch

diff --git a/drivers/crypto/nx/nx-842.h b/drivers/crypto/nx/nx-842.h
index 25fa70b2112c..887d4ce3cb49 100644
--- a/drivers/crypto/nx/nx-842.h
+++ b/drivers/crypto/nx/nx-842.h
@@ -157,6 +157,7 @@  struct nx842_crypto_header_group {
 } __packed;
 
 struct nx842_crypto_header {
+	/* New members MUST be added within the struct_group() macro below. */
 	struct_group_tagged(nx842_crypto_header_hdr, hdr,
 		__be16 magic;		/* NX842_CRYPTO_MAGIC */
 		__be16 ignore;		/* decompressed end bytes to ignore */
@@ -164,6 +165,8 @@  struct nx842_crypto_header {
 	);
 	struct nx842_crypto_header_group group[];
 } __packed;
+static_assert(offsetof(struct nx842_crypto_header, group) == sizeof(struct nx842_crypto_header_hdr),
+	      "struct member likely outside of struct_group_tagged()");
 
 #define NX842_CRYPTO_GROUP_MAX	(0x20)