Stop dereferencing msg_send!
's first argument
#102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The issue
In Objective-C, having a null pointer receiver is valid, but with
msg_send!
it would be converted into a reference, which is undefined behavior - yes, even though the reference is converted into a pointer immediately after, it is still undefined behaviour!Additionally, there might be a mutable reference somewhere else in the program, which would now be aliased.
Minimal example
How to fix this
This PR proposes changing
msg_send!
to no longer automatically dereference the receiver. This is a breaking change, but neither e.g.cocoa
norwinit
is affected by it - they don't use smart pointers likeobjc_id::Id
.objc-foundation
will require a few tweaks (it usesobjc_id::Id
), and so will probably also some other crates, but I would say this is acceptable, the fix is very simple, see SSheldon/rust-objc-foundation#14 (and it causes a compile error, not just silent UB if you don't fix it).Also, I think with unsafe things like
msg_send!
it's always better to be explicit.