Skip to content

Implement fixed-size-buffer constructors for CString #46795

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,115 @@ impl CString {
CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
}

/// Copies up to `maxlen` bytes from a C buffer into a new CString, and then appends
/// a null terminator. Copying stops early if a null byte is found before the end of
/// the buffer.
///
/// # Safety
///
/// No bytes past the end of the buffer will be accessed even if no null terminator
/// is found.
///
/// Since the data is copied, the buffer may be freed once this function returns.
///
/// # Examples
///
/// Receive a string into a fixed size buffer, and then extract it as a CString.
///
/// ```no_run
/// #![feature(cstr_of_fixed_size)]
///
/// use std::ffi::CString;
/// use std::os::raw::c_char;
/// use std::mem;
///
/// extern {
/// fn extern_fill_my_buf(s: *mut c_char, maxlen: usize);
/// }
///
/// unsafe {
/// let mut buf = mem::uninitialized::<[c_char; 256]>();
/// extern_fill_my_buf(buf.as_mut_ptr(), buf.len());
/// let c_string = CString::of_fixed_size_raw(buf.as_ptr(), buf.len());
/// }
/// ```
#[unstable(feature = "cstr_of_fixed_size", issue = "20475")]
pub unsafe fn of_fixed_size_raw(ptr: *const c_char, maxlen: usize) -> CString {
let len = sys::strnlen(ptr, maxlen) as usize; // Including the NUL byte
let mut result = Vec::with_capacity(len+1);
result.extend_from_slice(slice::from_raw_parts(ptr as *const u8, len));
CString::from_vec_unchecked(result)
}

/// Copies up to `maxlen` bytes from a buffer into a new CString, and then appends
/// a null terminator. Copying stops early if a null byte is found before the end of
/// the buffer.
///
/// # Examples
///
/// Receive a string into a fixed size buffer, and then extract it as a CString.
///
/// ```no_run
/// #![feature(cstr_of_fixed_size)]
///
/// use std::ffi::CString;
/// use std::os::raw::c_char;
/// use std::mem;
///
/// extern {
/// fn extern_fill_my_buf(s: *mut c_char, maxlen: usize);
/// }
///
/// unsafe {
/// let mut buf = mem::uninitialized::<[u8; 256]>();
/// extern_fill_my_buf(buf.as_mut_ptr() as *mut c_char, buf.len());
/// let c_string = CString::of_fixed_size(&buf[..]);
/// }
/// ```
#[unstable(feature = "cstr_of_fixed_size", issue = "20475")]
pub fn of_fixed_size(slice: &[u8]) -> CString {
unsafe {
CString::of_fixed_size_raw(slice.as_ptr() as *const c_char, slice.len())
}
}

/// Creates a new C-compatible string from a container of bytes.
///
/// The string is terminated at the first null byte present within the
/// container.
///
/// # Examples
///
/// Receive a string into a fixed size buffer, and then extract it as a CString.
///
/// ```no_run
/// #![feature(cstr_of_fixed_size)]
///
/// use std::ffi::CString;
/// use std::os::raw::c_char;
///
/// extern {
/// fn extern_fill_my_buf(s: *mut c_char, maxlen: usize);
/// }
///
/// unsafe {
/// let mut buf = vec![0u8; 256];
/// extern_fill_my_buf(buf.as_mut_ptr() as *mut c_char, buf.len());
/// let c_string = CString::from_fixed_size(buf);
/// }
/// ```
#[unstable(feature = "cstr_of_fixed_size", issue = "20475")]
pub fn from_fixed_size<T: Into<Vec<u8>>>(vec: T) -> CString {
Self::_from_fixed_size(vec.into())
}

fn _from_fixed_size(mut bytes: Vec<u8>) -> CString {
if let Some(i) = memchr::memchr(0, &bytes) {
bytes.truncate(i);
}
unsafe { CString::from_vec_unchecked(bytes) }
}

/// Consumes the `CString` and transfers ownership of the string to a C caller.
///
/// The pointer which this function returns must be returned to Rust and reconstituted using
Expand Down Expand Up @@ -1276,6 +1385,22 @@ mod tests {
}
}

#[test]
fn build_with_zero4() {
let s = CString::of_fixed_size(&b"1234\05678"[..]);
assert_eq!(s.as_bytes(), b"1234");
let s = CString::of_fixed_size(&b"1234"[..]);
assert_eq!(s.as_bytes(), b"1234");
}

#[test]
fn build_with_zero5() {
let s = CString::from_fixed_size(b"1234\05678".to_vec());
assert_eq!(s.as_bytes(), b"1234");
let s = CString::from_fixed_size(b"1234".to_vec());
assert_eq!(s.as_bytes(), b"1234");
}

#[test]
fn formatted() {
let s = CString::new(&b"abc\x01\x02\n\xE2\x80\xA6\xFF"[..]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/redox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use io::{self, ErrorKind};

pub use libc::strlen;
pub use libc::{strlen, strnlen};
pub use self::rand::hashmap_random_keys;

pub mod args;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use libc;
#[cfg(all(not(dox), target_os = "l4re"))] pub use os::linux as platform;

pub use self::rand::hashmap_random_keys;
pub use libc::strlen;
pub use libc::{strlen, strnlen};

#[macro_use]
pub mod weak;
Expand Down
9 changes: 9 additions & 0 deletions src/libstd/sys/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ pub unsafe fn strlen(mut s: *const c_char) -> usize {
return n
}

pub unsafe fn strnlen(mut s: *const c_char, maxlen: usize) -> usize {
let mut n = 0;
while n < maxlen && *s != 0 {
n += 1;
s = s.offset(1);
}
return n
}

pub unsafe fn abort_internal() -> ! {
::intrinsics::abort();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use os::windows::ffi::{OsStrExt, OsStringExt};
use path::PathBuf;
use time::Duration;

pub use libc::strlen;
pub use libc::{strlen, strnlen};
pub use self::rand::hashmap_random_keys;

#[macro_use] pub mod compat;
Expand Down