From d31dda395ccc52ea541e0e4ab361d13ba9f10324 Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Wed, 11 Oct 2023 15:07:16 -0400 Subject: [PATCH] Fix alignment issue with heap_string --- include/jsoncons/detail/heap_string.hpp | 9 +++++---- test/corelib/src/detail/heap_string_tests.cpp | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/jsoncons/detail/heap_string.hpp b/include/jsoncons/detail/heap_string.hpp index f1ee97a1dd..af70199a3a 100644 --- a/include/jsoncons/detail/heap_string.hpp +++ b/include/jsoncons/detail/heap_string.hpp @@ -18,10 +18,10 @@ namespace jsoncons { namespace detail { - inline void* - align_up(void* ptr, std::size_t alignment) noexcept + inline char* + align_up(char* ptr, std::size_t alignment) noexcept { - return reinterpret_cast(~(alignment - 1) & + return reinterpret_cast(~(alignment - 1) & (reinterpret_cast(ptr) + alignment - 1)); } @@ -143,7 +143,7 @@ namespace detail { char* q = extension_traits::to_plain_pointer(ptr); - void* storage = align_up(q, align); + char* storage = align_up(q, align); heap_string_type* ps = new(storage)heap_string_type(extra, byte_alloc); @@ -154,6 +154,7 @@ namespace detail { p[length] = 0; ps->p_ = std::pointer_traits::pointer_to(*p); ps->length_ = length; + ps->offset_ = (uint16_t)(q - storage); return std::pointer_traits::pointer_to(*ps); } diff --git a/test/corelib/src/detail/heap_string_tests.cpp b/test/corelib/src/detail/heap_string_tests.cpp index f81f88aeb6..91ed44eddd 100644 --- a/test/corelib/src/detail/heap_string_tests.cpp +++ b/test/corelib/src/detail/heap_string_tests.cpp @@ -16,9 +16,11 @@ using pointer = typename heap_string_factory_type::pointer; TEST_CASE("heap_string test") { - std::string s("Hello World"); + std::string s("String too long for short string"); pointer ptr = heap_string_factory_type::create(s.data(), s.length(), null_type(), std::allocator()); + + heap_string_factory_type::destroy(ptr); }