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

src/secure_allocator.hpp: define missing 'rebind' type #4480

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/secure_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ bool operator!= (const secure_allocator_t<T> &, const secure_allocator_t<U> &)
#else
template <typename T> struct secure_allocator_t : std::allocator<T>
{
secure_allocator_t () ZMQ_DEFAULT;

template <class U>
secure_allocator_t (const secure_allocator_t<U> &) ZMQ_NOEXCEPT
{
}

template <class U> struct rebind
Copy link

@jwakely jwakely Dec 20, 2022

Choose a reason for hiding this comment

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

Suggested change
template <class U> struct rebind
secure_allocator_t() { }
template<class U> secure_allocator_t(const secure_allocator_t<U>&) { }
template <class U> struct rebind

The error is because you need a converting constructor that can be used to construct Alloc<U> from Alloc<T> and vice versa. This adds it.

The other requirements for an allocator type are value_type, allocate and deallocate, and operator== which can both come from the std::allocator base class.

Copy link

Choose a reason for hiding this comment

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

became special

It didn't.

To rebind an allocator you need to be able to determine the rebound type, and construct an instance of the rebound type from the original type. With the missing rebind the rebound type is std::allocator<U> and that can be constructed from a secure_allocator_t<T> (via slicing). But that's broken.

With the new rebind member the rebound type is correct: secure_allocator_t<U>. But you can't construct that from secure_allocator_t<T>. So you need to add a converting constructor, and then you also need a user-declared default constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! Taken default constructor and conversion constructor definitions from the non-no-op variant as well. I think it is equivalent to the definition you suggested.

{
typedef secure_allocator_t<U> other;
};
};
#endif
}
Expand Down