From 2bd0f173113ab286c55c94f4867af3965797de0d Mon Sep 17 00:00:00 2001 From: redi Date: Thu, 17 Oct 2019 14:21:27 +0000 Subject: [PATCH] PR libstdc++/92124 fix incorrect container move assignment The container requirements say that for move assignment "All existing elements of [the target] are either move assigned or destroyed". Some of our containers currently use __make_move_if_noexcept which makes the move depend on whether the element type is nothrow move constructible. This is incorrect, because the standard says we must move assign, not move or copy depending on the move constructor. Use make_move_iterator instead so that we move unconditionally. This ensures existing elements won't be copy assigned. PR libstdc++/92124 * include/bits/forward_list.h (_M_move_assign(forward_list&&, false_type)): Do not use __make_move_if_noexcept, instead move unconditionally. * include/bits/stl_deque.h (_M_move_assign2(deque&&, false_type)): Likewise. * include/bits/stl_list.h (_M_move_assign(list&&, false_type)): Likewise. * include/bits/stl_vector.h (_M_move_assign(vector&&, false_type)): Likewise. * testsuite/23_containers/vector/92124.cc: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@277113 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 14 ++++++ libstdc++-v3/include/bits/forward_list.h | 4 +- libstdc++-v3/include/bits/stl_deque.h | 4 +- libstdc++-v3/include/bits/stl_list.h | 4 +- libstdc++-v3/include/bits/stl_vector.h | 5 +- .../testsuite/23_containers/deque/92124.cc | 49 +++++++++++++++++++ .../23_containers/forward_list/92124.cc | 49 +++++++++++++++++++ .../testsuite/23_containers/list/92124.cc | 49 +++++++++++++++++++ .../testsuite/23_containers/vector/92124.cc | 49 +++++++++++++++++++ 9 files changed, 219 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/deque/92124.cc create mode 100644 libstdc++-v3/testsuite/23_containers/forward_list/92124.cc create mode 100644 libstdc++-v3/testsuite/23_containers/list/92124.cc create mode 100644 libstdc++-v3/testsuite/23_containers/vector/92124.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c95aa32120b4..4c268795fdd1 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2019-10-17 Jonathan Wakely + + PR libstdc++/92124 + * include/bits/forward_list.h + (_M_move_assign(forward_list&&, false_type)): Do not use + __make_move_if_noexcept, instead move unconditionally. + * include/bits/stl_deque.h (_M_move_assign2(deque&&, false_type)): + Likewise. + * include/bits/stl_list.h (_M_move_assign(list&&, false_type)): + Likewise. + * include/bits/stl_vector.h (_M_move_assign(vector&&, false_type)): + Likewise. + * testsuite/23_containers/vector/92124.cc: New test. + 2019-10-16 Jonathan Wakely * include/bits/c++config (_GLIBCXX_BUILTIN_IS_SAME_AS): Define to diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h index e686283a4324..cab2ae788a75 100644 --- a/libstdc++-v3/include/bits/forward_list.h +++ b/libstdc++-v3/include/bits/forward_list.h @@ -1336,8 +1336,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER else // The rvalue's allocator cannot be moved, or is not equal, // so we need to individually move each element. - this->assign(std::__make_move_if_noexcept_iterator(__list.begin()), - std::__make_move_if_noexcept_iterator(__list.end())); + this->assign(std::make_move_iterator(__list.begin()), + std::make_move_iterator(__list.end())); } // Called by assign(_InputIterator, _InputIterator) if _Tp is diff --git a/libstdc++-v3/include/bits/stl_deque.h b/libstdc++-v3/include/bits/stl_deque.h index ac76d681ff00..50491e76ff5e 100644 --- a/libstdc++-v3/include/bits/stl_deque.h +++ b/libstdc++-v3/include/bits/stl_deque.h @@ -2256,8 +2256,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { // The rvalue's allocator cannot be moved and is not equal, // so we need to individually move each element. - _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()), - std::__make_move_if_noexcept_iterator(__x.end()), + _M_assign_aux(std::make_move_iterator(__x.begin()), + std::make_move_iterator(__x.end()), std::random_access_iterator_tag()); __x.clear(); } diff --git a/libstdc++-v3/include/bits/stl_list.h b/libstdc++-v3/include/bits/stl_list.h index 701982538dfe..328a79851a81 100644 --- a/libstdc++-v3/include/bits/stl_list.h +++ b/libstdc++-v3/include/bits/stl_list.h @@ -1957,8 +1957,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 else // The rvalue's allocator cannot be moved, or is not equal, // so we need to individually move each element. - _M_assign_dispatch(std::__make_move_if_noexcept_iterator(__x.begin()), - std::__make_move_if_noexcept_iterator(__x.end()), + _M_assign_dispatch(std::make_move_iterator(__x.begin()), + std::make_move_iterator(__x.end()), __false_type{}); } #endif diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index d33e589498a9..ff08b2666922 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1828,8 +1828,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { // The rvalue's allocator cannot be moved and is not equal, // so we need to individually move each element. - this->assign(std::__make_move_if_noexcept_iterator(__x.begin()), - std::__make_move_if_noexcept_iterator(__x.end())); + this->_M_assign_aux(std::make_move_iterator(__x.begin()), + std::make_move_iterator(__x.end()), + std::random_access_iterator_tag()); __x.clear(); } } diff --git a/libstdc++-v3/testsuite/23_containers/deque/92124.cc b/libstdc++-v3/testsuite/23_containers/deque/92124.cc new file mode 100644 index 000000000000..6f8cf5560c16 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/deque/92124.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +struct X { + X() = default; + X(const X&) = default; + + // Move constructor might throw + X(X&&) noexcept(false) {} + + // Tracking calls to assignment functions + X& operator=(const X&) { throw 1; } + + X& operator=(X&&) noexcept(true) { return *this; } +}; + +void +test01() +{ + using A = __gnu_test::propagating_allocator; + A a1(1), a2(2); + std::deque v1(1, a1), v2(1, a2); + v1 = std::move(v2); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/92124.cc b/libstdc++-v3/testsuite/23_containers/forward_list/92124.cc new file mode 100644 index 000000000000..52a28073daf2 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/forward_list/92124.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +struct X { + X() = default; + X(const X&) = default; + + // Move constructor might throw + X(X&&) noexcept(false) {} + + // Tracking calls to assignment functions + X& operator=(const X&) { throw 1; } + + X& operator=(X&&) noexcept(true) { return *this; } +}; + +void +test01() +{ + using A = __gnu_test::propagating_allocator; + A a1(1), a2(2); + std::forward_list v1(1, a1), v2(1, a2); + v1 = std::move(v2); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/23_containers/list/92124.cc b/libstdc++-v3/testsuite/23_containers/list/92124.cc new file mode 100644 index 000000000000..117cb71201bb --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/list/92124.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +struct X { + X() = default; + X(const X&) = default; + + // Move constructor might throw + X(X&&) noexcept(false) {} + + // Tracking calls to assignment functions + X& operator=(const X&) { throw 1; } + + X& operator=(X&&) noexcept(true) { return *this; } +}; + +void +test01() +{ + using A = __gnu_test::propagating_allocator; + A a1(1), a2(2); + std::list v1(1, a1), v2(1, a2); + v1 = std::move(v2); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/92124.cc b/libstdc++-v3/testsuite/23_containers/vector/92124.cc new file mode 100644 index 000000000000..3cb487d39f46 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/92124.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +struct X { + X() = default; + X(const X&) = default; + + // Move constructor might throw + X(X&&) noexcept(false) {} + + // Tracking calls to assignment functions + X& operator=(const X&) { throw 1; } + + X& operator=(X&&) noexcept(true) { return *this; } +}; + +void +test01() +{ + using A = __gnu_test::propagating_allocator; + A a1(1), a2(2); + std::vector v1(1, a1), v2(1, a2); + v1 = std::move(v2); +} + +int +main() +{ + test01(); +} -- 2.47.2