diff options
author | Ryan Zoeller <rtzoeller@rtzoeller.com> | 2021-10-22 22:50:29 -0500 |
---|---|---|
committer | Ryan Zoeller <rtzoeller@rtzoeller.com> | 2021-10-22 22:50:29 -0500 |
commit | 644d36cf1fa5515dade300e42de6f9877c892204 (patch) | |
tree | 40d0e7086ad9cea3511fdf7037439d17b5040ff2 | |
parent | 57d4c863ab3d1b25a58f6ebeea36bcb8fadcf36b (diff) | |
download | nix-644d36cf1fa5515dade300e42de6f9877c892204.zip |
Remove unsafe in with_nix_path() for [u8]
-rw-r--r-- | src/lib.rs | 18 |
1 files changed, 6 insertions, 12 deletions
@@ -78,9 +78,9 @@ pub mod unistd; * */ -use libc::{c_char, PATH_MAX}; +use libc::PATH_MAX; -use std::{ptr, result}; +use std::result; use std::ffi::{CStr, OsStr}; use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; @@ -184,16 +184,10 @@ impl NixPath for [u8] { return Err(Errno::ENAMETOOLONG) } - match self.iter().position(|b| *b == 0) { - Some(_) => Err(Errno::EINVAL), - None => { - unsafe { - // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028 - ptr::copy_nonoverlapping(self.as_ptr(), buf.as_mut_ptr(), self.len()); - Ok(f(CStr::from_ptr(buf.as_ptr() as *const c_char))) - } - - } + buf[..self.len()].copy_from_slice(self); + match CStr::from_bytes_with_nul(&buf[..=self.len()]) { + Ok(s) => Ok(f(s)), + Err(_) => Err(Errno::EINVAL), } } } |