lkml.org 
[lkml]   [2022]   [Sep]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRE: [PATCH v2] x86/fpu: use _Alignof to avoid UB in TYPE_ALIGN
Date
From: YingChi Long
> Sent: 27 September 2022 16:34
>
> WG14 N2350 made very clear that it is an UB having type definitions with
> in "offsetof". This patch change the implementation of macro
> "TYPE_ALIGN" to builtin "_Alignof" to avoid undefined behavior.

Interesting - what justification do they give?
Linux kernel requires that the compiler add no unnecessary padding
so that structure definitions are well defined.

IIRC that standard allows arbitrary padding between members.
So using a type definition inside offsetof() won't give a
useful value - but it still isn't really UB.

...
> #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
> - BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
> - TYPE_ALIGN(TYPE)))
> + BUILD_BUG_ON(sizeof(TYPE) != \
> + ALIGN(offsetofend(TYPE, MEMBER), _Alignof(TYPE)))

Has that ever worked?
Given:
struct foo {
int a;
char b;
char c;
};
I think CHECK_MEMBER_AT_END_OF_TYPE(struct foo, b) is true.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

\
 
 \ /
  Last update: 2022-09-27 17:59    [W:0.420 / U:0.440 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site