]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::deque::emplace calling wrong _M_insert_aux [PR90389]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 17 Dec 2024 17:38:43 +0000 (17:38 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 8 Jan 2025 12:45:37 +0000 (12:45 +0000)
We have several overloads of std::deque::_M_insert_aux, one of which is
variadic and called by std::deque::emplace. With a suitable set of
arguments to emplace, it's possible for one of the non-variadic
_M_insert_aux overloads to be selected by overload resolution, making
emplace ill-formed.

Rename the variadic _M_insert_aux to _M_emplace_aux so that calls to
emplace never select an _M_insert_aux overload. Also add an inline
_M_insert_aux for the const lvalue overload that is called from
insert(const_iterator, const value_type&).

libstdc++-v3/ChangeLog:

PR libstdc++/90389
* include/bits/deque.tcc (_M_insert_aux): Rename variadic
overload to _M_emplace_aux.
* include/bits/stl_deque.h (_M_insert_aux): Define inline.
(_M_emplace_aux): Declare.
* testsuite/23_containers/deque/modifiers/emplace/90389.cc: New
test.

libstdc++-v3/include/bits/deque.tcc
libstdc++-v3/include/bits/stl_deque.h
libstdc++-v3/testsuite/23_containers/deque/modifiers/emplace/90389.cc [new file with mode: 0644]

index 42dfa6a37aedefe05c5206e1ffc454cbc95704ae..fcbecca55b4fce62c88fae70833164226b7678a3 100644 (file)
@@ -200,8 +200,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
            return __tmp;
          }
        else
-         return _M_insert_aux(__position._M_const_cast(),
-                              std::forward<_Args>(__args)...);
+         return _M_emplace_aux(__position._M_const_cast(),
+                               std::forward<_Args>(__args)...);
       }
 #endif
 
@@ -646,7 +646,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     template<typename... _Args>
       typename deque<_Tp, _Alloc>::iterator
       deque<_Tp, _Alloc>::
-      _M_insert_aux(iterator __pos, _Args&&... __args)
+      _M_emplace_aux(iterator __pos, _Args&&... __args)
       {
        value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
 #else
index 225fac36e2bf4e9132beaf785e252df5fa845e55..69367140c8e7bb78f9491d15bbdc2bce703dbeaf 100644 (file)
@@ -2059,9 +2059,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       iterator
       _M_insert_aux(iterator __pos, const value_type& __x);
 #else
+      iterator
+      _M_insert_aux(iterator __pos, const value_type& __x)
+      { return _M_emplace_aux(__pos, __x); }
+
       template<typename... _Args>
        iterator
-       _M_insert_aux(iterator __pos, _Args&&... __args);
+       _M_emplace_aux(iterator __pos, _Args&&... __args);
 #endif
 
       // called by insert(p,n,x) via fill_insert
diff --git a/libstdc++-v3/testsuite/23_containers/deque/modifiers/emplace/90389.cc b/libstdc++-v3/testsuite/23_containers/deque/modifiers/emplace/90389.cc
new file mode 100644 (file)
index 0000000..b493249
--- /dev/null
@@ -0,0 +1,43 @@
+// { dg-do run { target c++11 } }
+
+// Bug 90389 - std::deque::emplace tries to call wrong overload internally
+
+#include <deque>
+#include <testsuite_hooks.h>
+
+struct X
+{
+  X() = default;
+  X(void*, void*, std::size_t) { }
+};
+
+void
+test_pr90389()
+{
+  const int n = 3;
+  std::deque<X> d(n);
+  d.emplace(d.begin(), nullptr, nullptr, d.size());
+  VERIFY( d.size() == n+1 );
+}
+
+struct Y
+{
+  Y() = default;
+  Y(std::size_t, const Y&) { }
+};
+
+void
+test_pr118079()
+{
+  const int n = 3;
+  std::deque<Y> d(n);
+  const Y y{};
+  d.emplace(d.begin(), d.size(), y);
+  VERIFY( d.size() == n+1 );
+}
+
+int main()
+{
+  test_pr90389();
+  test_pr118079();
+}