-
Notifications
You must be signed in to change notification settings - Fork 893
soundness for capsule reference and name #5474
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
Conversation
src/types/capsule.rs
Outdated
| /// This method returns `*const c_char` instead of `&CStr` because it's possible for | ||
| /// arbitrary Python code to change the capsule name. Callers can use `NonNull::from_ptr()` | ||
| /// to get a `&CStr` if they want to, however they should beware the fact that the pointer | ||
| /// may become invalid after arbitrary Python code has run. | ||
| fn name(&self) -> PyResult<*const c_char>; |
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.
I changed this because the &CStr had the same problematic lifetime as .reference(). Also CStr::from_ptr is linear complexity so this is potentially more efficient depending on what the user is doing with it.
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.
Maybe we can use Option<NonNull<c_char>> here, to enforce the check for the null pointer?
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.
I played around with this just now but the NonNull doesn't carry the const-ness of the value. It's also a fairly nested type, overall it just didn't feel that good.
One option is we could have a type CapsuleName which has an unsafe .as_cstr() method. (unsafe because it's legal for callers to change the capsule name later.)
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.
(So -> PyResult<Option<CapsuleName>>)
|
I wonder if the new names should be introduced as |
|
Thanks for taking over and extending the PR :) As a user of PyO3 I'd prefer the I think it would be best to eventually rename By the way, thanks for all the work that you put into this excellent crate! |
|
I took another pass at this file. I added Possibly in the future it might be correct to use |
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.
Looks great, just a comment to make sure I understood the design properly
| r != 0 | ||
| } | ||
|
|
||
| fn name(&self) -> PyResult<Option<CapsuleName>> { |
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.
Just to make sure: I guess the use case of the CapsuleName type is because it's lifetime is unknown and so it's nicer to make .as_cstr() the unsafe dereference-like function that assume the pointed value is still allocated, just like pointers?
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.
Yep precisely. I wasn't convinced that NonNull<c_char> was easy enough to use correctly in isolation, and NonNull<CStr> was better but currently costs a length check to create (because it's a fat pointer). So this abstraction will exist for now, maybe if CStr changes to be thin in the future we can simplify.
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.
Thanks! Makes perfect sense!
Co-authored-by: Thomas Tanon <thomas@pellissier-tanon.fr>
Extension of #5229
I could not push to that PR.
I'm too tired to continue tonight, will most likely revisit this tomorrow evening.