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

Public API of pgx exposes UB to safe code #196

Closed
dtolnay opened this issue Sep 19, 2021 · 2 comments · Fixed by #210
Closed

Public API of pgx exposes UB to safe code #196

dtolnay opened this issue Sep 19, 2021 · 2 comments · Fixed by #210
Assignees

Comments

@dtolnay
Copy link

dtolnay commented Sep 19, 2021

For example, the following safe program leads to pgx performing Undefined Behavior:

// [dependencies]
// pgx = { version = "0.1.22", features = ["pg13"] }

fn main() {
    pgx::itemptr::item_pointer_is_valid(0xdeadbeefusize as _);
}
Segmentation fault (core dumped)

In general it is unsound for a safe API to result in UB.

@eeeebbbbrrrr
Copy link
Contributor

There's probably a number of issues like this across pgx. We should definitely declare this functions (and its callers) as unsafe. That particular function is meant to mimic the Postgres ItemPointerIsValid macro:

/*
 * ItemPointerIsValid
 *              True iff the disk item pointer is not NULL.
 */
#define ItemPointerIsValid(pointer) \
        ((bool) (PointerIsValid(pointer) && ((pointer)->ip_posid != 0)))

Giving it a bad pointer like 0xdeadbeef isn't a thing we can trap (afaik?), so all I can see is to mark it unsafe.

@Hoverbear Hoverbear self-assigned this Sep 23, 2021
@Hoverbear Hoverbear modified the milestone: 0.2.0 Sep 23, 2021
@Hoverbear Hoverbear linked a pull request Sep 23, 2021 that will close this issue
@Hoverbear
Copy link
Contributor

Thanks so much for reporting this @dtolnay, I've fixed this specific case and will keep my eyes out for more. :)

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