Skip to contentSkip to conversation

Expand TRAILING_OVERLAP() capabilities

PATCH Applied Gustavo A. R. Silva 2 participants 4 messages Sep 17, 2025

lore
Share & tools
lore
#0 lore raw

cover Expand TRAILING_OVERLAP() capabilities

Small series aimed at expanding TRAILING_OVERLAP() capabilities.

Gustavo A. R. Silva (2):
stddef: Remove token-pasting in TRAILING_OVERLAP()
stddef: Introduce __TRAILING_OVERLAP()

 include/linux/stddef.h | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 stddef: Remove token-pasting in TRAILING_OVERLAP()

Currently, TRAILING_OVERLAP() token-pastes the FAM parameter into the name of internal pdding member __offset_to_##FAM. This forces FAM to be a single identifier, which prevents callers from using a FAM when it's a nested member. For instance, see the following scenario:

| struct flex {
| 	size_t count;
| 	int data[];
| };
| struct foo {
| 	int hdr_foo;
| 	struct flex f;
| };
| struct composite {
| 	struct foo hdr;
| 	int data[100];
| };

In this case, it'd be useful if TRAILING_OVERLAP() could be used in the following way:

| struct composite {
| 	TRAILING_OVERLAP(struct foo, hdr, f.data,
| 		int data[100];
| 	);
| };

However, this is not current possible due to the token concatenation in __offset_to_##FAM, which fails when FAM contains a dot.

So, remove token-pasting and use the fixed internal name __offset_to_FAM and, with this, expand the capabilities of TRAILING_OVERLAP(). :)

---
 include/linux/stddef.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/stddef.h +1 -1
index dab49e2ec8c0..701099c67c24 100644
@@ -108,7 +108,7 @@ enum {
 	union {									\
 		TYPE NAME;							\
 		struct {							\
-			unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)];	\
+			unsigned char __offset_to_FAM[offsetof(TYPE, FAM)];	\
 			MEMBERS							\
 		};								\
 	}
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 stddef: Introduce __TRAILING_OVERLAP()

In reply to Gustavo A. R. Silva

Introduce underlying __TRAILING_OVERLAP() macro to let callers apply atributes to trailing overlapping members.

For instance, the code below:

| struct flex {
| 	size_t count;
| 	int data[];
| };
| struct {
| 	struct flex f;
| 	struct foo a;
| 	struct boo b;
| } __packed instance;

can now be changed to the following, and preserve the __packed attribute:

| __TRAILING_OVERLAP(struct flex, f, data, __packed,
| 	struct foo a;
| 	struct boo b;
| ) instance;
---
 include/linux/stddef.h | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
include/linux/stddef.h +19 -3
index 701099c67c24..80b6bfb944f0 100644
@@ -94,7 +94,8 @@ enum {
 	__DECLARE_FLEX_ARRAY(TYPE, NAME)
 
 /**
- * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
+ * __TRAILING_OVERLAP() - Overlap a flexible-array member with trailing
+ *			  members.
  *
  * Creates a union between a flexible-array member (FAM) in a struct and a set
  * of additional members that would otherwise follow it.
@@ -102,15 +103,30 @@ enum {
  * @TYPE: Flexible structure type name, including "struct" keyword.
  * @NAME: Name for a variable to define.
  * @FAM: The flexible-array member within @TYPE
+ * @ATTRS: Any struct attributes (usually empty)
  * @MEMBERS: Trailing overlapping members.
  */
-#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS)				\
+#define __TRAILING_OVERLAP(TYPE, NAME, FAM, ATTRS, MEMBERS)			\
 	union {									\
 		TYPE NAME;							\
 		struct {							\
 			unsigned char __offset_to_FAM[offsetof(TYPE, FAM)];	\
 			MEMBERS							\
-		};								\
+		} ATTRS;							\
 	}
 
+/**
+ * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
+ *
+ * Creates a union between a flexible-array member (FAM) in a struct and a set
+ * of additional members that would otherwise follow it.
+ *
+ * @TYPE: Flexible structure type name, including "struct" keyword.
+ * @NAME: Name for a variable to define.
+ * @FAM: The flexible-array member within @TYPE
+ * @MEMBERS: Trailing overlapping members.
+ */
+#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS)				\
+	__TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS)
+
 #endif
Signature · 1 line
2.43.0
Applied
#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 10 lines · Gustavo A. R. Silva

Small series aimed at expanding TRAILING_OVERLAP() capabilities.

Gustavo A. R. Silva (2):
stddef: Remove token-pasting in TRAILING_OVERLAP()
stddef: Introduce __TRAILING_OVERLAP()

include/linux/stddef.h | 24 ++++++++++++++++++++----

 1 file changed, 20 insertions(+), 4 deletions(-)

[...]

Applied to for-next/hardening, thanks!

[1/2] stddef: Remove token-pasting in TRAILING_OVERLAP()

      https://git.kernel.org/kees/c/413187f79062
[2/2] stddef: Introduce __TRAILING_OVERLAP()
      https://git.kernel.org/kees/c/2bbdcf02c3f3

Take care,

Signature · 1 line
Kees Cook