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
Similarly to the temporary_cstring_as_ptr lint, a common mistake I've run into when dealing with FFI a bunch is immediately trying to get a raw pointer from an initialized vec:
let ptr = vec![0;4].as_mut_ptr();
This is a really easy way to get a dangling pointer and can be easily overlooked. It would be great if we had a clippy lint for it and as_ptr as well.
There are quite a few ways to initialize a vec that should be also taken into consideration, for example:
let ptr = Vec::from_elem(0,4).as_mut_ptr();
The text was updated successfully, but these errors were encountered:
warning: a dangling pointer will be produced because the temporary `Vec<i32>` will be dropped
--> src/main.rs:2:22
|
2 | let ptr = vec![0; 4].as_mut_ptr();
| ---------- ^^^^^^^^^^ this pointer will immediately be invalid
| |
| this `Vec<i32>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
= note: pointers do not have a lifetime; when calling `as_mut_ptr` the `Vec<i32>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
= note: `#[warn(dangling_pointers_from_temporaries)]` on by default
Hi!
Similarly to the
temporary_cstring_as_ptr
lint, a common mistake I've run into when dealing with FFI a bunch is immediately trying to get a raw pointer from an initialized vec:This is a really easy way to get a dangling pointer and can be easily overlooked. It would be great if we had a clippy lint for it and
as_ptr
as well.There are quite a few ways to initialize a vec that should be also taken into consideration, for example:
The text was updated successfully, but these errors were encountered: