Skip to content

Commit 3c379b5

Browse files
authored
Make the offsets for SeekFrom's Hole and Data unsigned. (#1266)
* Make the offsets for `SeekFrom`'s `Hole` and `Data` unsigned. These represent absolute offsets from the start of the file, so they should be unsigned, for consistency with `Start`. * Update tests.
1 parent 032304f commit 3c379b5

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/backend/libc/fs/syscalls.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,17 @@ pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
12931293
SeekFrom::End(offset) => (c::SEEK_END, offset),
12941294
SeekFrom::Current(offset) => (c::SEEK_CUR, offset),
12951295
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
1296-
SeekFrom::Data(offset) => (c::SEEK_DATA, offset),
1296+
SeekFrom::Data(pos) => {
1297+
let pos: u64 = pos;
1298+
// Silently cast; we'll get `EINVAL` if the value is negative.
1299+
(c::SEEK_DATA, pos as i64)
1300+
}
12971301
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
1298-
SeekFrom::Hole(offset) => (c::SEEK_HOLE, offset),
1302+
SeekFrom::Hole(pos) => {
1303+
let pos: u64 = pos;
1304+
// Silently cast; we'll get `EINVAL` if the value is negative.
1305+
(c::SEEK_HOLE, pos as i64)
1306+
}
12991307
};
13001308

13011309
// ESP-IDF and Vita don't support 64-bit offsets.

src/backend/linux_raw/fs/syscalls.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,16 @@ pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
238238
}
239239
SeekFrom::End(offset) => (SEEK_END, offset),
240240
SeekFrom::Current(offset) => (SEEK_CUR, offset),
241-
SeekFrom::Data(offset) => (SEEK_DATA, offset),
242-
SeekFrom::Hole(offset) => (SEEK_HOLE, offset),
241+
SeekFrom::Data(pos) => {
242+
let pos: u64 = pos;
243+
// Silently cast; we'll get `EINVAL` if the value is negative.
244+
(SEEK_DATA, pos as i64)
245+
}
246+
SeekFrom::Hole(pos) => {
247+
let pos: u64 = pos;
248+
// Silently cast; we'll get `EINVAL` if the value is negative.
249+
(SEEK_HOLE, pos as i64)
250+
}
243251
};
244252
_seek(fd, offset, whence)
245253
}

src/fs/seek_from.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ pub enum SeekFrom {
4141
///
4242
/// [`Errno::NXIO`]: crate::io::Errno::NXIO
4343
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
44-
Data(i64),
44+
Data(u64),
4545

4646
/// Sets the offset to the current position plus the specified number of
4747
/// bytes, plus the distance to the next byte which is in a hole.
4848
///
4949
/// If there is no hole past the offset, it will be set to the end of the
5050
/// file i.e. there is an implicit hole at the end of any file.
5151
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
52-
Hole(i64),
52+
Hole(u64),
5353
}

tests/fs/seek.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,15 @@ fn test_seek_holes() {
4343
assert_eq!(seek(&file, SeekFrom::Start(0)), Ok(0));
4444
assert_eq!(seek(&file, SeekFrom::Current(0)), Ok(0));
4545
assert_eq!(seek(&file, SeekFrom::Hole(0)), Ok(hole_size));
46-
assert_eq!(seek(&file, SeekFrom::Hole(hole_size as i64)), Ok(hole_size));
46+
assert_eq!(seek(&file, SeekFrom::Hole(hole_size)), Ok(hole_size));
4747
assert_eq!(
48-
seek(&file, SeekFrom::Hole(hole_size as i64 * 2)),
48+
seek(&file, SeekFrom::Hole(hole_size * 2)),
4949
Ok(hole_size * 2 + 6)
5050
);
5151
assert_eq!(seek(&file, SeekFrom::Data(0)), Ok(0));
52+
assert_eq!(seek(&file, SeekFrom::Data(hole_size)), Ok(hole_size * 2));
5253
assert_eq!(
53-
seek(&file, SeekFrom::Data(hole_size as i64)),
54-
Ok(hole_size * 2)
55-
);
56-
assert_eq!(
57-
seek(&file, SeekFrom::Data(hole_size as i64 * 2)),
54+
seek(&file, SeekFrom::Data(hole_size * 2)),
5855
Ok(hole_size * 2)
5956
);
6057
}

0 commit comments

Comments
 (0)