Various fixups to unsafe code (#37651)
A collection of fixups of possibly-unsound code and removing some small useless writes. Release Notes: - N/A
This commit is contained in:
+10
-5
@@ -20,6 +20,9 @@ use std::os::fd::{AsFd, AsRawFd};
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::fs::{FileTypeExt, MetadataExt};
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
use async_tar::Archive;
|
||||
use futures::{AsyncRead, Stream, StreamExt, future::BoxFuture};
|
||||
use git::repository::{GitRepository, RealGitRepository};
|
||||
@@ -261,14 +264,15 @@ impl FileHandle for std::fs::File {
|
||||
};
|
||||
|
||||
let fd = self.as_fd();
|
||||
let mut path_buf: [libc::c_char; libc::PATH_MAX as usize] = [0; libc::PATH_MAX as usize];
|
||||
let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();
|
||||
|
||||
let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };
|
||||
if result == -1 {
|
||||
anyhow::bail!("fcntl returned -1".to_string());
|
||||
}
|
||||
|
||||
let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr()) };
|
||||
// SAFETY: `fcntl` will initialize the path buffer.
|
||||
let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };
|
||||
let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
|
||||
Ok(path)
|
||||
}
|
||||
@@ -296,15 +300,16 @@ impl FileHandle for std::fs::File {
|
||||
};
|
||||
|
||||
let fd = self.as_fd();
|
||||
let mut kif: libc::kinfo_file = unsafe { std::mem::zeroed() };
|
||||
let mut kif = MaybeUninit::<libc::kinfo_file>::uninit();
|
||||
kif.kf_structsize = libc::KINFO_FILE_SIZE;
|
||||
|
||||
let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, &mut kif) };
|
||||
let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, kif.as_mut_ptr()) };
|
||||
if result == -1 {
|
||||
anyhow::bail!("fcntl returned -1".to_string());
|
||||
}
|
||||
|
||||
let c_str = unsafe { CStr::from_ptr(kif.kf_path.as_ptr()) };
|
||||
// SAFETY: `fcntl` will initialize the kif.
|
||||
let c_str = unsafe { CStr::from_ptr(kif.assume_init().kf_path.as_ptr()) };
|
||||
let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user