Skip to content

Commit 7acff86

Browse files
committed
instantly_dangling_pointer, temporary_cstring_as_ptr: add and move tests
1 parent bcd95b8 commit 7acff86

10 files changed

+291
-5
lines changed

tests/ui/lint/lint-temporary-cstring-as-param.stderr renamed to tests/ui/lint/dangling-ptr/cstring-as-param.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: getting a pointer from a temporary `CString` will result in a dangling pointer
2-
--> $DIR/lint-temporary-cstring-as-param.rs:9:45
2+
--> $DIR/cstring-as-param.rs:9:45
33
|
44
LL | some_function(CString::new("").unwrap().as_ptr());
55
| ------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -9,7 +9,7 @@ LL | some_function(CString::new("").unwrap().as_ptr());
99
= 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
1010
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1111
note: the lint level is defined here
12-
--> $DIR/lint-temporary-cstring-as-param.rs:1:9
12+
--> $DIR/cstring-as-param.rs:1:9
1313
|
1414
LL | #![deny(temporary_cstring_as_ptr)]
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/lint/lint-temporary-cstring-as-ptr.stderr renamed to tests/ui/lint/dangling-ptr/cstring-as-ptr.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: getting a pointer from a temporary `CString` will result in a dangling pointer
2-
--> $DIR/lint-temporary-cstring-as-ptr.rs:14:48
2+
--> $DIR/cstring-as-ptr.rs:14:48
33
|
44
LL | let s = CString::new("some text").unwrap().as_ptr();
55
| ---------------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -9,13 +9,13 @@ LL | let s = CString::new("some text").unwrap().as_ptr();
99
= 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
1010
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1111
note: the lint level is defined here
12-
--> $DIR/lint-temporary-cstring-as-ptr.rs:2:9
12+
--> $DIR/cstring-as-ptr.rs:2:9
1313
|
1414
LL | #![deny(temporary_cstring_as_ptr)]
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^
1616

1717
error: getting a pointer from a temporary `CString` will result in a dangling pointer
18-
--> $DIR/lint-temporary-cstring-as-ptr.rs:8:52
18+
--> $DIR/cstring-as-ptr.rs:8:52
1919
|
2020
LL | let s = CString::new("some text").unwrap().as_ptr();
2121
| ---------------------------------- ^^^^^^ this pointer will immediately be invalid
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![deny(instantly_dangling_pointer)]
2+
3+
const MAX_PATH: usize = 260;
4+
fn main() {
5+
let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
6+
//~^ ERROR [instantly_dangling_pointer]
7+
let str2 = String::from("TotototototototototototototototototoT").as_ptr();
8+
//~^ ERROR [instantly_dangling_pointer]
9+
unsafe {
10+
std::ptr::copy_nonoverlapping(str2, str1, 30);
11+
println!("{:?}", String::from_raw_parts(str1, 30, 30));
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: getting a pointer from a temporary `String` will result in a dangling pointer
2+
--> $DIR/instantly-dangling-pointer-issue123613.rs:5:48
3+
|
4+
LL | let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
5+
| ------------------------------- ^^^^^^^^^^ this pointer will immediately be invalid
6+
| |
7+
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
8+
|
9+
= note: pointers do not have a lifetime; when calling `as_mut_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: for more information, see https://doc.rust-lang.org/reference/destructors.html
11+
note: the lint level is defined here
12+
--> $DIR/instantly-dangling-pointer-issue123613.rs:1:9
13+
|
14+
LL | #![deny(instantly_dangling_pointer)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
error: getting a pointer from a temporary `String` will result in a dangling pointer
18+
--> $DIR/instantly-dangling-pointer-issue123613.rs:7:70
19+
|
20+
LL | let str2 = String::from("TotototototototototototototototototoT").as_ptr();
21+
| ----------------------------------------------------- ^^^^^^ this pointer will immediately be invalid
22+
| |
23+
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
24+
|
25+
= 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
26+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
27+
28+
error: aborting due to 2 previous errors
29+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![deny(instantly_dangling_pointer)]
2+
3+
fn main() {
4+
vec![0u8].as_ptr(); //~ ERROR [instantly_dangling_pointer]
5+
vec![0u8].as_mut_ptr(); //~ ERROR [instantly_dangling_pointer]
6+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: getting a pointer from a temporary `Vec<u8>` will result in a dangling pointer
2+
--> $DIR/instantly-dangling-pointer-methods.rs:4:15
3+
|
4+
LL | vec![0u8].as_ptr();
5+
| --------- ^^^^^^ this pointer will immediately be invalid
6+
| |
7+
| this `Vec<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
8+
|
9+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
11+
note: the lint level is defined here
12+
--> $DIR/instantly-dangling-pointer-methods.rs:1:9
13+
|
14+
LL | #![deny(instantly_dangling_pointer)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
error: getting a pointer from a temporary `Vec<u8>` will result in a dangling pointer
18+
--> $DIR/instantly-dangling-pointer-methods.rs:5:15
19+
|
20+
LL | vec![0u8].as_mut_ptr();
21+
| --------- ^^^^^^^^^^ this pointer will immediately be invalid
22+
| |
23+
| this `Vec<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
24+
|
25+
= note: pointers do not have a lifetime; when calling `as_mut_ptr` the `Vec<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
26+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
27+
28+
error: aborting due to 2 previous errors
29+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![deny(instantly_dangling_pointer)]
2+
3+
use std::cell::Cell;
4+
use std::ffi::{CStr, CString};
5+
use std::mem::MaybeUninit;
6+
7+
struct AsPtrFake;
8+
9+
impl AsPtrFake {
10+
fn as_ptr(&self) -> *const () {
11+
std::ptr::null()
12+
}
13+
}
14+
15+
fn declval<T>() -> T {
16+
loop {}
17+
}
18+
19+
fn main() {
20+
declval::<CString>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
21+
declval::<String>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
22+
declval::<Vec<u8>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
23+
declval::<Box<CString>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
24+
declval::<Box<[u8]>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
25+
declval::<Box<str>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
26+
declval::<Box<CStr>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
27+
declval::<[u8; 10]>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
28+
declval::<Box<[u8; 10]>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
29+
declval::<Box<Vec<u8>>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
30+
declval::<Box<String>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
31+
declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
32+
declval::<Cell<u8>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
33+
declval::<MaybeUninit<u8>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
34+
declval::<Vec<AsPtrFake>>().as_ptr(); //~ ERROR [instantly_dangling_pointer]
35+
declval::<Box<AsPtrFake>>().as_ptr();
36+
declval::<AsPtrFake>().as_ptr();
37+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
error: getting a pointer from a temporary `CString` will result in a dangling pointer
2+
--> $DIR/instantly-dangling-pointer-types.rs:20:26
3+
|
4+
LL | declval::<CString>().as_ptr();
5+
| -------------------- ^^^^^^ this pointer will immediately be invalid
6+
| |
7+
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
8+
|
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: for more information, see https://doc.rust-lang.org/reference/destructors.html
11+
note: the lint level is defined here
12+
--> $DIR/instantly-dangling-pointer-types.rs:1:9
13+
|
14+
LL | #![deny(instantly_dangling_pointer)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
error: getting a pointer from a temporary `String` will result in a dangling pointer
18+
--> $DIR/instantly-dangling-pointer-types.rs:21:25
19+
|
20+
LL | declval::<String>().as_ptr();
21+
| ------------------- ^^^^^^ this pointer will immediately be invalid
22+
| |
23+
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
24+
|
25+
= 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
26+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
27+
28+
error: getting a pointer from a temporary `Vec<u8>` will result in a dangling pointer
29+
--> $DIR/instantly-dangling-pointer-types.rs:22:26
30+
|
31+
LL | declval::<Vec<u8>>().as_ptr();
32+
| -------------------- ^^^^^^ this pointer will immediately be invalid
33+
| |
34+
| this `Vec<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
35+
|
36+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
37+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
38+
39+
error: getting a pointer from a temporary `Box<CString>` will result in a dangling pointer
40+
--> $DIR/instantly-dangling-pointer-types.rs:23:31
41+
|
42+
LL | declval::<Box<CString>>().as_ptr();
43+
| ------------------------- ^^^^^^ this pointer will immediately be invalid
44+
| |
45+
| this `Box<CString>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
46+
|
47+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<CString>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
48+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
49+
50+
error: getting a pointer from a temporary `Box<[u8]>` will result in a dangling pointer
51+
--> $DIR/instantly-dangling-pointer-types.rs:24:28
52+
|
53+
LL | declval::<Box<[u8]>>().as_ptr();
54+
| ---------------------- ^^^^^^ this pointer will immediately be invalid
55+
| |
56+
| this `Box<[u8]>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
57+
|
58+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<[u8]>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
59+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
60+
61+
error: getting a pointer from a temporary `Box<str>` will result in a dangling pointer
62+
--> $DIR/instantly-dangling-pointer-types.rs:25:27
63+
|
64+
LL | declval::<Box<str>>().as_ptr();
65+
| --------------------- ^^^^^^ this pointer will immediately be invalid
66+
| |
67+
| this `Box<str>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
68+
|
69+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<str>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
70+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
71+
72+
error: getting a pointer from a temporary `Box<CStr>` will result in a dangling pointer
73+
--> $DIR/instantly-dangling-pointer-types.rs:26:28
74+
|
75+
LL | declval::<Box<CStr>>().as_ptr();
76+
| ---------------------- ^^^^^^ this pointer will immediately be invalid
77+
| |
78+
| this `Box<CStr>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
79+
|
80+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<CStr>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
81+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
82+
83+
error: getting a pointer from a temporary `[u8; 10]` will result in a dangling pointer
84+
--> $DIR/instantly-dangling-pointer-types.rs:27:27
85+
|
86+
LL | declval::<[u8; 10]>().as_ptr();
87+
| --------------------- ^^^^^^ this pointer will immediately be invalid
88+
| |
89+
| this `[u8; 10]` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
90+
|
91+
= note: pointers do not have a lifetime; when calling `as_ptr` the `[u8; 10]` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
92+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
93+
94+
error: getting a pointer from a temporary `Box<[u8; 10]>` will result in a dangling pointer
95+
--> $DIR/instantly-dangling-pointer-types.rs:28:32
96+
|
97+
LL | declval::<Box<[u8; 10]>>().as_ptr();
98+
| -------------------------- ^^^^^^ this pointer will immediately be invalid
99+
| |
100+
| this `Box<[u8; 10]>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
101+
|
102+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<[u8; 10]>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
103+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
104+
105+
error: getting a pointer from a temporary `Box<Vec<u8>>` will result in a dangling pointer
106+
--> $DIR/instantly-dangling-pointer-types.rs:29:31
107+
|
108+
LL | declval::<Box<Vec<u8>>>().as_ptr();
109+
| ------------------------- ^^^^^^ this pointer will immediately be invalid
110+
| |
111+
| this `Box<Vec<u8>>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
112+
|
113+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<Vec<u8>>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
114+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
115+
116+
error: getting a pointer from a temporary `Box<String>` will result in a dangling pointer
117+
--> $DIR/instantly-dangling-pointer-types.rs:30:30
118+
|
119+
LL | declval::<Box<String>>().as_ptr();
120+
| ------------------------ ^^^^^^ this pointer will immediately be invalid
121+
| |
122+
| this `Box<String>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
123+
|
124+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<String>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
125+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
126+
127+
error: getting a pointer from a temporary `Box<Box<Box<Box<[u8]>>>>` will result in a dangling pointer
128+
--> $DIR/instantly-dangling-pointer-types.rs:31:43
129+
|
130+
LL | declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr();
131+
| ------------------------------------- ^^^^^^ this pointer will immediately be invalid
132+
| |
133+
| this `Box<Box<Box<Box<[u8]>>>>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
134+
|
135+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Box<Box<Box<Box<[u8]>>>>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
136+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
137+
138+
error: getting a pointer from a temporary `Cell<u8>` will result in a dangling pointer
139+
--> $DIR/instantly-dangling-pointer-types.rs:32:27
140+
|
141+
LL | declval::<Cell<u8>>().as_ptr();
142+
| --------------------- ^^^^^^ this pointer will immediately be invalid
143+
| |
144+
| this `Cell<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
145+
|
146+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Cell<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
147+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
148+
149+
error: getting a pointer from a temporary `MaybeUninit<u8>` will result in a dangling pointer
150+
--> $DIR/instantly-dangling-pointer-types.rs:33:34
151+
|
152+
LL | declval::<MaybeUninit<u8>>().as_ptr();
153+
| ---------------------------- ^^^^^^ this pointer will immediately be invalid
154+
| |
155+
| this `MaybeUninit<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
156+
|
157+
= note: pointers do not have a lifetime; when calling `as_ptr` the `MaybeUninit<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
158+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
159+
160+
error: getting a pointer from a temporary `Vec<AsPtrFake>` will result in a dangling pointer
161+
--> $DIR/instantly-dangling-pointer-types.rs:34:33
162+
|
163+
LL | declval::<Vec<AsPtrFake>>().as_ptr();
164+
| --------------------------- ^^^^^^ this pointer will immediately be invalid
165+
| |
166+
| this `Vec<AsPtrFake>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
167+
|
168+
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<AsPtrFake>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
169+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
170+
171+
error: aborting due to 15 previous errors
172+

0 commit comments

Comments
 (0)