summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorRyan Zoeller <rtzoeller@rtzoeller.com>2021-10-22 22:50:29 -0500
committerRyan Zoeller <rtzoeller@rtzoeller.com>2021-10-22 22:50:29 -0500
commit644d36cf1fa5515dade300e42de6f9877c892204 (patch)
tree40d0e7086ad9cea3511fdf7037439d17b5040ff2 /src/lib.rs
parent57d4c863ab3d1b25a58f6ebeea36bcb8fadcf36b (diff)
downloadnix-644d36cf1fa5515dade300e42de6f9877c892204.zip
Remove unsafe in with_nix_path() for [u8]
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs18
1 files changed, 6 insertions, 12 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 3a2b63ab..9f09c9fd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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),
}
}
}