[PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP()

Uwe Kleine-König posted 2 patches 2 weeks, 6 days ago
[PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP()
Posted by Uwe Kleine-König 2 weeks, 6 days ago
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
to use NAME instead of FAM. This makes the macro easier to use and thus
less prone to error.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
--
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(-)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index a748efcd626f..305e25a67271 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -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							\
 		};								\
 	}
-- 
2.50.1

Re: [PATCH 2/2] stddef: Stop using FAM parameter of TRAILING_OVERLAP()
Posted by Gustavo A. R. Silva 2 weeks, 6 days ago

On 9/12/25 12:28, Uwe Kleine-König wrote:
> 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/4b9eea66-f004-4b5f-bf48-4c32205cc8ee@embeddedor.com/