Copying a std::variant whose alternatives hold a std::string, at -O3 -Wall,
gives a false "may be used uninitialized" warning that points into
basic_string.h.

For the testcase from the PR:

  #include <string>
  #include <variant>
  #include <vector>

  void fails(std::vector<std::variant<int, std::string>>& vec, int n)
  {
      vec.push_back(n);
  }

At -O2 and -O3, GCC warns:

  basic_string.h:1229:19: warning: '*(const std::__cxx11::basic_string<char,
    std::char_traits<char>, std::allocator<char> >*)((char*)&<unnamed> +
    offsetof(std::value_type, [...])).std::__cxx11::basic_string<char>::
    _M_string_length' may be used uninitialized [-Wmaybe-uninitialized]

This happens because the move and copy constructors build the storage
first, and set the index to variant_npos (empty). If building the
alternative after this throws, the destructor reads the index, but the
optimizer can't prove the index is still variant_npos because memcpy into
the string's buffer could've overwritten it.

The change is a new _Variant_storage constructor specifically to build
the alternative and set the index, which the copy and move constructors
call. In these cases, if it throws, the destructor isn't called and
nothing reads _M_index. This only works around the problem; the
optimizer limitation in PR 114592 still exists.

Bootstrapped and tested on x86_64-pc-linux-gnu.

Assisted-by: Claude Opus 5.5

libstdc++-v3/ChangeLog:

        PR tree-optimization/114592
        * include/std/variant (_Variant_box_copy_tag): New.
        (_Variant_storage<false, _Types...>::_Variant_storage): Add
        constructor that copies or moves the contained value from another
        variant.
        (_Variant_storage<true, _Types...>::_Variant_storage): Likewise.
        (_Copy_ctor_base::_Copy_ctor_base): Delegate to new constructor.
        (_Move_ctor_base::_Move_ctor_base): Likewise.
        * testsuite/20_util/variant/114592.cc: New test.

Signed-off-by: Davey Hughes <[email protected]>
---
If it's ok, can trunk backport this to the 14/15/16 branches?

 libstdc++-v3/include/std/variant              | 61 ++++++++++++-------
 .../testsuite/20_util/variant/114592.cc       | 30 +++++++++
 2 files changed, 68 insertions(+), 23 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/variant/114592.cc

diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 6e1388363ca..1bceafd5f4a 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -477,6 +477,8 @@ namespace __variant
   template<bool __trivially_destructible, typename... _Types>
     struct _Variant_storage;

+  struct _Variant_box_copy_tag { };
+
   template <typename... _Types>
     using __select_index =
       typename __select_int::_Select_int_base<sizeof...(_Types),
@@ -498,6 +500,22 @@ namespace __variant
          _M_index{_Np}
        { }

+      template<typename _Rhs>
+       _GLIBCXX20_CONSTEXPR
+       _Variant_storage(_Variant_box_copy_tag, _Rhs&& __rhs)
+       {
+         __variant::__raw_idx_visit(
+           [this](auto&& __rhs_mem, auto __rhs_index) mutable
+           {
+             constexpr size_t __j = __rhs_index;
+             if constexpr (__j != variant_npos)
+               std::_Construct(std::__addressof(this->_M_u),
+                               in_place_index<__j>,
+                               std::forward<decltype(__rhs_mem)>(__rhs_mem));
+           }, __variant_cast<_Types...>(std::forward<_Rhs>(__rhs)));
+         this->_M_index = __rhs._M_index;
+       }
+
       constexpr void
       _M_reset()
       {
@@ -544,6 +562,22 @@ namespace __variant
          _M_index{_Np}
        { }

+      template<typename _Rhs>
+       _GLIBCXX20_CONSTEXPR
+       _Variant_storage(_Variant_box_copy_tag, _Rhs&& __rhs)
+       {
+         __variant::__raw_idx_visit(
+           [this](auto&& __rhs_mem, auto __rhs_index) mutable
+           {
+             constexpr size_t __j = __rhs_index;
+             if constexpr (__j != variant_npos)
+               std::_Construct(std::__addressof(this->_M_u),
+                               in_place_index<__j>,
+                               std::forward<decltype(__rhs_mem)>(__rhs_mem));
+           }, __variant_cast<_Types...>(std::forward<_Rhs>(__rhs)));
+         this->_M_index = __rhs._M_index;
+       }
+
       constexpr void
       _M_reset() noexcept
       { _M_index = static_cast<__index_type>(variant_npos); }
@@ -597,17 +631,8 @@ namespace __variant
       _GLIBCXX20_CONSTEXPR
       _Copy_ctor_base(const _Copy_ctor_base& __rhs)
          noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
-      {
-       __variant::__raw_idx_visit(
-         [this](auto&& __rhs_mem, auto __rhs_index) mutable
-         {
-           constexpr size_t __j = __rhs_index;
-           if constexpr (__j != variant_npos)
-             std::_Construct(std::__addressof(this->_M_u),
-                             in_place_index<__j>, __rhs_mem);
-         }, __variant_cast<_Types...>(__rhs));
-       this->_M_index = __rhs._M_index;
-      }
+      : _Base(_Variant_box_copy_tag{}, __rhs)
+      { }

       _Copy_ctor_base(_Copy_ctor_base&&) = default;
       _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
@@ -634,18 +659,8 @@ namespace __variant
       _GLIBCXX20_CONSTEXPR
       _Move_ctor_base(_Move_ctor_base&& __rhs)
          noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
-      {
-       __variant::__raw_idx_visit(
-         [this](auto&& __rhs_mem, auto __rhs_index) mutable
-         {
-           constexpr size_t __j = __rhs_index;
-           if constexpr (__j != variant_npos)
-             std::_Construct(std::__addressof(this->_M_u),
-                             in_place_index<__j>,
-                             std::forward<decltype(__rhs_mem)>(__rhs_mem));
-         }, __variant_cast<_Types...>(std::move(__rhs)));
-       this->_M_index = __rhs._M_index;
-      }
+      : _Base(_Variant_box_copy_tag{}, std::move(__rhs))
+      { }

       _Move_ctor_base(const _Move_ctor_base&) = default;
       _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
diff --git a/libstdc++-v3/testsuite/20_util/variant/114592.cc 
b/libstdc++-v3/testsuite/20_util/variant/114592.cc
new file mode 100644
index 00000000000..b075b5fdfd2
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/variant/114592.cc
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-O3 -Wall" }
+// PR tree-optimization/114592
+// Bogus maybe-uninitialized on std::variant with std::string with -O3
+
+#include <optional>
+#include <string>
+#include <variant>
+#include <vector>
+
+// Growing the vector moves the variants (testcase from comment 4 of the PR).
+void
+test01(std::vector<std::variant<int, std::string>>& vec, int n)
+{
+  vec.push_back(n);
+}
+
+// If copying the alternative throws, ~_Variant_storage() must not need to
+// check an index that the optimizer cannot prove is still variant_npos.
+struct A { std::string s; };
+struct B { std::string s; std::string t; };
+struct C { std::variant<B, A> v; };
+struct D { std::optional<C> o; };
+struct E { D d; } e;
+
+void
+test02()
+{
+  new E(e);
+}
--
2.56.1

Reply via email to