fix UBSan false positives with captureless lambdas in C++20#666
Merged
kris-jusiak merged 1 commit intoMay 23, 2026
Merged
Conversation
|
which AI agent are you using for these PRs? |
Contributor
Author
Claude. |
Might want to leave in the "Co-authored-by" for future ref. |
Contributor
Author
We are using this library in production so it's my duty to make sure its well maintained. |
Two changes:
1. zero_wrapper_impl::operator(): in C++20, captureless lambdas are
default-constructible so we can call TExpr{}(args...) directly
instead of the reinterpret_cast<const TExpr&>(*this) hack.
The reinterpret_cast violates strict aliasing and triggers
-fsanitize=undefined "insufficient space" when the wrapper has
zero size (via __BOOST_SML_ZERO_SIZE_ARRAY). In C++14/17 the
existing approach is preserved.
2. transition(always, ...): pass always by value instead of
const-reference. always{} temporaries bound to const& trigger
the same UBSan check when the optimizer recycles their stack slot.
Since always has no state, passing by value is equivalent.
Fixes boost-ext#627
05a7487 to
b2a8b9f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two sources of
-fsanitize=undefinednoise in C++20 builds:1.
zero_wrapper_impl::operator()The existing implementation uses
reinterpret_cast<const TExpr&>(*this)to call a captureless lambda stored in a zero-size wrapper. This violates strict aliasing and triggers UBSan "reference binding with insufficient space" because the wrapper has zero size (via__BOOST_SML_ZERO_SIZE_ARRAY).In C++20, captureless lambdas are default-constructible, so
TExpr{}(args...)works directly — no cast needed, no UBSan. The C++14/17 path is unchanged.2.
transition(always, ...)constructorsalways{}temporaries passed asconst always&trigger the same UBSan check when the optimizer recycles the stack slot. Sincealwaysis stateless, passing by value is equivalent and avoids the reference binding.Reproducer: compile
example/data.cppwith-std=c++20 -fsanitize=undefined -O1.Fixes #627
Co-authored-by claude.