Skip to contentSkip to conversation

nfp: Use static_assert() to check struct sizes

PATCH Applied Gustavo A. R. Silva 3 participants 3 messages Aug 8, 2024 – Aug 13, 2024

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

PATCH 1/1 nfp: Use static_assert() to check struct sizes

Commit d88cabfd9abc ("nfp: Avoid -Wflex-array-member-not-at-end warnings") introduced tagged struct nfp_dump_tl_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.

---
 drivers/net/ethernet/netronome/nfp/nfp_net_debugdump.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/netronome/nfp/nfp_net_debugdump.c +2 -0
index 2dd37557185e..7276e44a21d0 100644
@@ -41,6 +41,8 @@ struct nfp_dump_tl {
 	);
 	char data[];
 };
+static_assert(offsetof(struct nfp_dump_tl, data) == sizeof(struct nfp_dump_tl_hdr),
+	      "struct member likely outside of struct_group_tagged()");
 
 /* NFP CPP parameters */
 struct nfp_dumpspec_cpp_isl_id {
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Gustavo A. R. Silva

Commit d88cabfd9abc ("nfp: Avoid -Wflex-array-member-not-at-end warnings") introduced tagged struct nfp_dump_tl_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.

#2 lore raw
In reply to Gustavo A. R. Silva
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Thu, 8 Aug 2024 16:08:35 -0600 you wrote:

Commit d88cabfd9abc ("nfp: Avoid -Wflex-array-member-not-at-end warnings") introduced tagged struct nfp_dump_tl_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.

[...]

Here is the summary with links:
- [next] nfp: Use static_assert() to check struct sizes

    https://git.kernel.org/netdev/net-next/c/46dd90fe51f3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html