Skip to content

Commit 0ef89d7

Browse files
committed
dangling pointer from temp cleanup
1 parent e5efc33 commit 0ef89d7

File tree

18 files changed

+288
-287
lines changed

18 files changed

+288
-287
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,16 @@ lint_dangling_pointers_from_locals = {$fn_kind} returns a dangling pointer to dr
197197
.ret_ty = return type is `{$ret_ty}`
198198
.local_var = local variable `{$local_var_name}` is dropped at the end of the {$fn_kind}
199199
.created_at = dangling pointer created here
200+
.help_visit = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
200201
.note = a dangling pointer is safe, but dereferencing one is undefined behavior
201202
202-
lint_dangling_pointers_from_temporaries = a dangling pointer will be produced because the temporary `{$ty}` will be dropped
203-
.label_ptr = this pointer will immediately be invalid
204-
.label_temporary = this `{$ty}` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
205-
.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
206-
.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}`
207-
.help_returned = in particular, if this pointer is returned from the current function, binding the `{$ty}` inside the function will not suffice
203+
lint_dangling_pointers_from_temporaries = expression creates a dangling pointer to dropped temporary `{$ty}`
204+
.label_ptr = dangling pointer created here
205+
.label_temporary = this `{$ty}` is dropped at the end of the statement
206+
.help_bind = bind the `{$ty}` to a variable such that it outlives the pointer returned by `{$callee}`
207+
.help_returned = returning a pointer to a local variable will always result in a dangling pointer
208208
.help_visit = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
209+
.note = a dangling pointer is safe, but dereferencing one is undefined behavior
209210
210211
lint_default_hash_types = prefer `{$preferred}` over `{$used}`, it has better performance
211212
.note = a `use rustc_data_structures::fx::{$preferred}` may be necessary

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,10 @@ pub(crate) struct IgnoredUnlessCrateSpecified<'a> {
11061106
// dangling.rs
11071107
#[derive(LintDiagnostic)]
11081108
#[diag(lint_dangling_pointers_from_temporaries)]
1109-
#[note]
11101109
#[help(lint_help_bind)]
11111110
#[help(lint_help_returned)]
11121111
#[help(lint_help_visit)]
1112+
#[note]
11131113
// FIXME: put #[primary_span] on `ptr_span` once it does not cause conflicts
11141114
pub(crate) struct DanglingPointersFromTemporaries<'tcx> {
11151115
pub callee: Ident,

tests/ui/lint/dangling-pointers-from-temporaries/allow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
#[deny(dangling_pointers_from_temporaries)]
88
{
99
dbg!(String::new().as_ptr());
10-
//~^ ERROR a dangling pointer will be produced because the temporary `String` will be dropped
10+
//~^ ERROR dangling pointer
1111
}
1212
S.foo()
1313
}
@@ -18,6 +18,6 @@ impl S {
1818
#[warn(dangling_pointers_from_temporaries)]
1919
fn foo(self) {
2020
dbg!(String::new().as_ptr());
21-
//~^ WARNING a dangling pointer will be produced because the temporary `String` will be dropped
21+
//~^ WARNING dangling pointer
2222
}
2323
}

tests/ui/lint/dangling-pointers-from-temporaries/allow.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
error: a dangling pointer will be produced because the temporary `String` will be dropped
1+
error: expression creates a dangling pointer to dropped temporary `String`
22
--> $DIR/allow.rs:9:28
33
|
44
LL | dbg!(String::new().as_ptr());
5-
| ------------- ^^^^^^ this pointer will immediately be invalid
5+
| ------------- ^^^^^^ dangling pointer created here
66
| |
7-
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
7+
| this `String` is dropped at the end of the statement
88
|
9-
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10-
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
11-
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
9+
= help: bind the `String` to a variable such that it outlives the pointer returned by `as_ptr`
10+
= help: returning a pointer to a local variable will always result in a dangling pointer
1211
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
12+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
1313
note: the lint level is defined here
1414
--> $DIR/allow.rs:7:12
1515
|
1616
LL | #[deny(dangling_pointers_from_temporaries)]
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
warning: a dangling pointer will be produced because the temporary `String` will be dropped
19+
warning: expression creates a dangling pointer to dropped temporary `String`
2020
--> $DIR/allow.rs:20:28
2121
|
2222
LL | dbg!(String::new().as_ptr());
23-
| ------------- ^^^^^^ this pointer will immediately be invalid
23+
| ------------- ^^^^^^ dangling pointer created here
2424
| |
25-
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
25+
| this `String` is dropped at the end of the statement
2626
|
27-
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28-
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
29-
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
27+
= help: bind the `String` to a variable such that it outlives the pointer returned by `as_ptr`
28+
= help: returning a pointer to a local variable will always result in a dangling pointer
3029
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
30+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
3131
note: the lint level is defined here
3232
--> $DIR/allow.rs:18:12
3333
|

tests/ui/lint/dangling-pointers-from-temporaries/calls.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ fn ok() {
2525
fn not_ok() {
2626
{
2727
let ptr = cstring().as_ptr();
28-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
28+
//~^ ERROR dangling pointer
2929
consume(ptr);
3030
}
3131
consume({
3232
let ptr = cstring().as_ptr();
33-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
33+
//~^ ERROR dangling pointer
3434
ptr
3535
});
3636
consume({
@@ -39,11 +39,11 @@ fn not_ok() {
3939
//^ FIXME: should error
4040
});
4141
let _ptr: *const u8 = cstring().as_ptr().cast();
42-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
42+
//~^ ERROR dangling pointer
4343
let _ptr: *const u8 = { cstring() }.as_ptr().cast();
44-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
44+
//~^ ERROR dangling pointer
4545
let _ptr: *const u8 = { cstring().as_ptr() }.cast();
46-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
46+
//~^ ERROR dangling pointer
4747
}
4848

4949
fn main() {
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
1+
error: expression creates a dangling pointer to dropped temporary `CString`
22
--> $DIR/calls.rs:27:29
33
|
44
LL | let ptr = cstring().as_ptr();
5-
| --------- ^^^^^^ this pointer will immediately be invalid
5+
| --------- ^^^^^^ dangling pointer created here
66
| |
7-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
7+
| this `CString` is dropped at the end of the statement
88
|
9-
= 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
10-
= 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`
11-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
9+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
10+
= help: returning a pointer to a local variable will always result in a dangling pointer
1211
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
12+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
1313
note: the lint level is defined here
1414
--> $DIR/calls.rs:1:9
1515
|
1616
LL | #![deny(dangling_pointers_from_temporaries)]
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
19+
error: expression creates a dangling pointer to dropped temporary `CString`
2020
--> $DIR/calls.rs:32:29
2121
|
2222
LL | let ptr = cstring().as_ptr();
23-
| --------- ^^^^^^ this pointer will immediately be invalid
23+
| --------- ^^^^^^ dangling pointer created here
2424
| |
25-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
25+
| this `CString` is dropped at the end of the statement
2626
|
27-
= 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
28-
= 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`
29-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
27+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
28+
= help: returning a pointer to a local variable will always result in a dangling pointer
3029
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
30+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
3131

32-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
32+
error: expression creates a dangling pointer to dropped temporary `CString`
3333
--> $DIR/calls.rs:41:37
3434
|
3535
LL | let _ptr: *const u8 = cstring().as_ptr().cast();
36-
| --------- ^^^^^^ this pointer will immediately be invalid
36+
| --------- ^^^^^^ dangling pointer created here
3737
| |
38-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
38+
| this `CString` is dropped at the end of the statement
3939
|
40-
= 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
41-
= 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`
42-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
40+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
41+
= help: returning a pointer to a local variable will always result in a dangling pointer
4342
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
43+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
4444

45-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
45+
error: expression creates a dangling pointer to dropped temporary `CString`
4646
--> $DIR/calls.rs:43:41
4747
|
4848
LL | let _ptr: *const u8 = { cstring() }.as_ptr().cast();
49-
| ------------- ^^^^^^ this pointer will immediately be invalid
49+
| ------------- ^^^^^^ dangling pointer created here
5050
| |
51-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
51+
| this `CString` is dropped at the end of the statement
5252
|
53-
= 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
54-
= 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`
55-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
53+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
54+
= help: returning a pointer to a local variable will always result in a dangling pointer
5655
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
56+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
5757

58-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
58+
error: expression creates a dangling pointer to dropped temporary `CString`
5959
--> $DIR/calls.rs:45:39
6060
|
6161
LL | let _ptr: *const u8 = { cstring().as_ptr() }.cast();
62-
| --------- ^^^^^^ this pointer will immediately be invalid
62+
| --------- ^^^^^^ dangling pointer created here
6363
| |
64-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
64+
| this `CString` is dropped at the end of the statement
6565
|
66-
= 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
67-
= 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`
68-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
66+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
67+
= help: returning a pointer to a local variable will always result in a dangling pointer
6968
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
69+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
7070

7171
error: aborting due to 5 previous errors
7272

tests/ui/lint/dangling-pointers-from-temporaries/cstring-as-ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use std::ffi::CString;
77
macro_rules! mymacro {
88
() => {
99
let s = CString::new("some text").unwrap().as_ptr();
10-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
10+
//~^ ERROR dangling pointer
1111
}
1212
}
1313

1414
fn main() {
1515
let s = CString::new("some text").unwrap().as_ptr();
16-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
16+
//~^ ERROR dangling pointer
1717
mymacro!();
1818
}

tests/ui/lint/dangling-pointers-from-temporaries/cstring-as-ptr.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@ LL | #![deny(temporary_cstring_as_ptr)]
66
|
77
= note: `#[warn(renamed_and_removed_lints)]` on by default
88

9-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
9+
error: expression creates a dangling pointer to dropped temporary `CString`
1010
--> $DIR/cstring-as-ptr.rs:15:48
1111
|
1212
LL | let s = CString::new("some text").unwrap().as_ptr();
13-
| ---------------------------------- ^^^^^^ this pointer will immediately be invalid
13+
| ---------------------------------- ^^^^^^ dangling pointer created here
1414
| |
15-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
15+
| this `CString` is dropped at the end of the statement
1616
|
17-
= 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
18-
= 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`
19-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
17+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
18+
= help: returning a pointer to a local variable will always result in a dangling pointer
2019
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
20+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
2121
note: the lint level is defined here
2222
--> $DIR/cstring-as-ptr.rs:2:9
2323
|
2424
LL | #![deny(temporary_cstring_as_ptr)]
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^
2626

27-
error: a dangling pointer will be produced because the temporary `CString` will be dropped
27+
error: expression creates a dangling pointer to dropped temporary `CString`
2828
--> $DIR/cstring-as-ptr.rs:9:52
2929
|
3030
LL | let s = CString::new("some text").unwrap().as_ptr();
31-
| ---------------------------------- ^^^^^^ this pointer will immediately be invalid
31+
| ---------------------------------- ^^^^^^ dangling pointer created here
3232
| |
33-
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
33+
| this `CString` is dropped at the end of the statement
3434
...
3535
LL | mymacro!();
3636
| ---------- in this macro invocation
3737
|
38-
= 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
39-
= 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`
40-
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
38+
= help: bind the `CString` to a variable such that it outlives the pointer returned by `as_ptr`
39+
= help: returning a pointer to a local variable will always result in a dangling pointer
4140
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
41+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
4242
= note: this error originates in the macro `mymacro` (in Nightly builds, run with -Z macro-backtrace for more info)
4343

4444
error: aborting due to 2 previous errors; 1 warning emitted

tests/ui/lint/dangling-pointers-from-temporaries/example-from-issue123613.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const MAX_PATH: usize = 260;
44
fn main() {
55
let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
6-
//~^ ERROR a dangling pointer will be produced because the temporary `String` will be dropped
6+
//~^ ERROR dangling pointer
77
let str2 = String::from("TotototototototototototototototototoT").as_ptr();
8-
//~^ ERROR a dangling pointer will be produced because the temporary `String` will be dropped
8+
//~^ ERROR dangling pointer
99
unsafe {
1010
std::ptr::copy_nonoverlapping(str2, str1, 30);
1111
println!("{:?}", String::from_raw_parts(str1, 30, 30));

0 commit comments

Comments
 (0)