diff --git a/include/ylt/struct_pack/util.h b/include/ylt/struct_pack/util.h index ec9218c4c..58ecdb063 100644 --- a/include/ylt/struct_pack/util.h +++ b/include/ylt/struct_pack/util.h @@ -185,7 +185,6 @@ void string_set_length_hacker(std::string &, std::size_t); template inline void resize(std::basic_string &raw_str, std::size_t sz) { - std::string &str = *reinterpret_cast(&raw_str); #if defined(_GLIBCXX_USE_CXX11_ABI) constexpr bool is_use_cxx11_abi = _GLIBCXX_USE_CXX11_ABI; #else @@ -204,14 +203,15 @@ inline void resize(std::basic_string &raw_str, std::size_t sz) { defined(_MSVC_STL_VERSION) if constexpr (is_string_reserve_shrink) { if (sz > raw_str.capacity()) { - str.reserve(sz * sizeof(ch)); + raw_str.reserve(sz); } } else { - str.reserve(sz * sizeof(ch)); + raw_str.reserve(sz); } + std::string &str = *reinterpret_cast(&raw_str); string_set_length_hacker(str, sz); - for (auto i = sz; i < sz + sizeof(ch); ++i) str[i] = '\0'; + *(raw_str.data() + sz) = 0; #else raw_str.resize(sz); #endif