Skip to content

dangling_pointers_from_temporaries lint does not warn when as_ptr() result escapes function #139249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alex opened this issue Apr 2, 2025 · 4 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-dangling_pointers_from_temporaries Lint: dangling_pointers_from_temporaries L-false-negative Lint: False negative (should have fired but didn't). T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alex
Copy link
Member

alex commented Apr 2, 2025

Consider this basic dangling pointer from a temporary:

use std::ffi::CString;

unsafe extern "C" {
    fn g(v: *const i8);
}

pub fn f1(s: &str) {
    let p = CString::new(s).unwrap().as_ptr();
    unsafe { g(p); }
}

It produces a warning. Very good.

However, if instead we have an Option<&str> and we use some Option methods to convert it to a pointer, we no longer get a warning:

use std::ptr;
use std::ffi::CString;

unsafe extern "C" {
    fn g(v: *const i8);
}

pub fn f2(s: Option<&str>) {
    let p = s.map(|v| CString::new(v).unwrap()).map_or(ptr::null(), |v| v.as_ptr());
    unsafe { g(p); }
}

I believe the closure passed to map_or should be triggering the warning: it takes Option<CString> as an argument, it calls as_ptr(), it drops the Option<CString>, and then it makes use of the pointer (to return it).

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 2, 2025
@alex
Copy link
Member Author

alex commented Apr 2, 2025

(This was extracted from real code, so this is not a hypothetical consideration.)

@alex
Copy link
Member Author

alex commented Apr 2, 2025

Oh, this doesn't actually have anything to do with Option (other than that map_or easily produces this problem), the actual problem has a much simpler reproducer:

use std::ffi::CString;
pub fn f3(s: CString) -> *const i8 {
    s.as_ptr()
}

@alex
Copy link
Member Author

alex commented Apr 4, 2025

https://rustsec.org/advisories/RUSTSEC-2025-0022.html is a real-world instance of this

@alex alex changed the title dangling_pointers_from_temporaries lint does not warn on certain patterns involving Options and closures dangling_pointers_from_temporaries lint does not warn when as_ptr() result escapes function Apr 8, 2025
@lolbinarycat lolbinarycat added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Apr 8, 2025
@jieyouxu jieyouxu added L-dangling_pointers_from_temporaries Lint: dangling_pointers_from_temporaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. L-false-negative Lint: False negative (should have fired but didn't). and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 1, 2025
@jieyouxu jieyouxu marked this as a duplicate of #90449 May 1, 2025
@jieyouxu
Copy link
Member

jieyouxu commented May 1, 2025

Closing in favor of #78691, which had more false positive/negative examples, incl. this issue.

@jieyouxu jieyouxu closed this as completed May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-dangling_pointers_from_temporaries Lint: dangling_pointers_from_temporaries L-false-negative Lint: False negative (should have fired but didn't). T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants