about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-08 16:33:37 +0200
committerGitHub <noreply@github.com>2019-08-08 16:33:37 +0200
commit76938abba13de41dd9da498226d6d549a29a3e47 (patch)
treed6cfffd61f4816cb8856397c68a77d1e9d23a782 /src/libstd
parent8fe2d9c4e9693c5988bcebe81c9d1e046c9e791a (diff)
parent3adbf63b119d26edf1997e974d0727791d6f4060 (diff)
downloadrust-76938abba13de41dd9da498226d6d549a29a3e47.tar.gz
rust-76938abba13de41dd9da498226d6d549a29a3e47.zip
Rollup merge of #63332 - marmistrz:truncate, r=alexcrichton
Add an overflow check in truncate implementation for Unix.

Closes #63326.
cc @alexcrichton
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/fs.rs2
-rw-r--r--src/libstd/sys/unix/fs.rs12
2 files changed, 11 insertions, 3 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index f7c32a5c20d..5f76875bd66 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -468,6 +468,8 @@ impl File {
     /// # Errors
     ///
     /// This function will return an error if the file is not opened for writing.
+    /// Also, std::io::ErrorKind::InvalidInput will be returned if the desired
+    /// length would cause an overflow due to the implementation specifics.
     ///
     /// # Examples
     ///
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs
index 822feacea62..3b1eb86b84f 100644
--- a/src/libstd/sys/unix/fs.rs
+++ b/src/libstd/sys/unix/fs.rs
@@ -557,9 +557,15 @@ impl File {
         return crate::sys::android::ftruncate64(self.0.raw(), size);
 
         #[cfg(not(target_os = "android"))]
-        return cvt_r(|| unsafe {
-            ftruncate64(self.0.raw(), size as off64_t)
-        }).map(|_| ());
+        {
+            use crate::convert::TryInto;
+            let size: off64_t = size
+                .try_into()
+                .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
+            cvt_r(|| unsafe {
+                ftruncate64(self.0.raw(), size)
+            }).map(|_| ())
+        }
     }
 
     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {