Skip to content

Commit d71625b

Browse files
committed
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.
1 parent 9e3caa2 commit d71625b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/libstd/sys/unix/fd.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ impl FileDesc {
7676
-> io::Result<isize>
7777
{
7878
use libc::pread64;
79-
cvt(pread64(fd, buf, count, offset as i32))
79+
// pread64 on emscripten actually takes a 32 bit offset
80+
if let Ok(o) = offset.try_into() {
81+
cvt(pread64(fd, buf, count, o))
82+
} else {
83+
Err(io::Error::new(io::ErrorKind::InvalidInput,
84+
"cannot pread >2GB"))
85+
}
8086
}
8187

8288
#[cfg(not(any(target_os = "android", target_os = "emscripten")))]
@@ -117,7 +123,13 @@ impl FileDesc {
117123
-> io::Result<isize>
118124
{
119125
use libc::pwrite64;
120-
cvt(pwrite64(fd, buf, count, offset as i32))
126+
// pwrite64 on emscripten actually takes a 32 bit offset
127+
if let Ok(o) = offset.try_into() {
128+
cvt(pwrite64(fd, buf, count, o))
129+
} else {
130+
Err(io::Error::new(io::ErrorKind::InvalidInput,
131+
"cannot pwrite >2GB"))
132+
}
121133
}
122134

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

0 commit comments

Comments
 (0)