-
Notifications
You must be signed in to change notification settings - Fork 13.9k
dangling pointer from temp cleanup #148490
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,15 +197,16 @@ lint_dangling_pointers_from_locals = {$fn_kind} returns a dangling pointer to dr | |
| .ret_ty = return type is `{$ret_ty}` | ||
| .local_var = local variable `{$local_var_name}` is dropped at the end of the {$fn_kind} | ||
| .created_at = dangling pointer created here | ||
| .help_visit = for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| .note = a dangling pointer is safe, but dereferencing one is undefined behavior | ||
| lint_dangling_pointers_from_temporaries = a dangling pointer will be produced because the temporary `{$ty}` will be dropped | ||
| .label_ptr = this pointer will immediately be invalid | ||
| .label_temporary = this `{$ty}` 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 `{$callee}` the `{$ty}` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned | ||
| .help_bind = you must make sure that the variable you bind the `{$ty}` to lives at least as long as the pointer returned by the call to `{$callee}` | ||
| .help_returned = in particular, if this pointer is returned from the current function, binding the `{$ty}` inside the function will not suffice | ||
| lint_dangling_pointers_from_temporaries = expression creates a dangling pointer to dropped temporary `{$ty}` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that's the a good description of the issue, I feel like it's a bit misleading, the temporary is not yet dropped (otherwise this would insta UB), it's dropped only after creating the pointer, and it's the dropping of the temporary which makes the pointer dangling. The previously wording was clearer IMO on this point. Not sure how to best improve that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I thought that detail did not matter. Do you have a reference for your claim that that would be insta UB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For But for allocation like Footnotes |
||
| .label_ptr = dangling pointer created here | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also is misleading IMO, it's not yet dangling but it will be. |
||
| .label_temporary = this `{$ty}` is dropped at the end of the statement | ||
| .help_bind = bind the `{$ty}` to a variable such that it outlives the pointer returned by `{$callee}` | ||
| .help_returned = returning a pointer to a local variable will always result in a dangling pointer | ||
| .help_visit = for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| .note = a dangling pointer is safe, but dereferencing one is undefined behavior | ||
| lint_default_hash_types = prefer `{$preferred}` over `{$used}`, it has better performance | ||
| .note = a `use rustc_data_structures::fx::{$preferred}` may be necessary | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,72 +1,72 @@ | ||
| error: a dangling pointer will be produced because the temporary `CString` will be dropped | ||
| error: expression creates a dangling pointer to dropped temporary `CString` | ||
| --> $DIR/calls.rs:27:29 | ||
| | | ||
| LL | let ptr = cstring().as_ptr(); | ||
| | --------- ^^^^^^ this pointer will immediately be invalid | ||
| | --------- ^^^^^^ dangling pointer created here | ||
| | | | ||
| | this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime | ||
| | this `CString` is dropped at the end of the statement | ||
| | | ||
| = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned | ||
| = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr` | ||
| = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice | ||
| = help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr` | ||
| = help: returning a pointer to a local variable will always result in a dangling pointer | ||
| = help: for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| = note: a dangling pointer is safe, but dereferencing one is undefined behavior | ||
| note: the lint level is defined here | ||
| --> $DIR/calls.rs:1:9 | ||
| | | ||
| LL | #![deny(dangling_pointers_from_temporaries)] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: a dangling pointer will be produced because the temporary `CString` will be dropped | ||
| error: expression creates a dangling pointer to dropped temporary `CString` | ||
| --> $DIR/calls.rs:32:29 | ||
| | | ||
| LL | let ptr = cstring().as_ptr(); | ||
| | --------- ^^^^^^ this pointer will immediately be invalid | ||
| | --------- ^^^^^^ dangling pointer created here | ||
| | | | ||
| | this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime | ||
| | this `CString` is dropped at the end of the statement | ||
| | | ||
| = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned | ||
| = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr` | ||
| = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice | ||
| = help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr` | ||
| = help: returning a pointer to a local variable will always result in a dangling pointer | ||
| = help: for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| = note: a dangling pointer is safe, but dereferencing one is undefined behavior | ||
|
|
||
| error: a dangling pointer will be produced because the temporary `CString` will be dropped | ||
| error: expression creates a dangling pointer to dropped temporary `CString` | ||
| --> $DIR/calls.rs:41:37 | ||
| | | ||
| LL | let _ptr: *const u8 = cstring().as_ptr().cast(); | ||
| | --------- ^^^^^^ this pointer will immediately be invalid | ||
| | --------- ^^^^^^ dangling pointer created here | ||
| | | | ||
| | this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime | ||
| | this `CString` is dropped at the end of the statement | ||
| | | ||
| = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned | ||
| = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr` | ||
| = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice | ||
| = help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr` | ||
| = help: returning a pointer to a local variable will always result in a dangling pointer | ||
| = help: for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| = note: a dangling pointer is safe, but dereferencing one is undefined behavior | ||
|
|
||
| error: a dangling pointer will be produced because the temporary `CString` will be dropped | ||
| error: expression creates a dangling pointer to dropped temporary `CString` | ||
| --> $DIR/calls.rs:43:41 | ||
| | | ||
| LL | let _ptr: *const u8 = { cstring() }.as_ptr().cast(); | ||
| | ------------- ^^^^^^ this pointer will immediately be invalid | ||
| | ------------- ^^^^^^ dangling pointer created here | ||
| | | | ||
| | this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime | ||
| | this `CString` is dropped at the end of the statement | ||
| | | ||
| = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned | ||
| = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr` | ||
| = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice | ||
| = help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr` | ||
| = help: returning a pointer to a local variable will always result in a dangling pointer | ||
| = help: for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| = note: a dangling pointer is safe, but dereferencing one is undefined behavior | ||
|
|
||
| error: a dangling pointer will be produced because the temporary `CString` will be dropped | ||
| error: expression creates a dangling pointer to dropped temporary `CString` | ||
| --> $DIR/calls.rs:45:39 | ||
| | | ||
| LL | let _ptr: *const u8 = { cstring().as_ptr() }.cast(); | ||
| | --------- ^^^^^^ this pointer will immediately be invalid | ||
| | --------- ^^^^^^ dangling pointer created here | ||
| | | | ||
| | this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime | ||
| | this `CString` is dropped at the end of the statement | ||
| | | ||
| = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned | ||
| = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr` | ||
| = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice | ||
| = help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr` | ||
| = help: returning a pointer to a local variable will always result in a dangling pointer | ||
| = help: for more information, see <https://doc.rust-lang.org/reference/destructors.html> | ||
| = note: a dangling pointer is safe, but dereferencing one is undefined behavior | ||
|
|
||
| error: aborting due to 5 previous errors | ||
|
|
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.
That's not used right?
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.
Right, I forgot to wire that up. Will fix.