Skip to content

Safely working with libraries that use setjmp/longjmp #1656

Closed
@daurnimator

Description

(as discussed on IRC):

Some C environments/libraries use setjmp+longjmp for control flow. e.g. writing PostgreSQL Extensions or the Lua C API.

A C function longjmp-ing out may result in zig code not being run. This may include defer-d code for cleanup, which will result in various resource leaks or even deadlocks.

Proposed Solution

Functions should gain a may longjmp annotation (possibly with an arbitrary ID?).

  • Such a function can only be called from other functions annotated with may longjmp unless a matching setjmp annotation is found.
  • A compile error (possibly warning?) is thrown if defer occurs before a may longjmp function call in the same (or child) scope.
    It is fine if the defer block occurs after the call to the may longjmp function, as the longjmp() would already have happened.

Activity

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

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions