Skip to content

dangling_pointers_from_temporaries lint is imprecise (false positives and false negatives) #78691

Open
@jrmuizel

Description

@jrmuizel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-dangling_pointers_from_temporariesLint: dangling_pointers_from_temporariesL-false-positiveLint: False positive (should not have fired).T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions