Skip to content

Commit

Permalink
Pass null instead of empty slices to getxattr and fgetxattr on macos (#…
Browse files Browse the repository at this point in the history
…1045)

fixes the remainder of #957
fixes Stebalien/xattr#57
  • Loading branch information
Stebalien committed Apr 15, 2024
1 parent fa21a6d commit 48eeb83
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/backend/libc/fs/syscalls.rs
Expand Up @@ -2254,15 +2254,24 @@ pub(crate) fn getxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result
}

#[cfg(apple)]
unsafe {
ret_usize(c::getxattr(
path.as_ptr(),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
0,
0,
))
{
// Passing an empty to slice to getxattr leads to ERANGE on macOS. Pass null
// instead.
let ptr = if value.is_empty() {
core::ptr::null_mut()
} else {
value_ptr.cast::<c::c_void>()
};
unsafe {
ret_usize(c::getxattr(
path.as_ptr(),
name.as_ptr(),
ptr,
value.len(),
0,
0,
))
}
}
}

Expand Down Expand Up @@ -2318,15 +2327,24 @@ pub(crate) fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value: &mut [u8]) -> io
}

#[cfg(apple)]
unsafe {
ret_usize(c::fgetxattr(
borrowed_fd(fd),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
0,
0,
))
{
// Passing an empty to slice to getxattr leads to ERANGE on macOS. Pass null
// instead.
let ptr = if value.is_empty() {
core::ptr::null_mut()
} else {
value_ptr.cast::<c::c_void>()
};
unsafe {
ret_usize(c::fgetxattr(
borrowed_fd(fd),
name.as_ptr(),
ptr,
value.len(),
0,
0,
))
}
}
}

Expand Down

0 comments on commit 48eeb83

Please sign in to comment.