]> git.lizzy.rs Git - rust.git/commitdiff
Do not silently truncate offsets for `read_at`/`write_at` on emscripten
authorTobias Bucher <tobiasbucher5991@gmail.com>
Sat, 12 May 2018 00:31:38 +0000 (02:31 +0200)
committerMark Simulacrum <mark.simulacrum@gmail.com>
Sat, 12 May 2018 14:39:05 +0000 (08:39 -0600)
Generate an IO error if the offset is out of bounds for the system call.

src/libstd/sys/unix/fd.rs

index 5dafc3251e7551e10f6c68573b479a16cd2efbf6..67546d06b4e55055d98b767d0cd1b6a4fec43b5a 100644 (file)
@@ -75,8 +75,15 @@ pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
         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 @@ pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
         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")))]