Open
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.