Skip to content
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

Clarify/Correct example of a dangling pointer in FFI/Passing-Strings #313

Open
ImplOfAnImpl opened this issue Jul 1, 2022 · 0 comments
Open
Labels
C-amendment Category: Amendments to existing content C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content C-question Category: Further information is requested

Comments

@ImplOfAnImpl
Copy link

Hi.

I'm pretty new to Rust, so I may be missing something. But anyway :-)

So, ffi/passing-strings has this example:

fn report_error<S: Into<String>>(err: S) -> Result<(), std::ffi::NulError> {
    unsafe {
        // SAFETY: whoops, this contains a dangling pointer!
        seterr(std::ffi::CString::new(err.into())?.as_ptr());
    }
    Ok(())
}

I wonder, how can the pointer be dangling here, isn't the temporary that it's pointing to supposed to live until the end of the entire statement? I.e. the call to seterr should be completed before it's dropped.

Also, there was a review comment that suggested rewording this example completely, so that it would instead discourage constructing a temporary and obtaining a pointer to it in a let-statement. It seems that the comment's author also considered the example to be incorrect.

@simonsan simonsan added C-question Category: Further information is requested C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content C-amendment Category: Amendments to existing content labels Apr 8, 2023
@simonsan simonsan changed the title The example of a dangling pointer in ffi/passing-strings - is it correct? Clarify/Correct example of a dangling pointer in FFI/Passing-Strings Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-amendment Category: Amendments to existing content C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content C-question Category: Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants