Add an overflow check in truncate implementation for Unix.

This commit is contained in:
Marcin Mielniczuk 2019-08-06 19:34:10 +02:00
parent 188ab5c976
commit 3cd9f3f6ab
No known key found for this signature in database
GPG key ID: 2CF0CE66660B8CC9
2 changed files with 11 additions and 3 deletions

View file

@ -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
///

View file

@ -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> {