Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions include/exec/__memory_resource_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <cstddef>
#include <cstring>
#include <limits>
#include <memory>
#include <memory_resource>
#include <new>
Expand Down Expand Up @@ -178,6 +179,10 @@ namespace experimental::execution
// end of the buffer. To make room for the possible rightward shift
// and the copy of a pointer, we need to allocate extra space, and
// __bytes + __align - 1 + sizeof(void *) is the most we might need.
if (__bytes > (std::numeric_limits<std::size_t>::max)() - (__align - 1) - sizeof(void *))
{
STDEXEC_THROW(std::bad_alloc());
}
std::size_t __upstreamSize = __bytes + __align - 1 + sizeof(void *);
void *const __buffer = __traits::allocate(__alloc_, __upstreamSize);

Expand Down
27 changes: 25 additions & 2 deletions test/exec/test_memory_resource_adaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
* limitations under the License.
*/

#include <exec/__memory_resource_adaptor.hpp>

#include <catch2/catch_all.hpp>

#include <stdexec/execution.hpp>

#include <exec/__memory_resource_adaptor.hpp>

#include <bit>
#include <concepts>
#include <limits>
#include <memory>

namespace ex = STDEXEC;
Expand Down Expand Up @@ -85,6 +86,28 @@ namespace
template <std::size_t Alignment>
using alignment = std::integral_constant<std::size_t, Alignment>;

#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
TEMPLATE_TEST_CASE("exec::__memory_resource_adaptor rejects allocation size overflow",
"[types][memory_resource_adaptor]",
custom_allocator<std::byte>,
custom_allocator<int>)
{
using adaptor_t = exec::__memory_resource_adaptor_t<TestType>;

auto underlying = make_underlying_resource<TestType>();
adaptor_t rsc(adaptor_arg_from_resource(&underlying));
std::pmr::polymorphic_allocator<> alloc(&rsc);

auto const align = GENERATE(std::size_t{1}, std::size_t{16}, std::size_t{64});
auto const max_size = (std::numeric_limits<std::size_t>::max)();
auto const bytes = GENERATE_COPY(max_size,
max_size - (align - 1),
max_size - (align - 1) - sizeof(void*) + 1);

CHECK_THROWS_AS(alloc.allocate_bytes(bytes, align), std::bad_alloc);
}
#endif

TEMPLATE_TEST_CASE("exec::__memory_resource_adaptor supports over-alignment",
"[types][memory_resource_adaptor]",
std::allocator<std::byte>,
Expand Down
Loading