From 1f99a9321946db0c79a805caa95e7cc2fe310f25 Mon Sep 17 00:00:00 2001 From: Rick Richardson Date: Wed, 7 Jan 2015 13:55:12 -0500 Subject: more cstr fallout --- src/mount.rs | 2 +- src/sys/utsname.rs | 19 ++++++++++--------- src/unistd.rs | 4 ++-- src/utils.rs | 7 +++++++ 4 files changed, 20 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/mount.rs b/src/mount.rs index fa0f845a..20302352 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -1,8 +1,8 @@ -use std::c_str::ToCStr; use std::ptr; use std::path::Path; use libc::{c_ulong, c_int, c_void}; use errno::{SysResult, from_ffi}; +use utils::ToCStr; bitflags!( flags MsFlags: c_ulong { diff --git a/src/sys/utsname.rs b/src/sys/utsname.rs index ce76a3be..528b7766 100644 --- a/src/sys/utsname.rs +++ b/src/sys/utsname.rs @@ -1,6 +1,7 @@ use std::mem; -use std::c_str::CString; use libc::{c_char}; +use std::ffi::{c_str_to_bytes_with_nul}; +use std::str::from_utf8_unchecked; mod ffi { use libc::c_int; @@ -29,23 +30,23 @@ pub struct UtsName { impl UtsName { pub fn sysname<'a>(&'a self) -> &'a str { - to_str(&self.sysname as *const c_char) + to_str(&(&self.sysname as *const c_char ) as *const *const c_char) } pub fn nodename<'a>(&'a self) -> &'a str { - to_str(&self.nodename as *const c_char) + to_str(&(&self.nodename as *const c_char ) as *const *const c_char) } pub fn release<'a>(&'a self) -> &'a str { - to_str(&self.release as *const c_char) + to_str(&(&self.release as *const c_char ) as *const *const c_char) } pub fn version<'a>(&'a self) -> &'a str { - to_str(&self.version as *const c_char) + to_str(&(&self.version as *const c_char ) as *const *const c_char) } pub fn machine<'a>(&'a self) -> &'a str { - to_str(&self.machine as *const c_char) + to_str(&(&self.machine as *const c_char ) as *const *const c_char) } } @@ -58,9 +59,9 @@ pub fn uname() -> UtsName { } #[inline] -fn to_str<'a>(s: *const c_char) -> &'a str { +fn to_str<'a>(s: *const *const c_char) -> &'a str { unsafe { - let res = CString::new(s, false); - mem::transmute(res.as_str().expect("[BUG] uname field not UTF-8")) + let res = c_str_to_bytes_with_nul(mem::transmute(s)); + from_utf8_unchecked(res) } } diff --git a/src/unistd.rs b/src/unistd.rs index 0948fabc..1acbda3d 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1,4 +1,3 @@ -use std::ffi::CString; use std::{mem, ptr}; use libc::{c_char, c_void, c_int, size_t, pid_t, off_t}; use fcntl::{fcntl, Fd, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC}; @@ -7,6 +6,7 @@ use fcntl::FcntlArg::{F_SETFD, F_SETFL}; use errno::{SysResult, SysError, from_ffi}; use core::raw::Slice as RawSlice; use utils::ToCStr; +use std::ffi::CString; #[cfg(target_os = "linux")] pub use self::linux::*; @@ -411,10 +411,10 @@ pub fn ftruncate(fd: Fd, len: off_t) -> SysResult<()> { #[cfg(target_os = "linux")] mod linux { - use std::c_str::ToCStr; use std::path::Path; use syscall::{syscall, SYSPIVOTROOT}; use errno::{SysResult, SysError}; + use utils::ToCStr; pub fn pivot_root(new_root: &Path, put_old: &Path) -> SysResult<()> { let new_root = new_root.to_c_str(); diff --git a/src/utils.rs b/src/utils.rs index a6e279af..332cd500 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -11,6 +11,13 @@ impl ToCStr for Path { } } +impl<'a> ToCStr for &'a str { + fn to_c_str(&self) -> CString { + CString::from_slice(self.as_bytes()) + } +} + + impl ToCStr for String { fn to_c_str(&self) -> CString { CString::from_slice(self.as_bytes()) -- cgit v1.2.3