public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10231] libstdc++: Ensure active union member is correctly set Date: Mon, 18 Mar 2024 14:04:12 +0000 (GMT) [thread overview] Message-ID: <20240318140412.ED7663858438@sourceware.org> (raw) https://gcc.gnu.org/g:bfe05fbb30594b9f1fdfba32f3fc7b317e5158e0 commit r12-10231-gbfe05fbb30594b9f1fdfba32f3fc7b317e5158e0 Author: Nathaniel Shead <nathanieloshead@gmail.com> Date: Fri Sep 29 10:30:41 2023 +0100 libstdc++: Ensure active union member is correctly set This patch ensures that the union members for std::string and std::variant are always properly set when a change occurs. libstdc++-v3/ChangeLog: * include/bits/basic_string.h: (basic_string(basic_string&&)): Activate _M_local_buf when needed. (basic_string(basic_string&&, const _Alloc&)): Likewise. * include/bits/basic_string.tcc: (basic_string::swap): Likewise. * include/std/variant: (__detail::__variant::__construct_n): New. (__detail::__variant::__emplace): Use __construct_n. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> (cherry picked from commit 28adad7a32ed92a3c0906b38411c4ed686bbacc5) Diff: --- libstdc++-v3/include/bits/basic_string.h | 6 ++++-- libstdc++-v3/include/bits/basic_string.tcc | 4 ++++ libstdc++-v3/include/std/variant | 32 ++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 9a6fe883b6c..3f38f20dd18 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -680,6 +680,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { if (__str._M_is_local()) { + (void)_M_use_local_data(); traits_type::copy(_M_local_buf, __str._M_local_buf, __str.length() + 1); } @@ -693,7 +694,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 // basic_stringbuf relies on writing into unallocated capacity so // we mess up the contents if we put a '\0' in the string. _M_length(__str.length()); - __str._M_data(__str._M_local_data()); + __str._M_data(__str._M_use_local_data()); __str._M_set_length(0); } @@ -719,6 +720,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { if (__str._M_is_local()) { + (void)_M_use_local_data(); traits_type::copy(_M_local_buf, __str._M_local_buf, __str.length() + 1); _M_length(__str.length()); @@ -730,7 +732,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _M_data(__str._M_data()); _M_length(__str.length()); _M_capacity(__str._M_allocated_capacity); - __str._M_data(__str._M_local_buf); + __str._M_data(__str._M_use_local_data()); __str._M_set_length(0); } else diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 48fa28e6466..24465dab102 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -79,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } else if (__s.length()) { + (void)_M_use_local_data(); traits_type::copy(_M_local_buf, __s._M_local_buf, __s.length() + 1); _M_length(__s.length()); @@ -87,6 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } else if (length()) { + (void)__s._M_use_local_data(); traits_type::copy(__s._M_local_buf, _M_local_buf, length() + 1); __s._M_length(length()); @@ -97,6 +99,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else { const size_type __tmp_capacity = __s._M_allocated_capacity; + (void)__s._M_use_local_data(); traits_type::copy(__s._M_local_buf, _M_local_buf, length() + 1); _M_data(__s._M_data()); @@ -108,6 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const size_type __tmp_capacity = _M_allocated_capacity; if (__s._M_is_local()) { + (void)_M_use_local_data(); traits_type::copy(_M_local_buf, __s._M_local_buf, __s.length() + 1); __s._M_data(_M_data()); diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 2e784c744d3..64145da12bb 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -321,6 +321,33 @@ namespace __variant __get(_Variant&& __v) noexcept { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); } + // Gets the _Uninitialized to construct into for __u. + template<size_t _Np, typename _Union> + constexpr decltype(auto) + __construct_n(_Union& __u) noexcept + { + if constexpr (_Np == 0) + return &__u._M_first; + else if constexpr (_Np == 1) + { + std::_Construct(&__u._M_rest); + return &__u._M_rest._M_first; + } + else if constexpr (_Np == 2) + { + std::_Construct(&__u._M_rest); + std::_Construct(&__u._M_rest._M_rest); + return &__u._M_rest._M_rest._M_first; + } + else + { + std::_Construct(&__u._M_rest); + std::_Construct(&__u._M_rest._M_rest); + std::_Construct(&__u._M_rest._M_rest._M_rest); + return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest); + } + } + template<typename... _Types> struct _Traits { @@ -537,8 +564,9 @@ namespace __variant __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args) { __v._M_reset(); - auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u)); - std::_Construct(__addr, std::forward<_Args>(__args)...); + auto* __addr = __variant::__construct_n<_Np>(__v._M_u); + std::_Construct(__addr, in_place_index<0>, + std::forward<_Args>(__args)...); // Construction didn't throw, so can set the new index now: __v._M_index = _Np; }
reply other threads:[~2024-03-18 14:04 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240318140412.ED7663858438@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).