From: Jeffrey Yasskin Date: Thu, 12 Apr 2012 22:26:02 +0000 (+0000) Subject: Fix PR52822 by explicitly checking for object identity before move-assigning. X-Git-Tag: releases/gcc-4.6.4~590 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee23e527ee991f5fbe3033677eedfc9bb0d55c18;p=thirdparty%2Fgcc.git Fix PR52822 by explicitly checking for object identity before move-assigning. Fix PR52822 by explicitly checking for object identity before move-assigning. This is a simpler fix than was committed to 4.7 and 4.8. 2012-04-12 Jeffrey Yasskin PR libstdc++/52822 * include/bits/stl_algo.h (__stable_partition_adaptive): Avoid move-assigning an object to itself by explicitly testing for identity. * testsuite/25_algorithms/stable_partition/pr52822.cc: Test vectors, which have a destructive self-move-assignment. * testsuite/25_algorithms/stable_partition/moveable.cc (test02): Test with rvalstruct, which explicitly checks self-move-assignment. From-SVN: r186396 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 337a676b6410..d8fc4bfbcff9 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2012-04-12 Jeffrey Yasskin + + PR libstdc++/52822 + * include/bits/stl_algo.h (__stable_partition_adaptive): Avoid + move-assigning an object to itself by explicitly testing for + identity. + * testsuite/25_algorithms/stable_partition/pr52822.cc: Test + vectors, which have a destructive self-move-assignment. + * testsuite/25_algorithms/stable_partition/moveable.cc (test02): + Test with rvalstruct, which explicitly checks + self-move-assignment. + 2012-04-09 Terry Guo * testsuite/Makefile.am (TEST_GCC_EXEC_PREFIX): New. diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 8391d3e48541..6e9965ca789a 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -1811,7 +1811,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION for (; __first != __last; ++__first) if (__pred(*__first)) { - *__result1 = _GLIBCXX_MOVE(*__first); + if (__result1 != __first) + *__result1 = _GLIBCXX_MOVE(*__first); ++__result1; } else diff --git a/libstdc++-v3/testsuite/25_algorithms/stable_partition/moveable.cc b/libstdc++-v3/testsuite/25_algorithms/stable_partition/moveable.cc index fe46463df4fb..3fca316c7d9c 100644 --- a/libstdc++-v3/testsuite/25_algorithms/stable_partition/moveable.cc +++ b/libstdc++-v3/testsuite/25_algorithms/stable_partition/moveable.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++0x" } -// Copyright (C) 2009, 2010 Free Software Foundation, Inc. +// Copyright (C) 2009, 2010, 2012 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 @@ -39,6 +39,11 @@ const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; const int B[] = {2, 4, 6, 8, 10, 12, 14, 16, 1, 3, 5, 7, 9, 11, 13, 15, 17}; const int N = sizeof(A) / sizeof(int); +// Check that starting with a true predicate works too. (PR52822) +const int A2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; +const int B2[] = {2, 4, 6, 8, 10, 12, 14, 16, 3, 5, 7, 9, 11, 13, 15, 17}; +const int N2 = sizeof(A2) / sizeof(int); + struct Pred { bool @@ -46,7 +51,7 @@ struct Pred { return (x.val % 2) == 0; } }; -// 25.2.12 stable_partition() +// 25.2.12 stable_partition(), starting with a false predicate. void test01() { @@ -60,9 +65,24 @@ test01() VERIFY( std::equal(s1, s1 + N, B) ); } +// 25.2.12 stable_partition(), starting with a true predicate. +void +test02() +{ + bool test __attribute__((unused)) = true; + + rvalstruct s1[N2]; + std::copy(A2, A2 + N2, s1); + Container con(s1, s1 + N2); + + std::stable_partition(con.begin(), con.end(), Pred()); + VERIFY( std::equal(s1, s1 + N2, B2) ); +} + int main() { test01(); + test02(); return 0; } diff --git a/libstdc++-v3/testsuite/25_algorithms/stable_partition/pr52822.cc b/libstdc++-v3/testsuite/25_algorithms/stable_partition/pr52822.cc new file mode 100644 index 000000000000..c5f95f367791 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/stable_partition/pr52822.cc @@ -0,0 +1,43 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2012 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 Pred 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 +// . + +// 25.2.12 [lib.alg.partitions] Partitions. + +#include +#include +#include + +bool true_vector_pred(const std::vector&) { return true; } + +void +test01() +{ + std::vector > v(1); + v[0].push_back(7); + VERIFY( v[0].size() == 1 ); + std::stable_partition(v.begin(), v.end(), &true_vector_pred); + VERIFY( v[0].size() == 1 ); +} + +int +main() +{ + test01(); + return 0; +}