From a70ae2ffb9a5dd08d916b9938eeca820486ba7a0 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Tue, 29 Dec 2015 13:41:43 -0500 Subject: [PATCH 1/4] CStr::from_bytes --- src/libstd/ffi/c_str.rs | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index ee744b868dd73..f50e906f14c48 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -436,6 +436,57 @@ impl CStr { mem::transmute(slice::from_raw_parts(ptr, len as usize + 1)) } + /// Creates a C string wrapper from a byte slice. + /// + /// This function will cast the provided `bytes` to a `CStr` wrapper after + /// ensuring that it is null terminated but does not contain any interior + /// nul bytes. + /// + /// # Examples + /// + /// ``` + /// # #![feature(cstr_from_bytes)] + /// use std::ffi::CStr; + /// + /// # fn main() { + /// let cstr = CStr::from_bytes(b"hello\0"); + /// assert!(cstr.is_some()); + /// # } + /// ``` + #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "0")] + pub fn from_bytes<'a>(bytes: &'a [u8]) -> Option<&'a CStr> { + if bytes.is_empty() || memchr::memchr(0, &bytes) != Some(bytes.len() - 1) { + None + } else { + Some(unsafe { Self::from_bytes_unchecked(bytes) }) + } + } + + /// Unsafely creates a C string wrapper from a byte slice. + /// + /// This function will cast the provided `bytes` to a `CStr` wrapper without + /// performing any sanity checks. The provided slice must be null terminated + /// and not contain any interior nul bytes. + /// + /// # Examples + /// + /// ``` + /// # #![feature(cstr_from_bytes)] + /// use std::ffi::{CStr, CString}; + /// + /// # fn main() { + /// unsafe { + /// let cstring = CString::new("hello").unwrap(); + /// let cstr = CStr::from_bytes_unchecked(cstring.to_bytes_with_nul()); + /// assert_eq!(cstr, &*cstring); + /// } + /// # } + /// ``` + #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "0")] + pub unsafe fn from_bytes_unchecked<'a>(bytes: &'a [u8]) -> &'a CStr { + mem::transmute(bytes) + } + /// Returns the inner pointer to this C string. /// /// The returned pointer will be valid for as long as `self` is and points From 16f218b6006faa752925ca5934372c34f1eb2953 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Sun, 24 Jan 2016 19:51:19 -0500 Subject: [PATCH 2/4] Rename CStr::from_bytes to from_bytes_with_nul --- src/libstd/ffi/c_str.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index f50e906f14c48..e3c51a74b0962 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -449,16 +449,16 @@ impl CStr { /// use std::ffi::CStr; /// /// # fn main() { - /// let cstr = CStr::from_bytes(b"hello\0"); + /// let cstr = CStr::from_bytes_with_nul(b"hello\0"); /// assert!(cstr.is_some()); /// # } /// ``` #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "0")] - pub fn from_bytes<'a>(bytes: &'a [u8]) -> Option<&'a CStr> { + pub fn from_bytes_with_nul<'a>(bytes: &'a [u8]) -> Option<&'a CStr> { if bytes.is_empty() || memchr::memchr(0, &bytes) != Some(bytes.len() - 1) { None } else { - Some(unsafe { Self::from_bytes_unchecked(bytes) }) + Some(unsafe { Self::from_bytes_with_nul_unchecked(bytes) }) } } @@ -477,13 +477,13 @@ impl CStr { /// # fn main() { /// unsafe { /// let cstring = CString::new("hello").unwrap(); - /// let cstr = CStr::from_bytes_unchecked(cstring.to_bytes_with_nul()); + /// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul()); /// assert_eq!(cstr, &*cstring); /// } /// # } /// ``` #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "0")] - pub unsafe fn from_bytes_unchecked<'a>(bytes: &'a [u8]) -> &'a CStr { + pub unsafe fn from_bytes_with_nul_unchecked<'a>(bytes: &'a [u8]) -> &'a CStr { mem::transmute(bytes) } From 9414c4ea18dab870bc0acbcd2be9a882aa17d68e Mon Sep 17 00:00:00 2001 From: arcnmx Date: Tue, 23 Feb 2016 01:36:50 -0500 Subject: [PATCH 3/4] Link cstr from_bytes to tracking issue --- src/libstd/ffi/c_str.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index e3c51a74b0962..f3561622fa377 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -439,7 +439,7 @@ impl CStr { /// Creates a C string wrapper from a byte slice. /// /// This function will cast the provided `bytes` to a `CStr` wrapper after - /// ensuring that it is null terminated but does not contain any interior + /// ensuring that it is null terminated and does not contain any interior /// nul bytes. /// /// # Examples @@ -453,8 +453,8 @@ impl CStr { /// assert!(cstr.is_some()); /// # } /// ``` - #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "0")] - pub fn from_bytes_with_nul<'a>(bytes: &'a [u8]) -> Option<&'a CStr> { + #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "31190")] + pub fn from_bytes_with_nul(bytes: &[u8]) -> Option<&CStr> { if bytes.is_empty() || memchr::memchr(0, &bytes) != Some(bytes.len() - 1) { None } else { @@ -482,8 +482,8 @@ impl CStr { /// } /// # } /// ``` - #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "0")] - pub unsafe fn from_bytes_with_nul_unchecked<'a>(bytes: &'a [u8]) -> &'a CStr { + #[unstable(feature = "cstr_from_bytes", reason = "recently added", issue = "31190")] + pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr { mem::transmute(bytes) } From 71f29cd83704b39a2aefd609eab645decd3bce92 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Tue, 23 Feb 2016 01:37:21 -0500 Subject: [PATCH 4/4] CStr::from_bytes_with_nul tests --- src/libstd/ffi/c_str.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index f3561622fa377..1db4576455239 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -721,4 +721,31 @@ mod tests { assert_eq!(cstr_hash, cstring_hash); } + + #[test] + fn from_bytes_with_nul() { + let data = b"123\0"; + let cstr = CStr::from_bytes_with_nul(data); + assert_eq!(cstr.map(CStr::to_bytes), Some(&b"123"[..])); + assert_eq!(cstr.map(CStr::to_bytes_with_nul), Some(&b"123\0"[..])); + + unsafe { + let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data); + assert_eq!(cstr, Some(cstr_unchecked)); + } + } + + #[test] + fn from_bytes_with_nul_unterminated() { + let data = b"123"; + let cstr = CStr::from_bytes_with_nul(data); + assert!(cstr.is_none()); + } + + #[test] + fn from_bytes_with_nul_interior() { + let data = b"1\023\0"; + let cstr = CStr::from_bytes_with_nul(data); + assert!(cstr.is_none()); + } }