Skip to content

Commit

Permalink
Fix alignment issue with heap_string
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 11, 2023
1 parent b5f5958 commit d31dda3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions include/jsoncons/detail/heap_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*>(~(alignment - 1) &
return reinterpret_cast<char*>(~(alignment - 1) &
(reinterpret_cast<uintptr_t>(ptr) + alignment - 1));
}

Expand Down Expand Up @@ -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);

Expand All @@ -154,6 +154,7 @@ namespace detail {
p[length] = 0;
ps->p_ = std::pointer_traits<typename heap_string_type::pointer>::pointer_to(*p);
ps->length_ = length;
ps->offset_ = (uint16_t)(q - storage);
return std::pointer_traits<pointer>::pointer_to(*ps);
}

Expand Down
4 changes: 3 additions & 1 deletion test/corelib/src/detail/heap_string_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>());

heap_string_factory_type::destroy(ptr);
}


0 comments on commit d31dda3

Please sign in to comment.