about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTobias Bucher <tobiasbucher5991@gmail.com>2018-05-12 02:31:38 +0200
committerTobias Bucher <tobiasbucher5991@gmail.com>2018-05-12 02:31:38 +0200
commit4ce24269bb498b799ad14bd1fe3fb11900c1ce64 (patch)
treeab7870ee3f9af6c2ec3e554f760dd03010d5e7fb
parent9e3caa23f99816caf3c2fb69ff5c88b512fb1b38 (diff)
downloadrust-4ce24269bb498b799ad14bd1fe3fb11900c1ce64.tar.gz
rust-4ce24269bb498b799ad14bd1fe3fb11900c1ce64.zip
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.
-rw-r--r--src/libstd/sys/unix/fd.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/libstd/sys/unix/fd.rs b/src/libstd/sys/unix/fd.rs
index 5dafc3251e7..67546d06b4e 100644
--- a/src/libstd/sys/unix/fd.rs
+++ b/src/libstd/sys/unix/fd.rs
@@ -75,8 +75,15 @@ impl FileDesc {
         unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: usize, offset: i64)
             -> io::Result<isize>
         {
+            use convert::TryInto;
             use libc::pread64;
-            cvt(pread64(fd, buf, count, offset as i32))
+            // pread64 on emscripten actually takes a 32 bit offset
+            if let Ok(o) = offset.try_into() {
+                cvt(pread64(fd, buf, count, o))
+            } else {
+                Err(io::Error::new(io::ErrorKind::InvalidInput,
+                                   "cannot pread >2GB"))
+            }
         }
 
         #[cfg(not(any(target_os = "android", target_os = "emscripten")))]
@@ -116,8 +123,15 @@ impl FileDesc {
         unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: usize, offset: i64)
             -> io::Result<isize>
         {
+            use convert::TryInto;
             use libc::pwrite64;
-            cvt(pwrite64(fd, buf, count, offset as i32))
+            // pwrite64 on emscripten actually takes a 32 bit offset
+            if let Ok(o) = offset.try_into() {
+                cvt(pwrite64(fd, buf, count, o))
+            } else {
+                Err(io::Error::new(io::ErrorKind::InvalidInput,
+                                   "cannot pwrite >2GB"))
+            }
         }
 
         #[cfg(not(any(target_os = "android", target_os = "emscripten")))]