Skip to content

BUG: use _Alignof rather than offsetof() on most compilers#23016

Merged
seberg merged 2 commits intonumpy:mainfrom
kraj:alignof
Jan 17, 2023
Merged

BUG: use _Alignof rather than offsetof() on most compilers#23016
seberg merged 2 commits intonumpy:mainfrom
kraj:alignof

Conversation

@kraj
Copy link
Copy Markdown
Contributor

@kraj kraj commented Jan 15, 2023

WG14 N2350 made very clear that it is an UB having type definitions within "offsetof" [1]. This patch enhances the implementation of macro _ALIGN to use builtin "_Alignof" to avoid undefined behavior on when using std=c11 or newer

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+

Older compilers gcc < 4.9 or clang < 8 has buggy _Alignof even though it may support C11, exclude those compilers too

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm [2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj raj.khem@gmail.com

WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch enhances the implementation of macro
_ALIGN to use builtin "_Alignof" to avoid undefined behavior on
when using std=c11 or newer

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+

Older compilers gcc < 4.9 or clang < 8 has buggy _Alignof even though it
may support C11, exclude those compilers too

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj <raj.khem@gmail.com>
@mattip
Copy link
Copy Markdown
Member

mattip commented Jan 16, 2023

It seems this is a duplicate of #23008 or am I missing something?

@seberg
Copy link
Copy Markdown
Member

seberg commented Jan 16, 2023

This one has a bug mitigation, @hawkinsp does this seem right to you to include the mitigation? (Also maybe it is interesting to you.)

@hawkinsp
Copy link
Copy Markdown
Contributor

@seberg This looks like a better fix than mine. Let's merge this one (and please add it to the backport list, if you wouldn't mind).

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Jan 16, 2023
@hawkinsp
Copy link
Copy Markdown
Contributor

By the way, I'm not sure if NumPy specifies a minimum set of compilers, but it might make sense to do so because then you have a way to determine if you need this kind of workaround. I'm thinking of something along the lines of the LLVM minimum compiler versions: https://llvm.org/docs/GettingStarted.html#host-c-toolchain-both-compiler-and-standard-library

Copy link
Copy Markdown
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, two nits (could also prettify the rest, but don't know how and its fine). May just apply and merge later.

@seberg seberg changed the title Define _ALIGN using _Alignof when using C11 or newer BUG: use _Alignof rather than offsetof() on most compilers Jan 16, 2023
@seberg seberg merged commit 90e233a into numpy:main Jan 17, 2023
@seberg
Copy link
Copy Markdown
Member

seberg commented Jan 17, 2023

Thanks @kraj and @hawkinsp.

@charris charris added this to the 1.24.2 release milestone Jan 17, 2023
charris pushed a commit to charris/numpy that referenced this pull request Jan 17, 2023
…#23016)

WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch enhances the implementation of macro
_ALIGN to use builtin "_Alignof" to avoid undefined behavior on
when using std=c11 or newer

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+

Older compilers gcc < 4.9 or clang < 8 has buggy _Alignof even though it
may support C11, exclude those compilers too

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj <raj.khem@gmail.com>

* Apply suggestions from code review

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 17, 2023
@charris charris removed this from the 1.24.2 release milestone Jan 17, 2023
@charris charris changed the title BUG: use _Alignof rather than offsetof() on most compilers BUG: use _Alignof rather than offsetof() on most compilers Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants