Skip to content

Comments

Keep message pointer valid by binding CString#2

Open
chlohal wants to merge 1 commit intotozny:masterfrom
chlohal:master
Open

Keep message pointer valid by binding CString#2
chlohal wants to merge 1 commit intotozny:masterfrom
chlohal:master

Conversation

@chlohal
Copy link

@chlohal chlohal commented Mar 14, 2023

From the docs of as_ptr():

It is your responsibility to make sure that the underlying memory is not freed too early. For example, the following code will cause undefined behavior when ptr is used inside the unsafe block:

use std::ffi::CString;

let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
unsafe {
    // `ptr` is dangling
    *ptr;
}

I noticed that this causes an issue in PamConv::send, and fixed it by binding the CString to a variable, keeping it valid through the use of the pointer.

Please let me know if there are any issues with my PR! Although it's short, I'm just learning Rust, so I would definitely appreciate correction on any pitfalls that I've fallen into :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant