libstdc++: Add assertion to std::promise::set_exception (LWG 2276)
authorJonathan Wakely <jwakely@redhat.com>
Wed, 14 Sep 2022 13:03:19 +0000 (14:03 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 14 Sep 2022 18:17:36 +0000 (19:17 +0100)
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.

libstdc++-v3/include/std/future
libstdc++-v3/testsuite/30_threads/promise/members/set_exception_neg.cc [new file with mode: 0644]

index ba1f28c..a1b2d7f 100644 (file)
@@ -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 (file)
index 0000000..1666093
--- /dev/null
@@ -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 <future>
+
+int main()
+{
+  std::promise<void> prom;
+  auto f = prom.get_future();
+  std::exception_ptr p;
+  prom.set_exception(p); // Preconditions: p is not null
+  f.get();
+}