Skip to content

Commit

Permalink
fix(coro): fix read-after-free in dpp::async
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Jul 6, 2024
1 parent 69dd902 commit fb59bba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions include/dpp/coro/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ class async : public awaitable<R> {
explicit async(Fun &&fun, Args&&... args) : async{std::make_shared<basic_promise<R>>()} {
std::invoke(std::forward<Fun>(fun), std::forward<Args>(args)..., api_callback);
}

async(const async&) = delete;
async(async&&) = default;

async& operator=(const async&) = delete;
async& operator=(async&&) = default;

~async() {
this->abandon();
}
};

DPP_CHECK_ABI_COMPAT(async<>, async_dummy);
Expand Down
7 changes: 5 additions & 2 deletions include/dpp/coro/awaitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,11 @@ using promise = moveable_promise<T>;

template <typename T>
auto awaitable<T>::abandon() -> uint8_t {
auto previous_state = state_ptr->state.fetch_or(state_flags::sf_broken, std::memory_order::acq_rel);
state_ptr = nullptr;
uint8_t previous_state = state_flags::sf_broken;
if (state_ptr) {
previous_state = state_ptr->state.fetch_or(state_flags::sf_broken, std::memory_order::acq_rel);
state_ptr = nullptr;
}
return previous_state;
}

Expand Down

0 comments on commit fb59bba

Please sign in to comment.