diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -1549,6 +1549,11 @@ inline void __init(size_type __n, value_type __c); + // Slow path for the (inlined) copy constructor. + // Always externally instantiated and not inlined. + // Asserts that __s is zero terminated. + void __init_copy_ctor_external(const value_type* __s, size_type __sz); + template inline _EnableIf @@ -1840,7 +1845,9 @@ if (!__str.__is_long()) __r_.first().__r = __str.__r_.first().__r; else - __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size()); + __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()), + __str.__get_long_size()); + #if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif @@ -1854,7 +1861,8 @@ if (!__str.__is_long()) __r_.first().__r = __str.__r_.first().__r; else - __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size()); + __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()), + __str.__get_long_size()); #if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); #endif @@ -1880,6 +1888,25 @@ #endif } +template +void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external( + const value_type* __s, size_type __sz) { + pointer __p; + if (__sz >= __min_cap) { + if (__sz > max_size()) + this->__throw_length_error(); + size_t __cap = __recommend(__sz); + __p = __alloc_traits::allocate(__alloc(), __cap + 1); + __set_long_pointer(__p); + __set_long_cap(__cap + 1); + __set_long_size(__sz); + } else { + __p = __get_short_pointer(); + __set_short_size(__sz); + } + traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1); +} + template inline basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a) diff --git a/libcxx/test/std/strings/basic.string/string.cons/copy_shrunk_long.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/copy_shrunk_long.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/string.cons/copy_shrunk_long.pass.cpp @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// + +// basic_string(const basic_string& str); + +#include +#include + +#include "test_macros.h" +#include "test_allocator.h" +#include "min_allocator.h" + +template +void +test() +{ + // Tests that a long string holding a SSO size string results in + // an SSO copy constructed value. + S s1("1234567890123456789012345678901234567890123456789012345678901234567890"); + s1.resize(7); + S s2(s1); + LIBCPP_ASSERT(s2.__invariants()); + assert(s2 == s1); + assert(s2.capacity() < sizeof(S)); +} + +int main(int, char**) +{ + { + typedef test_allocator A; + typedef std::basic_string, A> S; + test(); + } +#if TEST_STD_VER >= 11 + { + typedef min_allocator A; + typedef std::basic_string, A> S; + test(); + } +#endif + + return 0; +}