Skip to content

Commit 644d36c

Browse files
committed
Remove unsafe in with_nix_path() for [u8]
1 parent 57d4c86 commit 644d36c

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/lib.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ pub mod unistd;
7878
*
7979
*/
8080

81-
use libc::{c_char, PATH_MAX};
81+
use libc::PATH_MAX;
8282

83-
use std::{ptr, result};
83+
use std::result;
8484
use std::ffi::{CStr, OsStr};
8585
use std::os::unix::ffi::OsStrExt;
8686
use std::path::{Path, PathBuf};
@@ -184,16 +184,10 @@ impl NixPath for [u8] {
184184
return Err(Errno::ENAMETOOLONG)
185185
}
186186

187-
match self.iter().position(|b| *b == 0) {
188-
Some(_) => Err(Errno::EINVAL),
189-
None => {
190-
unsafe {
191-
// TODO: Replace with bytes::copy_memory. rust-lang/rust#24028
192-
ptr::copy_nonoverlapping(self.as_ptr(), buf.as_mut_ptr(), self.len());
193-
Ok(f(CStr::from_ptr(buf.as_ptr() as *const c_char)))
194-
}
195-
196-
}
187+
buf[..self.len()].copy_from_slice(self);
188+
match CStr::from_bytes_with_nul(&buf[..=self.len()]) {
189+
Ok(s) => Ok(f(s)),
190+
Err(_) => Err(Errno::EINVAL),
197191
}
198192
}
199193
}

0 commit comments

Comments
 (0)