diff options
author | Jonathan Wakely <jwakely.gcc@gmail.com> | 2010-12-14 23:27:17 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2010-12-14 23:27:17 +0000 |
commit | 5262c72ad4f6c5895f1318935dfa8980050ce784 (patch) | |
tree | acbd413506b308a516f39c0b59d371c06d8bceb1 /libstdc++-v3 | |
parent | 5ba58d4f041bce790343516de7d4a17378f19856 (diff) | |
download | gcc-5262c72ad4f6c5895f1318935dfa8980050ce784.tar.gz |
re PR libstdc++/45133 ([c++0x] std::future will crash with NULL deref if get() is called twice)
2010-12-14 Jonathan Wakely <jwakely.gcc@gmail.com>
PR libstdc++/45133
* include/std/future (__basic_future::wait): Throw if not valid.
(__basic_future::wait_for): Likewise.
(__basic_future::wait_until): Likewise.
(__basic_future::_M_get_result): Likewise.
* testsuite/30_threads/future/members/45133.cc: New.
* testsuite/30_threads/shared_future/members/45133.cc: New.
From-SVN: r167823
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/ChangeLog | 10 | ||||
-rw-r--r-- | libstdc++-v3/include/std/future | 17 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/30_threads/future/members/45133.cc | 100 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/30_threads/shared_future/members/45133.cc | 90 |
4 files changed, 214 insertions, 3 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 548a4ee88e3..640cf6f2ce7 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,15 @@ 2010-12-14 Jonathan Wakely <jwakely.gcc@gmail.com> + PR libstdc++/45133 + * include/std/future (__basic_future::wait): Throw if not valid. + (__basic_future::wait_for): Likewise. + (__basic_future::wait_until): Likewise. + (__basic_future::_M_get_result): Likewise. + * testsuite/30_threads/future/members/45133.cc: New. + * testsuite/30_threads/shared_future/members/45133.cc: New. + +2010-12-14 Jonathan Wakely <jwakely.gcc@gmail.com> + PR libstdc++/46910 * include/bits/shared_ptr_base.h (_Sp_counted_deleter): Do not derive from _Sp_counted_ptr. diff --git a/libstdc++-v3/include/std/future b/libstdc++-v3/include/std/future index 45691168f3f..6fc3cf0a804 100644 --- a/libstdc++-v3/include/std/future +++ b/libstdc++-v3/include/std/future @@ -508,23 +508,34 @@ _GLIBCXX_BEGIN_NAMESPACE(std) valid() const { return static_cast<bool>(_M_state); } void - wait() const { _M_state->wait(); } + wait() const + { + _State::_S_check(_M_state); + _M_state->wait(); + } template<typename _Rep, typename _Period> bool wait_for(const chrono::duration<_Rep, _Period>& __rel) const - { return _M_state->wait_for(__rel); } + { + _State::_S_check(_M_state); + return _M_state->wait_for(__rel); + } template<typename _Clock, typename _Duration> bool wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const - { return _M_state->wait_until(__abs); } + { + _State::_S_check(_M_state); + return _M_state->wait_until(__abs); + } protected: /// Wait for the state to be ready and rethrow any stored exception __result_type _M_get_result() { + _State::_S_check(_M_state); _Result_base& __res = _M_state->wait(); if (!(__res._M_error == 0)) rethrow_exception(__res._M_error); diff --git a/libstdc++-v3/testsuite/30_threads/future/members/45133.cc b/libstdc++-v3/testsuite/30_threads/future/members/45133.cc new file mode 100644 index 00000000000..a4e4551d895 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/future/members/45133.cc @@ -0,0 +1,100 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } +// { dg-require-atomic-builtins "" } + +// Copyright (C) 2010 Free Software Foundation +// +// 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/>. + +// 30.6.6 Class template future [futures.unique_future] + +#include <future> +#include <testsuite_hooks.h> + +// This test verifies behaviour which is encouraged by a non-normative note, +// but not required. + +void +test01() +{ + bool test __attribute__((unused)) = true; + + std::promise<int> p; + std::future<int> f = p.get_future(); + p.set_value(0); + f.get(); + try + { + f.get(); + VERIFY( false ); + } + catch (std::future_error& e) + { + VERIFY( e.code() == std::future_errc::no_state ); + } +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::promise<int&> p; + std::future<int&> f = p.get_future(); + int i = 0; + p.set_value(i); + f.get(); + try + { + f.get(); + VERIFY( false ); + } + catch (std::future_error& e) + { + VERIFY( e.code() == std::future_errc::no_state ); + } +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::promise<void> p; + std::future<void> f = p.get_future(); + p.set_value(); + f.get(); + try + { + f.get(); + VERIFY( false ); + } + catch (std::future_error& e) + { + VERIFY( e.code() == std::future_errc::no_state ); + } +} + +int main() +{ + test01(); + test02(); + test03(); + + return 0; +} + diff --git a/libstdc++-v3/testsuite/30_threads/shared_future/members/45133.cc b/libstdc++-v3/testsuite/30_threads/shared_future/members/45133.cc new file mode 100644 index 00000000000..d71097fb1d6 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/shared_future/members/45133.cc @@ -0,0 +1,90 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } +// { dg-require-atomic-builtins "" } + +// Copyright (C) 2010 Free Software Foundation +// +// 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/>. + +// 30.6.7 Class template shared_future [futures.shared_future] + +#include <future> +#include <testsuite_hooks.h> + +// This test verifies behaviour which is encouraged by a non-normative note, +// but not required. + +void +test01() +{ + bool test __attribute__((unused)) = true; + + std::shared_future<int> f; + try + { + f.get(); + VERIFY( false ); + } + catch (std::future_error& e) + { + VERIFY( e.code() == std::future_errc::no_state ); + } +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + + std::shared_future<int&> f; + try + { + f.get(); + VERIFY( false ); + } + catch (std::future_error& e) + { + VERIFY( e.code() == std::future_errc::no_state ); + } +} + +void +test03() +{ + bool test __attribute__((unused)) = true; + + std::shared_future<void> f; + try + { + f.get(); + VERIFY( false ); + } + catch (std::future_error& e) + { + VERIFY( e.code() == std::future_errc::no_state ); + } +} + +int main() +{ + test01(); + test02(); + test03(); + + return 0; +} + |