You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One of the subtle aspects of *ptr = val where ptr is a raw pointer is that this will drop the old contents stored behind that pointer. On the one hand that might not be surprising because this is the same for mutable references, but on the other hand raw pointers are often used to point to invalid/uninitialized data, and then this is an easy way to cause a double-drop or to drop some garbage data. I have made this mistake myself and seen it a few times "out there".
So I think it'd be a good idea to lint against *ptr = val when ptr: *mut T and T has drop glue. The lint should recommend to use ptr.write(val) instead, which does not drop. Code that wants to drop should explicitly do ptr.drop_in_place().
The text was updated successfully, but these errors were encountered:
and contain a note that say something along these lines: "If the memory pointed to by ptr is not valid, the behavior of trying to drop that memory using ptr.drop_in_place(); is undefined. This can happen if, for example, *ptr = val is used to initialize uninitialized memory".
I agree. I would keep the note on the lint message short though ("ptr.drop_in_place() could be undefined behavior") and put the long explanation in the documentation.
One of the subtle aspects of
*ptr = val
whereptr
is a raw pointer is that this will drop the old contents stored behind that pointer. On the one hand that might not be surprising because this is the same for mutable references, but on the other hand raw pointers are often used to point to invalid/uninitialized data, and then this is an easy way to cause a double-drop or to drop some garbage data. I have made this mistake myself and seen it a few times "out there".So I think it'd be a good idea to lint against
*ptr = val
whenptr: *mut T
andT
has drop glue. The lint should recommend to useptr.write(val)
instead, which does not drop. Code that wants to drop should explicitly doptr.drop_in_place()
.The text was updated successfully, but these errors were encountered: