From 530b95620f39f99d20b234e7e1d39afa5cf3e2e7 Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Wed, 11 Oct 2023 15:22:22 -0400 Subject: [PATCH] Fix alignment issue with heap_string --- include/jsoncons/detail/heap_string.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/jsoncons/detail/heap_string.hpp b/include/jsoncons/detail/heap_string.hpp index af70199a3a..265ae7d29e 100644 --- a/include/jsoncons/detail/heap_string.hpp +++ b/include/jsoncons/detail/heap_string.hpp @@ -69,7 +69,7 @@ namespace detail { Extra extra() const { return this->extra_; } heap_string(Extra extra, const Allocator& alloc) - : heap_string_base(extra, alloc), p_(nullptr), length_(0) + : heap_string_base(extra, alloc), p_(nullptr), length_(0), offset_(0) { } @@ -145,6 +145,8 @@ namespace detail { char* storage = align_up(q, align); + JSONCONS_ASSERT(storage >= q); + heap_string_type* ps = new(storage)heap_string_type(extra, byte_alloc); auto psa = launder_cast(storage); @@ -154,7 +156,7 @@ namespace detail { p[length] = 0; ps->p_ = std::pointer_traits::pointer_to(*p); ps->length_ = length; - ps->offset_ = (uint16_t)(q - storage); + ps->offset_ = (uint16_t)(storage - q); return std::pointer_traits::pointer_to(*ps); }