diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2017-01-19 20:29:07 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2017-01-19 20:29:07 +0000 |
commit | 9ade9945a07c47abf2d1d5ad3a5de504e9f37b73 (patch) | |
tree | 96ac5c66055ac8069ff89eb49b7208b430f59042 | |
parent | ab014eb3ae304092718018b8ae6122636b4c0b10 (diff) | |
download | gcc-9ade9945a07c47abf2d1d5ad3a5de504e9f37b73.zip gcc-9ade9945a07c47abf2d1d5ad3a5de504e9f37b73.tar.gz gcc-9ade9945a07c47abf2d1d5ad3a5de504e9f37b73.tar.bz2 |
Fix unsafe moves inside loops
PR libstdc++/67085
* include/bits/stl_heap.h (__is_heap): Use _GLIBCXX_MOVE.
(__make_heap, __sort_heap): Don't use _GLIBCXX_MOVE inside loops.
* testsuite/23_containers/priority_queue/67085.cc: Adjust expected
number of copies.
* testsuite/25_algorithms/make_heap/movable.cc: New test.
From-SVN: r244650
-rw-r--r-- | libstdc++-v3/ChangeLog | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/stl_heap.h | 9 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/23_containers/priority_queue/67085.cc | 4 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/25_algorithms/make_heap/movable.cc | 38 |
4 files changed, 53 insertions, 5 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7fd289d..0106c23 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,6 +1,13 @@ 2017-01-19 Jonathan Wakely <jwakely@redhat.com> PR libstdc++/67085 + * include/bits/stl_heap.h (__is_heap): Use _GLIBCXX_MOVE. + (__make_heap, __sort_heap): Don't use _GLIBCXX_MOVE inside loops. + * testsuite/23_containers/priority_queue/67085.cc: Adjust expected + number of copies. + * testsuite/25_algorithms/make_heap/movable.cc: New test. + + PR libstdc++/67085 * include/bits/stl_heap.h (push_heap, __adjust_heap, __pop_heap) (pop_heap, __make_heap, make_heap, __sort_heap, sort_heap): Use _GLIBCXX_MOVE when passing comparison function to other functions. diff --git a/libstdc++-v3/include/bits/stl_heap.h b/libstdc++-v3/include/bits/stl_heap.h index c82ce77..b19c9f4 100644 --- a/libstdc++-v3/include/bits/stl_heap.h +++ b/libstdc++-v3/include/bits/stl_heap.h @@ -113,7 +113,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline bool __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) - { return std::__is_heap(__first, __comp, std::distance(__first, __last)); } + { + return std::__is_heap(__first, _GLIBCXX_MOVE(__comp), + std::distance(__first, __last)); + } // Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap, // + is_heap and is_heap_until in C++0x. @@ -336,7 +339,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent)); std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value), - _GLIBCXX_MOVE(__comp)); + __comp); if (__parent == 0) return; __parent--; @@ -401,7 +404,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION while (__last - __first > 1) { --__last; - std::__pop_heap(__first, __last, __last, _GLIBCXX_MOVE(__comp)); + std::__pop_heap(__first, __last, __last, __comp); } } diff --git a/libstdc++-v3/testsuite/23_containers/priority_queue/67085.cc b/libstdc++-v3/testsuite/23_containers/priority_queue/67085.cc index 5a3ca32..4ccea30 100644 --- a/libstdc++-v3/testsuite/23_containers/priority_queue/67085.cc +++ b/libstdc++-v3/testsuite/23_containers/priority_queue/67085.cc @@ -34,9 +34,9 @@ test01() { int v[] = {1, 2, 3, 4}; std::priority_queue<int, std::vector<int>, CopyCounter> q{v, v+4}; - VERIFY(count == 2); + VERIFY(count == 4); q.push(1); - VERIFY(count == 3); + VERIFY(count == 5); } int diff --git a/libstdc++-v3/testsuite/25_algorithms/make_heap/movable.cc b/libstdc++-v3/testsuite/25_algorithms/make_heap/movable.cc new file mode 100644 index 0000000..f6738f1 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/make_heap/movable.cc @@ -0,0 +1,38 @@ +// Copyright (C) 2017 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-do run { target c++11 } } + +#include <functional> +#include <algorithm> +#include <iterator> + +void +test01() +{ + int i[] = { 1, 2, 3, 4 }; + std::function<bool(int, int)> f = std::less<>{}; + // If this uses a moved-from std::function we'll get an exception: + std::make_heap(std::begin(i), std::end(i), f); + std::sort_heap(std::begin(i), std::end(i), f); +} + +int +main() +{ + test01(); +} |