diff -r b66532b5d8db -r 6f9d97a53d67 test/test_allocator.cpp --- a/test/test_allocator.cpp Sat Apr 16 08:59:51 2022 +0200 +++ b/test/test_allocator.cpp Sat Apr 16 09:10:10 2022 +0200 @@ -72,3 +72,21 @@ cxFree(cxDefaultAllocator, test); ); } + +TEST(Allocator, FailingReallocate) { + // Mock an allocator that always returns nullptr on realloc + cx_allocator_class mock_cl; + mock_cl.realloc = [](void* p, void* d, size_t n) -> void* { return nullptr; }; + cx_allocator_s mock{&mock_cl, nullptr}; + + void *test = calloc(8, 1); + memcpy(test, "Test", 5); + void *original = test; + int ret = cxReallocate(&mock, &test, 16); + // non-zero return code because of the failure + EXPECT_NE(ret, 0); + // the test pointer was not changed and still points to the same memory + EXPECT_EQ(test, original); + EXPECT_STREQ(reinterpret_cast(test), "Test"); + free(test); +}