Closed
Description
Feature gate: #![feature(cstr_from_bytes_until_nul)]
This is a tracking issue for adding member fn to CStr
that convert from a byte slice, without the caller needing to know where the nul byte is located within the slice.
This is intended for use in FFI calls, where a foreign function wrote a string into a Rust-allocated buffer. The existing member fns fall short in this case:
CStr::from_ptr
is unsafe and may read past the end of the buffer if no nul byte is found.CStr::from_bytes_with_nul
only works if there is exactly one nul byte at the end of the slice.
The proposed new member fn (tentatively named from_bytes_until_nul
) is easier and can be used safely on any byte slice.
Public API
// std::ffi:
pub struct FromBytesUntilNulError(...);
impl CStr {
pub fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError>;
}
Steps / History
- CStr Implementation: add
CStr
method that accepts any slice containing a nul-terminated string #94984 - Final comment period (FCP): Stabilize feature
cstr_from_bytes_until_nul
#107429 (comment) - Stabilization PR: Stabilize feature
cstr_from_bytes_until_nul
#107429
Unresolved Questions
CString
API needs to be designed.- With CStr eventually revised to just be a pointer, rather than (ptr, len) pair, then this becomes a little less good. See add
CStr
method that accepts any slice containing a nul-terminated string #94984 (comment) for details.