Skip to content

Fix issue #274 UBSan Error constructor call with insufficient space for an object of type 'node_t'#323

Merged
arximboldi merged 3 commits into
arximboldi:masterfrom
mongodb-forks:immer-issue-274
Jan 29, 2026
Merged

Fix issue #274 UBSan Error constructor call with insufficient space for an object of type 'node_t'#323
arximboldi merged 3 commits into
arximboldi:masterfrom
mongodb-forks:immer-issue-274

Conversation

@atesteve
Copy link
Copy Markdown
Contributor

@atesteve atesteve commented Jan 26, 2026

This PR adds the CRTP struct with_trailing_storage. Any standard layout struct can inherit from it to be able to use the storage immediately after it to store objects of a given type. For efficiency and to accommodate other immer facilities, there is a special case for an empty base class.

Copy link
Copy Markdown
Owner

@arximboldi arximboldi left a comment

Choose a reason for hiding this comment

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

Super nice, thank you! There are some comments inline with some small requests, could you please take care of those?

Comment thread immer/detail/util.hpp Outdated
typename aligned_storage<sizeof(T), alignof(T)>::type;

template <typename Base, typename T, bool EmptyBase = false>
struct has_trailing_storage;
Copy link
Copy Markdown
Owner

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 the with_ prefix, as in:

struct with_trailing_storage;

Copy link
Copy Markdown
Owner

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, with_trailing_storage is 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 original aligned_storage solution.

Comment thread immer/detail/util.hpp Outdated
template <typename Base, typename T, bool EmptyBase = false>
struct has_trailing_storage;

template <typename Base, typename T>
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Nitpick: Base should be rename the Deriv, as it is not the base clase but the class that derives from this (technically, with_trailing_storage is the base class of Deriv as usual in CRTP)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Haha true, I did it completely backwards. Fixed.

Comment thread immer/detail/util.hpp Outdated
static constexpr void check_base()
{
static_assert(std::is_standard_layout<Base>::value,
"Please remove 'true' if the base class is non emtpy");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Pretty neat trick to discover when this still holds :)

@atesteve atesteve requested a review from arximboldi January 27, 2026 09:43
@arximboldi
Copy link
Copy Markdown
Owner

Thank you for the name change and the comments. This is looking great; thank you so much! Merging :)

@atesteve
Copy link
Copy Markdown
Contributor Author

Hello @arximboldi, just to clarify, did you forget to actually merge or is there some kind of schedule?

@arximboldi
Copy link
Copy Markdown
Owner

Sorry, was waiting for CI and then forgot. It works (the failing test is because you're a third-party contributor it doesn't expose secrets, I should fix the actions to ignore those steps in that case). Merging!

@arximboldi arximboldi merged commit 7181330 into arximboldi:master Jan 29, 2026
39 of 41 checks passed
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