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

Undefined Behavior to access non active union member #59

Closed
1 of 2 tasks
ChuanqiXu9 opened this issue Mar 10, 2022 · 8 comments
Closed
1 of 2 tasks

Undefined Behavior to access non active union member #59

ChuanqiXu9 opened this issue Mar 10, 2022 · 8 comments

Comments

@ChuanqiXu9
Copy link
Collaborator

Search before asking

  • I searched the issues and found no similar issues.

What happened + What you expected to happen

Extract from: https://www.reddit.com/r/cpp/comments/ta4h29/async_simple_a_c20_coroutine_library/:

ContextUnion u;
u.ctx = ctx;

auto prompt =
    _pool.getCurrentId() == (u.id & 0xBFFFFFFF) && opts.prompt;

It is undefined behavior, access to non active union member

Reproduction way

Anything else

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!
@qicosmos
Copy link
Collaborator

I think we need to check if there are more union UB code.

@qicosmos
Copy link
Collaborator

Shell we use std::variant instead of union? however maybe visiting union is more efficient.

@ChuanqiXu9
Copy link
Collaborator Author

I think we need to check if there are more union UB code.

Of course, it is always good to eliminate UB.

Shell we use std::variant instead of union? however maybe visiting union is more efficient.

I think the readability and maintainability is more important given it wouldn't hurt performance much. @RainMark how do you think about?

@RainMark
Copy link
Collaborator

the SimpleExecutor's ContextUnion looks like can be removed, static_cast void* to int64_t directly?

And the other tagged union keep it firstly?

@qicosmos
Copy link
Collaborator

the SimpleExecutor's ContextUnion looks like can be removed, static_cast void* to int64_t directly?

And the other tagged union keep it firstly?

I think it's acceptable, but we should make sure no UB anymore.

@ChuanqiXu9
Copy link
Collaborator Author

but we should make sure no UB anymore.

This is the right direction. But we couldn't make sure no UB actually : ) Some static analysis tool might help.

@qicosmos
Copy link
Collaborator

I agree, union is not safe, we'd better use std::variant as safe union.

@ChuanqiXu9
Copy link
Collaborator Author

Yeah, I agree std::variant is better. But given async_simple is based on practicing. Let's be conservative and refactor it step by step in stead of in one shot.

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 a pull request may close this issue.

3 participants