-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.L-dangling_pointers_from_temporariesLint: dangling_pointers_from_temporariesLint: dangling_pointers_from_temporariesL-false-positiveLint: False positive (should not have fired).Lint: False positive (should not have fired).T-langRelevant to the language teamRelevant to the language team
Description
I tried this code:
use std::ffi::CString;
extern "C" {
fn gecko_profiler_register_thread(foo: *const std::os::raw::c_char);
}
pub fn main() {
unsafe {
gecko_profiler_register_thread(CString::new("foo").unwrap().as_ptr());
}
}
I get:
warning: getting the inner pointer of a temporary `CString`
--> <source>:9:69
|
9 | gecko_profiler_register_thread(CString::new("foo").unwrap().as_ptr());
| ---------------------------- ^^^^^^ this pointer will be invalid
| |
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
= note: `#[warn(temporary_cstring_as_ptr)]` on by default
= 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: for more information, see https://doc.rust-lang.org/reference/destructors.html
warning: 1 warning emitted
Compiler returned: 0
However since the CString is not deallocated until after the function call the code is fine and the warning should not trigger.
See https://rust.godbolt.org/z/vjrvGn for the assembly.
CoconutMacaroon and correabuscar
Metadata
Metadata
Assignees
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.L-dangling_pointers_from_temporariesLint: dangling_pointers_from_temporariesLint: dangling_pointers_from_temporariesL-false-positiveLint: False positive (should not have fired).Lint: False positive (should not have fired).T-langRelevant to the language teamRelevant to the language team