From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 70FF6385740E; Wed, 14 Sep 2022 18:22:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 70FF6385740E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663179744; bh=2Lbu9OeGdp4vfyIjEWcx+MXW/88Y97hgEbWSlp1VA4U=; h=From:To:Subject:Date:From; b=ydx7UtJLV14eEySYNqgEvu64gySZ4dsQvfxwtoJfZHbV7eW1JborbcRouqYJ0Mymn OjsInWFQJMjo6FIQGk6ZsMK9D2JZ6GTjXmAv+kUwH0jZrbYx0/71L3aq3bM2zu25oF BnD43y+IP+0thbnuIZnp/Ugk0dVWuM2gRBSvgId4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-2674] libstdc++: Add assertion to std::promise::set_exception (LWG 2276) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 8b1bc3051bd68ce193a8612fa3b1a65c0353b5b0 X-Git-Newrev: 0bc9aa9c3fcb1f3534575314038661ff111874dd Message-Id: <20220914182224.70FF6385740E@sourceware.org> Date: Wed, 14 Sep 2022 18:22:24 +0000 (GMT) List-Id: https://gcc.gnu.org/g:0bc9aa9c3fcb1f3534575314038661ff111874dd commit r13-2674-g0bc9aa9c3fcb1f3534575314038661ff111874dd Author: Jonathan Wakely Date: Wed Sep 14 14:03:19 2022 +0100 libstdc++: Add assertion to std::promise::set_exception (LWG 2276) Without this assertion, the shared state is made ready, but contains neither a value nor an exception. Add an assertion to prevent users from accessing a value that was never initialized in the shared state. libstdc++-v3/ChangeLog: * include/std/future (_State_baseV2::__setter(exception_ptr&, promise&)): Add assertion for LWG 2276 precondition. * testsuite/30_threads/promise/members/set_exception_neg.cc: New test. Diff: --- libstdc++-v3/include/std/future | 1 + .../30_threads/promise/members/set_exception_neg.cc | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/libstdc++-v3/include/std/future b/libstdc++-v3/include/std/future index ba1f28c6c75..a1b2d7f1d3a 100644 --- a/libstdc++-v3/include/std/future +++ b/libstdc++-v3/include/std/future @@ -559,6 +559,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _Setter<_Res, __exception_ptr_tag> __setter(exception_ptr& __ex, promise<_Res>* __prom) noexcept { + __glibcxx_assert(__ex != nullptr); // LWG 2276 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex }; } diff --git a/libstdc++-v3/testsuite/30_threads/promise/members/set_exception_neg.cc b/libstdc++-v3/testsuite/30_threads/promise/members/set_exception_neg.cc new file mode 100644 index 00000000000..16660931242 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/promise/members/set_exception_neg.cc @@ -0,0 +1,18 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { xfail *-*-* } } +// { dg-additional-options "-pthread" { target pthread } } +// { dg-require-effective-target c++11 } +// { dg-require-gthreads "" } + +// LWG 2276. Missing requirement on std::promise::set_exception + +#include + +int main() +{ + std::promise prom; + auto f = prom.get_future(); + std::exception_ptr p; + prom.set_exception(p); // Preconditions: p is not null + f.get(); +}