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
Copy file name to clipboardExpand all lines: compiler/rustc_lint/src/methods.rs
+20-2Lines changed: 20 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,25 @@ use rustc_middle::ty;
6
6
use rustc_span::{symbol::sym,ExpnKind,Span};
7
7
8
8
declare_lint!{
9
+
/// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of
10
+
/// a temporary `CString`.
11
+
///
12
+
/// ### Example
13
+
///
14
+
/// ```rust
15
+
/// # #![allow(unused)]
16
+
/// let c_str = CString::new("foo").unwrap().as_ptr();
17
+
/// ```
18
+
///
19
+
/// {{produces}}
20
+
///
21
+
/// ### Explanation
22
+
///
23
+
/// The inner pointer of a `CString` lives only as long as the `CString` it
24
+
/// points to. Getting the inner pointer of a *temporary* `CString` allows the `CString`
25
+
/// to be dropped at the end of the statement, as it is not being referenced as far as the typesystem
26
+
/// is concerned. This means outside of the statement the pointer will point to freed memory, which
27
+
/// causes undefined behavior if the pointer is later dereferenced.
9
28
pubTEMPORARY_CSTRING_AS_PTR,
10
29
Warn,
11
30
"detects getting the inner pointer of a temporary `CString`"
@@ -75,8 +94,7 @@ fn lint_cstring_as_ptr(
75
94
unwrap.span,
76
95
"this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime",
77
96
);
78
-
diag.note("pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement...");
79
-
diag.note("...because nothing is referencing it as far as the type system is concerned");
97
+
diag.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");
80
98
diag.help("for more information, see https://doc.rust-lang.org/reference/destructors.html");
Copy file name to clipboardExpand all lines: src/test/ui/lint/lint-temporary-cstring-as-param.stderr
+1-2Lines changed: 1 addition & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,7 @@ note: the lint level is defined here
11
11
|
12
12
LL | #![deny(temporary_cstring_as_ptr)]
13
13
| ^^^^^^^^^^^^^^^^^^^^^^^^
14
-
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement...
15
-
= note: ...because nothing is referencing it as far as the type system is concerned
14
+
= 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
16
15
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
Copy file name to clipboardExpand all lines: src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr
+3-4Lines changed: 3 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,17 @@
1
1
error: getting the inner pointer of a temporary `CString`
2
-
--> $DIR/lint-temporary-cstring-as-ptr.rs:7:48
2
+
--> $DIR/lint-temporary-cstring-as-ptr.rs:8:48
3
3
|
4
4
LL | let s = CString::new("some text").unwrap().as_ptr();
5
5
| ---------------------------------- ^^^^^^ this pointer will be invalid
6
6
| |
7
7
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
8
8
|
9
9
note: the lint level is defined here
10
-
--> $DIR/lint-temporary-cstring-as-ptr.rs:2:9
10
+
--> $DIR/lint-temporary-cstring-as-ptr.rs:3:9
11
11
|
12
12
LL | #![deny(temporary_cstring_as_ptr)]
13
13
| ^^^^^^^^^^^^^^^^^^^^^^^^
14
-
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement...
15
-
= note: ...because nothing is referencing it as far as the type system is concerned
14
+
= 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
16
15
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
0 commit comments