Skip to content

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

Description

@lichray

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 ..."

Activity

  1. jensmaurer commented on Oct 20, 2017

    @jensmaurer
    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.

  2. gnaggnoyil commented on Oct 20, 2017

    @gnaggnoyil

    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.

  3. jwakely commented on Oct 21, 2017

    @jwakely
    Member

    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.

  4. lichray commented on Oct 21, 2017

    @lichray
    ContributorAuthor

    @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?

  5. zygoloid commented on Oct 21, 2017

    @zygoloid
    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.

  6. lichray commented on Oct 21, 2017

    @lichray
    ContributorAuthor

    Filed a core issue.

  7. added
    not-editorialIssue is not deemed editorial; the editorial issue is kept open for tracking.
    on Mar 24, 2018
  8. jensmaurer commented on Apr 10, 2018

    @jensmaurer
    Member

    This is CWG2361.

  9. changed the title [-][csetjmp.syn] Imprecise description of UB[/-] [+][csetjmp.syn] CWG 2361: Imprecise description of UB[/+] on Apr 11, 2018
  10. changed the title [-][csetjmp.syn] CWG 2361: Imprecise description of UB[/-] [+][csetjmp.syn] CWG2361: Imprecise description of UB[/+] on Dec 23, 2021
  11. changed the title [-][csetjmp.syn] CWG2361: Imprecise description of UB[/-] [+][csetjmp.syn] CWG2361, LWG3652: Imprecise description of UB[/+] on Dec 23, 2021
  12. jensmaurer commented on Dec 23, 2021

    @jensmaurer
    Member

    See also LWG3652.

  13. gnaggnoyil commented on Dec 24, 2021

    @gnaggnoyil

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

  14. jensmaurer commented on Dec 24, 2021

    @jensmaurer
    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.

  15. frederick-vs-ja commented on Nov 28, 2024

    @frederick-vs-ja
    Contributor

    It seems that such UB should be removed in the spirit of CWG2523. But there's still an issue on whether destructor is called on each of these objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cwgIssue must be reviewed by CWG.not-editorialIssue is not deemed editorial; the editorial issue is kept open for tracking.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions