Closed gretchenfrage closed 5 years ago
Oh, thanks for the report! I'm thinking we should be conservative and make two changes:
1) make it an unsafe trait 2) assert the trait object pointer and the reference share the same address
The macro is probably not necessary, since the trait is meant to be implemented using a blanket impl.
I don't remember the details of Unsized
, but if it's stable and works it should probably replace CastFrom
entirely.
I was reading through this code out of interest, and I saw that the metatable can be used to trigger undefined behavior without using any unsafe code. Basically, if an implementation of
CastFrom
borrows out an object reference to sub-data within the itself, instead of its entire self, it will incorrectly cast raw pointers.This program trigger a seg fault by dereferencing null:
I'm not sure if you're aware of this, but it seems noteworthy to me. One solution I could see would be to make
CastFrom
an unsafe trait, and create a macro for correctly implementing it on a type. Another approach that might work would be to use theUnsize<T>
API, but i'm not sure how stable that is. Another approach would be to modify the metatable to actually invoke the implementation ofCastFrom
, which seems like the less disruptive solution, but it would add a slight additional runtime cost.