Skip to content

Commit 5dbcf91

Browse files
committed
Always cast pread, lseek etc. offsets to off_t.
When calling libc functions that take `off_t` arguments, always cast to `off_t`. On platforms where this cast is a no-op, these casts should be optimized away. This eliminates the need for explicit `cfg`s for platforms which need the casts. Fixes #1432.
1 parent cb01fbe commit 5dbcf91

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/backend/libc/fs/syscalls.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,8 @@ pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
14041404
}
14051405
};
14061406

1407-
// ESP-IDF and Vita don't support 64-bit offsets.
1408-
#[cfg(any(target_os = "espidf", target_os = "vita"))]
1409-
let offset: i32 = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
1407+
// ESP-IDF and Vita don't support 64-bit offsets, for example.
1408+
let offset = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
14101409

14111410
let offset = unsafe { ret_off_t(c::lseek(borrowed_fd(fd), offset, whence))? };
14121411
Ok(offset as u64)
@@ -1734,10 +1733,13 @@ pub(crate) fn fallocate(
17341733
offset: u64,
17351734
len: u64,
17361735
) -> io::Result<()> {
1737-
// Silently cast; we'll get `EINVAL` if the value is negative.
1736+
// Silently cast to `i64`; we'll get `EINVAL` if the value is negative.
17381737
let offset = offset as i64;
17391738
let len = len as i64;
17401739

1740+
// ESP-IDF and Vita don't support 64-bit offsets, for example.
1741+
let offset = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
1742+
17411743
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
17421744
unsafe {
17431745
ret(c::fallocate(

src/backend/libc/io/syscalls.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ pub(crate) unsafe fn pread(
5252
// Silently cast; we'll get `EINVAL` if the value is negative.
5353
let offset = offset as i64;
5454

55-
// ESP-IDF and Vita don't support 64-bit offsets.
56-
#[cfg(any(target_os = "espidf", target_os = "vita"))]
57-
let offset: i32 = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
55+
// ESP-IDF and Vita don't support 64-bit offsets, for example.
56+
let offset = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
5857

5958
ret_usize(c::pread(borrowed_fd(fd), buf.0.cast(), len, offset))
6059
}
@@ -65,9 +64,8 @@ pub(crate) fn pwrite(fd: BorrowedFd<'_>, buf: &[u8], offset: u64) -> io::Result<
6564
// Silently cast; we'll get `EINVAL` if the value is negative.
6665
let offset = offset as i64;
6766

68-
// ESP-IDF and Vita don't support 64-bit offsets.
69-
#[cfg(any(target_os = "espidf", target_os = "vita"))]
70-
let offset: i32 = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
67+
// ESP-IDF and Vita don't support 64-bit offsets, for example.
68+
let offset = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
7169

7270
unsafe { ret_usize(c::pwrite(borrowed_fd(fd), buf.as_ptr().cast(), len, offset)) }
7371
}
@@ -111,6 +109,10 @@ pub(crate) fn preadv(
111109
) -> io::Result<usize> {
112110
// Silently cast; we'll get `EINVAL` if the value is negative.
113111
let offset = offset as i64;
112+
113+
// ESP-IDF and Vita don't support 64-bit offsets, for example.
114+
let offset = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
115+
114116
unsafe {
115117
ret_usize(c::preadv(
116118
borrowed_fd(fd),
@@ -134,6 +136,10 @@ pub(crate) fn preadv(
134136
pub(crate) fn pwritev(fd: BorrowedFd<'_>, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
135137
// Silently cast; we'll get `EINVAL` if the value is negative.
136138
let offset = offset as i64;
139+
140+
// ESP-IDF and Vita don't support 64-bit offsets, for example.
141+
let offset = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?;
142+
137143
unsafe {
138144
ret_usize(c::pwritev(
139145
borrowed_fd(fd),

0 commit comments

Comments
 (0)