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

Creating CString from nul-terminated data #73100

Closed
r4v3n6101 opened this issue Jun 7, 2020 · 2 comments · Fixed by #73139
Closed

Creating CString from nul-terminated data #73100

r4v3n6101 opened this issue Jun 7, 2020 · 2 comments · Fixed by #73139
Labels
A-FFI Area: Foreign function interface (FFI) C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@r4v3n6101
Copy link

r4v3n6101 commented Jun 7, 2020

It seems there's no idiomatic way to create CString from nul-terminated Vec<u8>. The only approriate way is to pop() last nul and then from_vec_unchecked will append a new one. Need the method just passes data as is (e.g. CString::from_vec_with_nul such as CStr::from_bytes_with_nul).
Possibly, code should be like:

pub unsafe fn from_vec_with_nul_unchecked(v: Vec<u8>) -> CString {
    CString { inner: v.into_boxed_slice() }
}

and safe version:

pub fn from_vec_with_nul(v: Vec<u8>) -> Result<CString, FromBytesWithNulError> {
    let nul_pos = memchr::memchr(0, bytes);
    match nul_pos {
        Some(nul_pos) if nul_pos + 1 == v.len() => Ok(unsafe { CString::from_vec_with_nul_unchecked(v) }),
        Some(nul_pos) => Err(FromBytesWithNulError::interior_nul(nul_pos)),
        None => Err(FromBytesWithNulError::not_nul_terminated()),
    }
}
@jonas-schievink jonas-schievink added A-FFI Area: Foreign function interface (FFI) C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jun 7, 2020
@poliorcetics
Copy link
Contributor

I can do it if the lib team is ok with it being added.

@Xanewok
Copy link
Member

Xanewok commented Jun 8, 2020

Not a part of libs team but it seems like it covers a certain gap from std and I know that such PRs are definitely encouraged!

RalfJung added a commit to RalfJung/rust that referenced this issue Jun 15, 2020
…nul, r=dtolnay

Add methods to go from a nul-terminated Vec<u8> to a CString

Fixes rust-lang#73100.

Doc tests have been written and the documentation on the error type
updated too.

I used `#[stable(feature = "cstring_from_vec_with_nul", since = "1.46.0")]` but I don't know if the version is correct.
@bors bors closed this as completed in ec6fe42 Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI) C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants