-
-
Notifications
You must be signed in to change notification settings - Fork 203
Fix issue #274 UBSan Error constructor call with insufficient space for an object of type 'node_t' #323
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
Fix issue #274 UBSan Error constructor call with insufficient space for an object of type 'node_t' #323
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,76 @@ template <typename T> | |
| using aligned_storage_for = | ||
| typename aligned_storage<sizeof(T), alignof(T)>::type; | ||
|
|
||
| template <typename Base, typename T, bool EmptyBase = false> | ||
| struct has_trailing_storage; | ||
|
|
||
| template <typename Base, typename T> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha true, I did it completely backwards. Fixed. |
||
| struct alignas(alignof(T)) has_trailing_storage<Base, T, true> | ||
| { | ||
| using storage_type = T; | ||
|
|
||
| T* get_storage_ptr() | ||
| { | ||
| check_base(); | ||
| return reinterpret_cast<T*>(this); | ||
| } | ||
|
|
||
| const T* get_storage_ptr() const | ||
| { | ||
| check_base(); | ||
| return reinterpret_cast<const T*>(this); | ||
| } | ||
|
|
||
| static constexpr size_t get_storage_offset() | ||
| { | ||
| check_base(); | ||
| return 0; | ||
| } | ||
|
|
||
| private: | ||
| static constexpr void check_base() | ||
| { | ||
| static_assert(std::is_standard_layout<Base>::value, | ||
| "Please remove 'true' if the base class is non emtpy"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty neat trick to discover when this still holds :) |
||
| } | ||
|
|
||
| // Dummy field to make the base class non-empty. | ||
| char _; | ||
| }; | ||
|
|
||
| template <typename Base, typename T> | ||
| struct alignas(alignof(T)) has_trailing_storage<Base, T, false> | ||
| { | ||
| using storage_type = T; | ||
|
|
||
| T* get_storage_ptr() | ||
| { | ||
| check_base(); | ||
| auto* base = static_cast<Base*>(this); | ||
| return reinterpret_cast<T*>(base + 1); | ||
| } | ||
|
|
||
| const T* get_storage_ptr() const | ||
| { | ||
| check_base(); | ||
| auto* base = static_cast<const Base*>(this); | ||
| return reinterpret_cast<const T*>(base + 1); | ||
| } | ||
|
|
||
| static constexpr size_t get_storage_offset() | ||
| { | ||
| check_base(); | ||
| return sizeof(Base); | ||
| } | ||
|
|
||
| private: | ||
| static constexpr void check_base() | ||
| { | ||
| static_assert(std::is_standard_layout<Base>::value && !std::is_empty<Base>::value, | ||
| "Please add 'true' if the base class is emtpy"); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| T& auto_const_cast(const T& x) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: the
has_verb makes it looks like this is a meta-predicate (a boolean returning meta-function). For "mix-ins" of this form I prefer thewith_prefix, as in:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a comment explaining why and how this is needed would be nice to have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed,
with_trailing_storageis a much better name. I added some comments, let me know if you are OK with them or if you wanted to explain why this is needed instead of the originalaligned_storagesolution.