Skip to content

Safely working with libraries that use setjmp/longjmp #1656

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

Closed
daurnimator opened this issue Oct 15, 2018 · 4 comments
Closed

Safely working with libraries that use setjmp/longjmp #1656

daurnimator opened this issue Oct 15, 2018 · 4 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@daurnimator
Copy link
Contributor

daurnimator commented Oct 15, 2018

(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.
@andrewrk andrewrk added this to the 0.5.0 milestone Oct 15, 2018
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Oct 15, 2018
@shawnl
Copy link
Contributor

shawnl commented May 31, 2019

What about setjmp/longjmp in Zig code?

@andrewrk
Copy link
Member

The zig language currently has no plan for the spec to include the concept of a function that returns twice. This means that setjmp in Zig is unsound and cannot be used.

@daurnimator
Copy link
Contributor Author

The zig language currently has no plan for the spec to include the concept of a function that returns twice. This means that setjmp in Zig is unsound and cannot be used.

This issue is not so much about setjmp as longjmp, which is a very common error handling pattern in C libraries (see, Postgres and Lua as linked in original post).

@yrashk
Copy link

yrashk commented Dec 12, 2022

@daurnimator I wonder if you researched the subject of integrating with setjmp/longjmp-using libraries with Zig any further? My angle of interest is Postgres extensions. In Rust's pgx library they establish an FFI boundary around every call to Postgres to ensure that if they throw an error, it's caught by Rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants