Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AsyncWrapper to make it safer #1206

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Anohkka
Copy link
Contributor

@Anohkka Anohkka commented Mar 30, 2025

It's currently possible to construct an unsafe cancelable AsyncWrapper by explicitly specifying the template arguments in the constructor, like so:

#include "cpr/cpr.h"

int main(int, char**) {
    auto wrapper = cpr::AsyncWrapper<void, true>{std::future<void>{}};
    
    // Dereferences a null pointer
    auto _ = wrapper.IsCancelled();
}

To prevent that, we split AsyncWrapper it into two template specializations. AsyncWrapper<T, false> keeps most of the code, while cancelation-related code is made exclusive to AsyncWrapper<T, true>. Most importantly, we can prevent a cancelable AsyncWrapper from being constructed without a provided pointer. AsyncWrappers that aren't cancelable get to be a bit slimmer, no longer containing the pointer to keep track of cancelation state.

Anohkka added 4 commits March 30, 2025 16:02
AsyncWrapper<T, false> no longer has an isCancelled() member function
It creates an unsafe object, and tests pass without it
@Anohkka Anohkka changed the title Refactor AsyncWrapper to make it a bit safer Refactor AsyncWrapper to make it safer Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant