Skip to content

Commit 50ad367

Browse files
authored
Rollup merge of rust-lang#50634 - tbu-:pr_preadwrite_emscripten, r=kennytm
Do not silently truncate offsets for `read_at`/`write_at` on emscripten Generate an IO error if the offset is out of bounds for the system call.
2 parents ded7a7e + 4ce2426 commit 50ad367

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/libstd/sys/unix/fd.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ impl FileDesc {
7575
unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: usize, offset: i64)
7676
-> io::Result<isize>
7777
{
78+
use convert::TryInto;
7879
use libc::pread64;
79-
cvt(pread64(fd, buf, count, offset as i32))
80+
// pread64 on emscripten actually takes a 32 bit offset
81+
if let Ok(o) = offset.try_into() {
82+
cvt(pread64(fd, buf, count, o))
83+
} else {
84+
Err(io::Error::new(io::ErrorKind::InvalidInput,
85+
"cannot pread >2GB"))
86+
}
8087
}
8188

8289
#[cfg(not(any(target_os = "android", target_os = "emscripten")))]
@@ -116,8 +123,15 @@ impl FileDesc {
116123
unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: usize, offset: i64)
117124
-> io::Result<isize>
118125
{
126+
use convert::TryInto;
119127
use libc::pwrite64;
120-
cvt(pwrite64(fd, buf, count, offset as i32))
128+
// pwrite64 on emscripten actually takes a 32 bit offset
129+
if let Ok(o) = offset.try_into() {
130+
cvt(pwrite64(fd, buf, count, o))
131+
} else {
132+
Err(io::Error::new(io::ErrorKind::InvalidInput,
133+
"cannot pwrite >2GB"))
134+
}
121135
}
122136

123137
#[cfg(not(any(target_os = "android", target_os = "emscripten")))]

0 commit comments

Comments
 (0)