stddef: Simplify TRAILING_OVERLAP() and improve docs
cover stddef: Simplify TRAILING_OVERLAP() and improve docs
Hello,
as a follup to the discussion I had with Gustavo in reply to
https://lore.kernel.org/linux-pwm/aJtRPZpc-Lv-C6zD@kspp here comes my suggestion to improve TRAILING_OVERLAP() for wider audience.
While working at it, I wonder if __packed should also better be part of the macro to ensure that
struct myfamstruct {
char c;
char payload[];
};
TRAILING_OVERLAP(struct myfamstruct, mfs, payload,
unsigned int payload_as_int;
) fs;does the right thing. This however is orthogonal and better discussed separately from this patch set.
Best regards
Uwe
Uwe Kleine-König (2):
stddef: Add a comment about why TRAILING_OVERLAP() exists
stddef: Stop using FAM parameter of TRAILING_OVERLAP()
include/linux/stddef.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
Signature · 1 line
2.50.1
PATCH 1/2 stddef: Add a comment about why TRAILING_OVERLAP() exists
Just from reading the definition it's not obvious what the macro does and why it's needed and sensible to be used.
--- include/linux/stddef.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
include/linux/stddef.h +11 -1
@@ -97,7 +97,17 @@ enum {* 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. +* of additional members that would otherwise follow it. This is needed because +* the traditional +* +* struct { +* struct some_struct_with_FAM mystruct; +* struct some_other_struct fill_FAM; +* } +* +* with the purpose that fill_FAM overlaps the flexible-array in mystruct +* triggers a compiler warning about the flexible array member not being at the +* end of the structure. * * @TYPE: Flexible structure type name, including "struct" keyword. * @NAME: Name for a variable to define.
Signature · 1 line
2.50.1
PATCH 2/2 stddef: Stop using FAM parameter of TRAILING_OVERLAP()
In reply to Uwe Kleine-KönigAccording to my understanding of flexible array members and the c99 docs ("In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.") sizeof(TYPE) yields the same value as offsetof(TYPE, FAM). To make FAM unused rename the padding struct member to use NAME instead of FAM. This makes the macro easier to use and thus less prone to error.
--
Hello,
This allows in a followup change to drop the parameter. As this has to happen in sync with all users of the macro, I delay this change until after this patch was accepted and hits Linus' tree and then coordinate a tree-wide change with him before the next -rc1.
Best regards
Uwe
--- include/linux/stddef.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/stddef.h +1 -1
@@ -118,7 +118,7 @@ enum { union { \ TYPE NAME; \ struct { \ - unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)]; \ + unsigned char __offset_to_end_of_##NAME[sizeof(TYPE)]; \ MEMBERS \ }; \ }
Signature · 1 line
2.50.1
Re: [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP()
Quoted 6 lines · Uwe Kleine-König
According to my understanding of flexible array members and the c99 docs ("In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.") sizeof(TYPE) yields the same value as
offsetof(TYPE, FAM). To make FAM unused rename the padding struct member
No, as I commented here[1]:
"Flexible structures (structs that contain a FAM) may have trailing padding. Under that scenario sizeof(TYPE) causes the overlay between FAM and MEMBERS to be misaligned.
On the other hand, offsetof(TYPE, FAM) precisely positions the trailing MEMBERS where the FAM begins, which is correct and safe."
sizeof(TYPE) does not always equal to offsetof(TYPE, FAM).
See this:
https://lore.kernel.org/linux-hardening/aLiYrQGdGmaDTtLF@kspp/
Thanks
-Gustavo
[1] https://lore.kernel.org/linux-hardening/[email protected]/
Quoted 8 lines · Uwe Kleine-König
Hello,
as a follup to the discussion I had with Gustavo in reply to
https://lore.kernel.org/linux-pwm/aJtRPZpc-Lv-C6zD@kspp here comes my suggestion to improveTRAILING_OVERLAP()for wider audience.While working at it, I wonder if
__packedshould also better be part of the macro to ensure that
I have patch ready for this:
include/linux/stddef.h +8 -0
@@ -93,6 +93,14 @@ enum {#define DECLARE_FLEX_ARRAY (TYPE ,NAME ) \ __DECLARE_FLEX_ARRAY(TYPE, NAME) +#define __TRAILING_OVERLAP (TYPE ,NAME ,FAM ,MEMBERS ,ATTRS ) \ + union { \ + TYPE NAME; \ + struct { \ + unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)]; \ + MEMBERS \ + } ATTRS; \ + }/** * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members. *
Thanks
-Gustavo