Skip to content

Commit

Permalink
Clarify hazards in 'as *const _ casts of *const u8 to *const libc…
Browse files Browse the repository at this point in the history
…::c_char`.
  • Loading branch information
briansmith committed May 20, 2024
1 parent 656d744 commit 76bede3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/util_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ impl Weak {
// the use of non-Relaxed operations is probably unnecessary.
match self.addr.load(Ordering::Relaxed) {
Self::UNINIT => {
let symbol = self.name.as_ptr() as *const _;
// XXX/FIXME: Unchecked UTF-8-to-c_char cast.
let symbol = self.name.as_ptr().cast::<libc::c_char>();
let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, symbol) };
// Synchronizes with the Acquire fence below
self.addr.store(addr, Ordering::Release);
Expand All @@ -136,7 +137,11 @@ impl Weak {
pub unsafe fn open_readonly(path: &str) -> Result<libc::c_int, Error> {
debug_assert_eq!(path.as_bytes().last(), Some(&0));
loop {
let fd = libc::open(path.as_ptr() as *const _, libc::O_RDONLY | libc::O_CLOEXEC);
// XXX/FIXME: Unchecked UTF-8-to-c_char cast.
let fd = libc::open(
path.as_ptr().cast::<libc::c_char>(),
libc::O_RDONLY | libc::O_CLOEXEC,
);
if fd >= 0 {
return Ok(fd);
}
Expand All @@ -154,7 +159,7 @@ pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
unsafe {
libc::syscall(
libc::SYS_getrandom,
buf.as_mut_ptr().cast::<libc::c_void>(),
buf.as_mut_ptr() as *mut libc::c_void,
buf.len(),
0,
) as libc::ssize_t
Expand Down

0 comments on commit 76bede3

Please sign in to comment.