]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR libstdc++/59603 (std::random_shuffle tries to swap element with itself)
authorJonathan Wakely <jwakely@redhat.com>
Fri, 12 Sep 2014 13:30:35 +0000 (14:30 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 12 Sep 2014 13:30:35 +0000 (14:30 +0100)
PR libstdc++/59603
* include/bits/stl_algo.h (random_shuffle): Prevent self-swapping.
* testsuite/25_algorithms/random_shuffle/59603.cc: New.

From-SVN: r215219

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

index 29fdb5662f1bb8bf7fb6e34d9a0bad8966abbf91..afcba385f21ba14cf2f4e493718e1e614ad87ea0 100644 (file)
@@ -1,3 +1,9 @@
+2014-09-12  Jonathan Wakely  <jwakely@redhat.com>
+
+       PR libstdc++/59603
+       * include/bits/stl_algo.h (random_shuffle): Prevent self-swapping.
+       * testsuite/25_algorithms/random_shuffle/59603.cc: New.
+
 2014-09-11  Jonathan Wakely  <jwakely@redhat.com>
 
        PR libstdc++/63219
index 4c6ca8a72682c4e55b7dc3d10a23834ae10cf7b1..f2dfc208ce5a9b531366d8b73a6315a41692c422 100644 (file)
@@ -4430,7 +4430,13 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
 
       if (__first != __last)
        for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-         std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
+         {
+           // XXX rand() % N is not uniformly distributed
+           _RandomAccessIterator __j = __first
+                                       + std::rand() % ((__i - __first) + 1);
+           if (__i != __j)
+             std::iter_swap(__i, __j);
+         }
     }
 
   /**
@@ -4464,7 +4470,11 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
       if (__first == __last)
        return;
       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-       std::iter_swap(__i, __first + __rand((__i - __first) + 1));
+       {
+         _RandomAccessIterator __j = __first + __rand((__i - __first) + 1);
+         if (__i != __j)
+           std::iter_swap(__i, __j);
+       }
     }
 
 
diff --git a/libstdc++-v3/testsuite/25_algorithms/random_shuffle/59603.cc b/libstdc++-v3/testsuite/25_algorithms/random_shuffle/59603.cc
new file mode 100644 (file)
index 0000000..7b179ac
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright (C) 2014 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-require-debug-mode "" }
+
+// libstdc++/59603
+
+#include <algorithm>
+#include <vector>
+
+struct C {
+    std::vector<int> v;
+    C (int a) : v{a} {};
+};
+
+int main () {
+    std::vector<C> cs { {1}, {2}, {3}, {4} };
+    std::random_shuffle(cs.begin(), cs.end());
+}