Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: fix UB taking pointer to inner type T #6

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

johannst
Copy link
Contributor

@johannst johannst commented Apr 9, 2024

Casting the pointer Storage<T>::value_ to T is undefined according to the standard [basic.life/8], as it violates item (8.2), since the types are not equal [1].

Therefore, we need to launder the pointer after reinterpreting it.

See also the examples in std::launder[2] and std::aligned_storage [3].

Not sure I follow your style correctly, please let me know.

Best

[1] https://timsong-cpp.github.io/cppwp/n4868/basic.life#8
[2] https://en.cppreference.com/w/cpp/utility/launder
[3] https://en.cppreference.com/w/cpp/types/aligned_storage
[4] https://timsong-cpp.github.io/cppwp/n4868/ptr.launder

johannst added 2 commits April 9, 2024 20:58
Casting the pointer Storage<T>::value_ to T is undefined according to
the standard [basic.life/8], as it violates item (8.2), since the types
are not equal [1].

Therefore, we need to launder the pointer after reinterpreting it.

See also the examples in std::launder[2] and std::aligned_storage [3].

[1] https://timsong-cpp.github.io/cppwp/n4868/basic.life#8
[2] https://en.cppreference.com/w/cpp/utility/launder
[3] https://en.cppreference.com/w/cpp/types/aligned_storage
[4] https://timsong-cpp.github.io/cppwp/n4868/ptr.launder
@TheNumbat
Copy link
Owner

Good timing, I was just thinking I should add a std::launder equivalent. I'll also add the launder in Variant.
Style looks fine (though I'm not sure how the existing code ended up using protected...)

@TheNumbat TheNumbat merged commit 9791bc8 into TheNumbat:main Apr 9, 2024
4 of 5 checks passed
@TheNumbat
Copy link
Owner

Oh, it seems MSVC doesn't expose __is_function. Will also fix that.

@johannst
Copy link
Contributor Author

johannst commented Apr 9, 2024

Forgot the poor variant again, out of sight out of mind I guess :^)

Style looks fine (though I'm not sure how the existing code ended up using protected...)

In the Storage class? If by accident, then we can also mark it private.

Oh, it seems MSVC doesn't expose __is_function. Will also fix that.

Oops, didn't have a machine at hand. Probably should have checked before. Thanks for fixing.

@TheNumbat
Copy link
Owner

Yeah, I just got rid of the protected.
Normally the CI build would catch errors from MSVC, but the GitHub runners don't have the latest compiler version yet.

@johannst
Copy link
Contributor Author

johannst commented Apr 9, 2024

A small note, __is_void() also checks for cv qualified void, whereas the new Is_Void trait does not.

@TheNumbat
Copy link
Owner

Ah, good point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants