diff options
| author | Marcin Mielniczuk <marmistrz.dev@zoho.eu> | 2019-08-06 19:34:10 +0200 |
|---|---|---|
| committer | Marcin Mielniczuk <marmistrz.dev@zoho.eu> | 2019-08-06 19:35:54 +0200 |
| commit | 3cd9f3f6ab7d906e23d0ddc4bb0604b09966961f (patch) | |
| tree | 65afc1cb899c874fc4064b71cc2fab3720eb0c4b /src/libstd | |
| parent | 188ab5c976a5696ac710b7ba78849ef5dcf0235a (diff) | |
| download | rust-3cd9f3f6ab7d906e23d0ddc4bb0604b09966961f.tar.gz rust-3cd9f3f6ab7d906e23d0ddc4bb0604b09966961f.zip | |
Add an overflow check in truncate implementation for Unix.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/fs.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/unix/fs.rs | 12 |
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 cc1f0790d43..48e449d9c37 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -1,5 +1,6 @@ use crate::os::unix::prelude::*; +use crate::convert::TryInto; use crate::ffi::{CString, CStr, OsString, OsStr}; use crate::fmt; use crate::io::{self, Error, ErrorKind, SeekFrom, IoSlice, IoSliceMut}; @@ -554,9 +555,14 @@ 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(|_| ()); + { + 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> { |
