]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
stddef: Introduce __TRAILING_OVERLAP()
authorGustavo A. R. Silva <gustavoars@kernel.org>
Wed, 17 Sep 2025 13:28:07 +0000 (15:28 +0200)
committerKees Cook <kees@kernel.org>
Wed, 17 Sep 2025 16:29:43 +0000 (09:29 -0700)
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;

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/f80c529b239ce11f0a51f714fe00ddf839e05f5e.1758115257.git.gustavoars@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>
include/linux/stddef.h

index 701099c67c240aecc3b39b53d4aff5a13d1c8ecf..80b6bfb944f0d27a6841693ddd960f6f353303bc 100644 (file)
@@ -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