From 74ce9f3cfeeef074eb821f85e39fa38c644c9df6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 28 Feb 2024 21:48:58 +1100 Subject: [PATCH] Fix missing destructor call and simplify code slightly Although the destructor call is a no-op, it is more correct to have it to ensure the compiler knows the lifetime of that object ended. --- src/shared_string.hh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/shared_string.hh b/src/shared_string.hh index bb06c7fc0..78e9fef4c 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -37,7 +37,9 @@ private: return; if (r->refcount & interned_flag) Registry::instance().remove(r->strview()); - StringData::operator delete(r, sizeof(StringData) + r->length + 1); + auto alloc_len = sizeof(StringData) + r->length + 1; + r->~StringData(); + operator delete(r, alloc_len); } static void ptr_moved(StringData*, void*, void*) noexcept {} }; @@ -60,11 +62,11 @@ public: static Ptr create(ConvertibleTo auto&&... strs) { const int len = ((int)StringView{strs}.length() + ...); - void* ptr = StringData::operator new(sizeof(StringData) + len + 1); + void* ptr = operator new(sizeof(StringData) + len + 1); auto* res = new (ptr) StringData(len); auto* data = reinterpret_cast(res + 1); auto append = [&](StringView str) { - if (str.empty()) // memccpy(..., nullptr, 0) is UB + if (str.empty()) // memcpy(..., nullptr, 0) is UB return; memcpy(data, str.begin(), (size_t)str.length()); data += (int)str.length();