]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix PR52822 by explicitly checking for object identity before move-assigning.
authorJeffrey Yasskin <jyasskin@google.com>
Thu, 12 Apr 2012 22:26:02 +0000 (22:26 +0000)
committerJeffrey Yasskin <jyasskin@gcc.gnu.org>
Thu, 12 Apr 2012 22:26:02 +0000 (22:26 +0000)
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  <jyasskin@google.com>

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

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_algo.h
libstdc++-v3/testsuite/25_algorithms/stable_partition/moveable.cc
libstdc++-v3/testsuite/25_algorithms/stable_partition/pr52822.cc [new file with mode: 0644]

index 337a676b6410e9d4aa4ed16fe0ebdb298216ff88..d8fc4bfbcff93cf91ea739ca03abeb307d9d2819 100644 (file)
@@ -1,3 +1,15 @@
+2012-04-12   Jeffrey Yasskin  <jyasskin@google.com>
+
+       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  <terry.guo@arm.com>
 
        * testsuite/Makefile.am (TEST_GCC_EXEC_PREFIX): New.
index 8391d3e4854175d0c7e0775af4ad61bd831f438e..6e9965ca789a5580cd76ca6990420852dd580616 100644 (file)
@@ -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
index fe46463df4fbad591074b6477642e95f61c00f26..3fca316c7d9c99b1b3bb93bd9dbea70f4751270d 100644 (file)
@@ -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 (file)
index 0000000..c5f95f3
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// 25.2.12 [lib.alg.partitions] Partitions.
+
+#include <algorithm>
+#include <vector>
+#include <testsuite_hooks.h>
+
+bool true_vector_pred(const std::vector<int>&) { return true; }
+
+void
+test01()
+{
+  std::vector<std::vector<int> > 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;
+}