Skip to content

Commit 68f7b26

Browse files
authored
Rollup merge of rust-lang#34916 - tbu-:pr_comment_on_seek_cast, r=GuillaumeGomez
Comment on the casts in the `seek` implementations on files
2 parents 8e04264 + 291b6f1 commit 68f7b26

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/libstd/sys/unix/fs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use sys_common::{AsInner, FromInner};
2727
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
2828
use libc::{stat64, fstat64, lstat64, off64_t, ftruncate64, lseek64, dirent64, readdir64_r, open64};
2929
#[cfg(target_os = "android")]
30-
use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, off64_t, lseek64,
30+
use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, lseek64,
3131
dirent as dirent64, open as open64};
3232
#[cfg(not(any(target_os = "linux",
3333
target_os = "emscripten",
@@ -485,9 +485,11 @@ impl File {
485485

486486
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
487487
let (whence, pos) = match pos {
488-
SeekFrom::Start(off) => (libc::SEEK_SET, off as off64_t),
489-
SeekFrom::End(off) => (libc::SEEK_END, off as off64_t),
490-
SeekFrom::Current(off) => (libc::SEEK_CUR, off as off64_t),
488+
// Casting to `i64` is fine, too large values will end up as
489+
// negative which will cause an error in `lseek64`.
490+
SeekFrom::Start(off) => (libc::SEEK_SET, off as i64),
491+
SeekFrom::End(off) => (libc::SEEK_END, off),
492+
SeekFrom::Current(off) => (libc::SEEK_CUR, off),
491493
};
492494
let n = cvt(unsafe { lseek64(self.0.raw(), pos, whence) })?;
493495
Ok(n as u64)

src/libstd/sys/windows/fs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ impl File {
324324

325325
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
326326
let (whence, pos) = match pos {
327+
// Casting to `i64` is fine, `SetFilePointerEx` reinterprets this
328+
// integer as `u64`.
327329
SeekFrom::Start(n) => (c::FILE_BEGIN, n as i64),
328330
SeekFrom::End(n) => (c::FILE_END, n),
329331
SeekFrom::Current(n) => (c::FILE_CURRENT, n),

0 commit comments

Comments
 (0)