-
Notifications
You must be signed in to change notification settings - Fork 758
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
Avoid calling slice::from_raw_parts with a null pointer #2687
Conversation
Opened #2688 |
The PR template says to consider adding something to |
Maintainers can add |
Sounds good. I can always drop the commit if a maintainer agrees with me. |
The |
I think UB / crashes are generally changelog-worthy. Additional CI for soundness checks is always welcome. @adamreichold configured some for rust-numpy which might be a starting point. However, I want to discuss, is this change really necessary? The /// - `args` must be a pointer to a C-style array of valid `ffi::PyObject` pointers. I wrote this implying that
|
Well, our test suite hits it, apparently. https://asan.saethlin.dev/ub?crate=pyo3&version=0.17.2 |
I think that if the test suite hits it, then we do need a fix, but it might be that it should be higher up in the call chain. |
Where? I'm a novice in this codebase, so I just don't see anywhere in the caller that makes sense to place this. |
To be clear, I simply didn't read this safety comment. I agree that the implementation is fine as-is and the caller should be adjusted. |
So after trawling through Python docs for a while, I finally found this line here:
Which makes it clear to me that there is a design mismatch between what I'd implemented for So in my opinion:
|
Done. |
Thanks. I'm... not sure what's up with CI, will have to try to investigate that when I can find a moment. |
No worries. I figured you'd get to it when you have time 😄 |
Looks like mmastrac/rust-ctor#241 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha good find - that's since been fixed, and CI now passes, so let's merge!
slice::from_raw_parts
requires that the pointer not be null, even if the length is zero. This is because the pointer is converted into a reference, and references must not be null. This is detected by the standard library debug assertions, which are compiled out in all released toolchains but you can usecargo careful
if you want to enable them and some other nightly-only opt-in checks.This is probably the third instance of this bug I've fixed, I think it's pretty common for C libraries to return a pointer and length, and it is very tempting to just convert that to a Rust slice. But unfortunately you need a null check.
I'm not really sure what to do about testing this. I feel like there should be CI to guard against making this mistake again, but I'm not sure how to jam
cargo careful
orcargo +nightly test -Zbuild-std
into your xtask setup.