Skip to content

Commit

Permalink
iOS/tvOS/watchOS/visionOS: Fix reading large files
Browse files Browse the repository at this point in the history
Tested in the iOS simulator with something like:
```
let mut buf = vec![0; c_int::MAX as usize - 1 + 2];
let read_bytes = f.read(&mut buf).unwrap();
```
  • Loading branch information
madsmtm committed May 6, 2024
1 parent 53bd38b commit 28622c9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions library/std/src/sys/pal/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ pub struct FileDesc(OwnedFd);
// with the man page quoting that if the count of bytes to read is
// greater than `SSIZE_MAX` the result is "unspecified".
//
// On macOS, however, apparently the 64-bit libc is either buggy or
// On Apple targets however, apparently the 64-bit libc is either buggy or
// intentionally showing odd behavior by rejecting any read with a size
// larger than or equal to INT_MAX. To handle both of these the read
// size is capped on both platforms.
#[cfg(target_os = "macos")]
const READ_LIMIT: usize = libc::c_int::MAX as usize - 1;
#[cfg(not(target_os = "macos"))]
const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
const READ_LIMIT: usize = if cfg!(target_vendor = "apple") {
libc::c_int::MAX as usize - 1
} else {
libc::ssize_t::MAX as usize
};

#[cfg(any(
target_os = "dragonfly",
Expand Down

0 comments on commit 28622c9

Please sign in to comment.