From 4423f9c6eefb2fcbe96516357965566c10e232f7 Mon Sep 17 00:00:00 2001 From: MaliaLabor Date: Mon, 22 Dec 2025 15:47:30 -0800 Subject: [PATCH 1/3] :bug: Throw exception on memory exhaustion for monotonic_allocator --- modules/strong_ptr.cppm | 4 +++- tests/strong_ptr.test.cpp | 15 ++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/modules/strong_ptr.cppm b/modules/strong_ptr.cppm index 7b73766..951245e 100644 --- a/modules/strong_ptr.cppm +++ b/modules/strong_ptr.cppm @@ -70,6 +70,8 @@ public: * @param p_alignment the desired alignment * @return void* pointer to allocated space or nullptr if no space is * available + * @throws std::bad_alloc if storage of the requested size and alignment + * cannot be obtained */ void* do_allocate(std::size_t p_bytes, std::size_t p_alignment) { @@ -80,7 +82,7 @@ public: m_space -= p_bytes; return result; } - return nullptr; + throw std::bad_alloc(); }; /** diff --git a/tests/strong_ptr.test.cpp b/tests/strong_ptr.test.cpp index 575f960..5b519e4 100644 --- a/tests/strong_ptr.test.cpp +++ b/tests/strong_ptr.test.cpp @@ -349,7 +349,8 @@ boost::ut::suite<"strong_ptr_only_test"> strong_ptr_only_test = []() { }; "get_allocator_for_static_allocation"_test = [&] { - // Create a static object (using int to avoid affecting test_class instance count) + // Create a static object (using int to avoid affecting test_class instance + // count) static int static_obj = 777; // Create strong_ptr to static object using unsafe_assume_static_tag @@ -456,7 +457,6 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() { "max_buffer_test"_test = [&] { auto allocator = monotonic_allocator<8>(); - auto ptr1 = allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t)); auto int_ptr1 = static_cast(ptr1); @@ -467,13 +467,18 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() { auto int_ptr2 = static_cast(ptr2); *int_ptr2 = 2; - [[maybe_unused]] auto ptr3 = - allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t)); expect(that % 1 == *int_ptr1) << "Int assignment failed.\n"; expect(that % 2 == *int_ptr2) << "Int assignment failed.\n"; + + expect(throws([&] { + [[maybe_unused]] auto ptr3 = + allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t)); + })) + << "Exception not thrown when bad alloc happens.\n"; // TODO(#34): fix nullptr check on linux // expect(that % nullptr == ptr3) - // << "Allocated memory out of bounds of buffer " << reinterpret_cast(ptr3); + // << "Allocated memory out of bounds of buffer " << + // reinterpret_cast(ptr3); allocator.deallocate(ptr1, sizeof(std::uint32_t)); allocator.deallocate(ptr2, sizeof(std::uint32_t)); }; From cf338d40c3e8d68957c6955b39bd8671aa5ef5b7 Mon Sep 17 00:00:00 2001 From: MaliaLabor Date: Tue, 23 Dec 2025 08:29:15 -0800 Subject: [PATCH 2/3] remove unecessary TODO --- tests/strong_ptr.test.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/strong_ptr.test.cpp b/tests/strong_ptr.test.cpp index 5b519e4..442cab9 100644 --- a/tests/strong_ptr.test.cpp +++ b/tests/strong_ptr.test.cpp @@ -475,10 +475,7 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() { allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t)); })) << "Exception not thrown when bad alloc happens.\n"; - // TODO(#34): fix nullptr check on linux - // expect(that % nullptr == ptr3) - // << "Allocated memory out of bounds of buffer " << - // reinterpret_cast(ptr3); + allocator.deallocate(ptr1, sizeof(std::uint32_t)); allocator.deallocate(ptr2, sizeof(std::uint32_t)); }; From 09b69163167626d0208bd510acbdae1fd7df0dca Mon Sep 17 00:00:00 2001 From: Malia Labor <99438964+MaliaLabor@users.noreply.github.com> Date: Tue, 23 Dec 2025 11:35:11 -0800 Subject: [PATCH 3/3] Specify exception Co-authored-by: Khalil Estell --- tests/strong_ptr.test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/strong_ptr.test.cpp b/tests/strong_ptr.test.cpp index 442cab9..8662ebe 100644 --- a/tests/strong_ptr.test.cpp +++ b/tests/strong_ptr.test.cpp @@ -470,7 +470,7 @@ boost::ut::suite<"monotonic_allocator_test"> monotonic_allocator_test = []() { expect(that % 1 == *int_ptr1) << "Int assignment failed.\n"; expect(that % 2 == *int_ptr2) << "Int assignment failed.\n"; - expect(throws([&] { + expect(throws([&] { [[maybe_unused]] auto ptr3 = allocator.allocate(sizeof(std::uint32_t), alignof(std::uint32_t)); }))