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

[csetjmp.syn] CWG2361, LWG3652: Imprecise description of UB #1778

Open
lichray opened this issue Oct 20, 2017 · 10 comments
Open

[csetjmp.syn] CWG2361, LWG3652: Imprecise description of UB #1778

lichray opened this issue Oct 20, 2017 · 10 comments
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.

Comments

@lichray
Copy link
Contributor

lichray commented Oct 20, 2017

A setjmp/longjmp call pair has undefined behavior if replacing the setjmp and longjmp by catch
and throw would invoke any non-trivial destructors for any automatic objects.

It's unclear what does the document mean by "replacing". Could be something like "if ... skips stack unwinding ..."

@jensmaurer
Copy link
Member

Since "stack unwinding" is only defined in the context of exceptions [except.ctor], I don't understand what exactly the improvement is with your proposal.

I do agree that the wording is sub-optimal, but I'm not sure we can find something better that is a small enough fix to be considered editorial.

@gnaggnoyil
Copy link

gnaggnoyil commented Oct 20, 2017

The original wording is unclear beacuse it does not exactly explain which try block after replacement would be catched. And as that try block varies it might differ whether non-trival destructors of an automatic storage objects would be invoked or not. So what about explaining the try block? Not sure if it is an editoral fix though.

@jwakely
Copy link
Member

jwakely commented Oct 21, 2017

The meaning seems fairly obvious to me.

If non-trivial destructors would be invoked by transferring control from point A to point B by throwing an exception at A and catching it at B, then it's undefined to transfer control from A to B by calling setjmp at B and longjmp at A.

@lichray
Copy link
Contributor Author

lichray commented Oct 21, 2017

@jwakely Then why don't we put your explanation

If non-trivial destructors would be invoked by transferring control from point A to point B by throwing an exception at A and catching it at B, then it's undefined to transfer control from A to B by calling setjmp at B and longjmp at A.

in place of

A setjmp/longjmp call pair has undefined behavior if replacing the setjmp and longjmp by catch
and throw would invoke any non-trivial destructors for any automatic objects.

in the standard?

@zygoloid
Copy link
Member

I still find that to have a fundamental problem: we can't throw an exception at the longjmp and catch it at the setjmp, because execution in the function containing the setjmp has by definition already got past the setjmp. Whatever rule we use needs to take into account a third point C, which is the point within the function evaluation containing the setjmp call at which longjmp is directly or indirectly invoked. And we want to consider if any local variables would be destroyed by unwinding from A to C, and also something about the relationship between B and C (something like, you can goto from C to B without destroying any local variables).

So I agree the wording is wrong. I do not agree that it can be repaired editorially. While technically library wording, this is a core language mechanism, so we probably want CWG to look at this and propose a wording change.

@zygoloid zygoloid added the cwg Issue must be reviewed by CWG. label Oct 21, 2017
@lichray
Copy link
Contributor Author

lichray commented Oct 21, 2017

Filed a core issue.

@jensmaurer jensmaurer added the not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking. label Mar 24, 2018
@jensmaurer
Copy link
Member

jensmaurer commented Apr 10, 2018

This is CWG2361.

@jensmaurer jensmaurer changed the title [csetjmp.syn] Imprecise description of UB [csetjmp.syn] CWG 2361: Imprecise description of UB Apr 11, 2018
@jensmaurer jensmaurer changed the title [csetjmp.syn] CWG 2361: Imprecise description of UB [csetjmp.syn] CWG2361: Imprecise description of UB Dec 23, 2021
@jensmaurer jensmaurer changed the title [csetjmp.syn] CWG2361: Imprecise description of UB [csetjmp.syn] CWG2361, LWG3652: Imprecise description of UB Dec 23, 2021
@jensmaurer
Copy link
Member

See also LWG3652.

@gnaggnoyil
Copy link

My understanding is that LWG3652 still doesn't explain how we can "throw an exception at the longjmp and catch it at the setjmp"

@jensmaurer
Copy link
Member

My understanding is that LWG3652 still doesn't explain how we can "throw an exception at the longjmp and catch it at the setjmp"

Yes, that's what CWG2361 is about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.
Projects
None yet
Development

No branches or pull requests

5 participants