diff options
author | redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-12-06 20:38:07 +0000 |
---|---|---|
committer | redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-12-06 20:38:07 +0000 |
commit | 5ab13c44a3bb365f2f858cb5bf4722ec878159d5 (patch) | |
tree | 4ede925db8506c30799a942f3fb0e4d0f9aa0023 /libstdc++-v3 | |
parent | 1b53453055c8f84cd56194c1d8dcd7bd1eeebd4b (diff) | |
download | gcc-5ab13c44a3bb365f2f858cb5bf4722ec878159d5.tar.gz |
PR libstdc++/59603
* include/bits/stl_algo.h (random_shuffle): Prevent self-swapping.
* testsuite/25_algorithms/random_shuffle/59603.cc: New.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-4_8-branch@218454 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/ChangeLog | 4 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/stl_algo.h | 13 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/25_algorithms/random_shuffle/59603.cc | 34 |
3 files changed, 49 insertions, 2 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index de13b7cebd7..ac06a3a0a27 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -15,6 +15,10 @@ * testsuite/20_util/tuple/61947.cc: New. * testsuite/20_util/uses_allocator/cons_neg.cc: Adjust dg-error line. + PR libstdc++/59603 + * include/bits/stl_algo.h (random_shuffle): Prevent self-swapping. + * testsuite/25_algorithms/random_shuffle/59603.cc: New. + 2014-11-27 Thomas Preud'homme <thomas.preudhomme@arm.com> Backport from mainline diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 7fa7133aeee..3a33bf40382 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -5193,7 +5193,12 @@ _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))); + { + _RandomAccessIterator __j = __first + + std::rand() % ((__i - __first) + 1); + if (__i != __j) + std::iter_swap(__i, __j); + } } /** @@ -5227,7 +5232,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 index 00000000000..7b179ac19a0 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/random_shuffle/59603.cc @@ -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()); +} |