[libcxx-commits] [libcxx] 46fdaac - [libc++] Fix heap UaF issue in coroutine test

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 28 13:10:31 PDT 2020


Author: Louis Dionne
Date: 2020-09-28T16:09:15-04:00
New Revision: 46fdaac098a3a14cfbca3fe2d922ae62a100794d

URL: https://github.com/llvm/llvm-project/commit/46fdaac098a3a14cfbca3fe2d922ae62a100794d
DIFF: https://github.com/llvm/llvm-project/commit/46fdaac098a3a14cfbca3fe2d922ae62a100794d.diff

LOG: [libc++] Fix heap UaF issue in coroutine test

This wasn't being flagged by older versions of ASAN, but it is now.

Added: 
    

Modified: 
    libcxx/test/std/experimental/language.support/support.coroutines/end.to.end/expected.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/experimental/language.support/support.coroutines/end.to.end/expected.pass.cpp b/libcxx/test/std/experimental/language.support/support.coroutines/end.to.end/expected.pass.cpp
index a0c31b6a6eda..2bd297660c4e 100644
--- a/libcxx/test/std/experimental/language.support/support.coroutines/end.to.end/expected.pass.cpp
+++ b/libcxx/test/std/experimental/language.support/support.coroutines/end.to.end/expected.pass.cpp
@@ -11,53 +11,50 @@
 
 #include <experimental/coroutine>
 #include <cassert>
+#include <memory>
 
 #include "test_macros.h"
 using namespace std::experimental;
 
-struct error {};
+struct error_tag { };
 
 template <typename T, typename Error = int>
 struct expected {
 
   struct Data {
+    Data() : val(), error() { }
+    Data(T v, Error e) : val(v), error(e) { }
     T val;
     Error error;
   };
-  Data data;
+  std::shared_ptr<Data> data;
 
-  struct DataPtr {
-    Data *p;
-    ~DataPtr() { delete p; }
-  };
-
-  expected() {}
-  expected(T val) : data{std::move(val),{}} {}
-  expected(struct error, Error error) : data{{}, std::move(error)} {}
-  expected(DataPtr & p) : data{std::move(p.p->val), std::move(p.p->error)} {}
+  expected(T val) : data(std::make_shared<Data>(val, Error())) {}
+  expected(error_tag, Error error) : data(std::make_shared<Data>(T(), error)) {}
+  expected(std::shared_ptr<Data> p) : data(p) {}
 
   struct promise_type {
-    Data* data;
-    DataPtr get_return_object() { data = new Data{}; return {data}; }
+    std::shared_ptr<Data> data;
+    std::shared_ptr<Data> get_return_object() { data = std::make_shared<Data>(); return data; }
     suspend_never initial_suspend() { return {}; }
     suspend_never final_suspend() noexcept { return {}; }
-    void return_value(T v) { data->val = std::move(v); data->error = {};}
+    void return_value(T v) { data->val = v; data->error = {}; }
     void unhandled_exception() {}
   };
 
-  bool await_ready() { return !data.error; }
-  T await_resume() { return std::move(data.val); }
+  bool await_ready() { return !data->error; }
+  T await_resume() { return data->val; }
   void await_suspend(coroutine_handle<promise_type> h) {
-    h.promise().data->error =std::move(data.error);
+    h.promise().data->error = data->error;
     h.destroy();
   }
 
-  T const& value() { return data.val; }
-  Error const& error() { return data.error; }
+  T const& value() { return data->val; }
+  Error const& error() { return data->error; }
 };
 
 expected<int> g() { return {0}; }
-expected<int> h() { return {error{}, 42}; }
+expected<int> h() { return {error_tag{}, 42}; }
 
 extern "C" void print(int);
 


        


More information about the libcxx-commits mailing list