Add an overflow check in truncate implementation for Unix.
This commit is contained in:
parent
188ab5c976
commit
3cd9f3f6ab
2 changed files with 11 additions and 3 deletions
|
@ -468,6 +468,8 @@ impl File {
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// This function will return an error if the file is not opened for writing.
|
/// 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
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::os::unix::prelude::*;
|
use crate::os::unix::prelude::*;
|
||||||
|
|
||||||
|
use crate::convert::TryInto;
|
||||||
use crate::ffi::{CString, CStr, OsString, OsStr};
|
use crate::ffi::{CString, CStr, OsString, OsStr};
|
||||||
use crate::fmt;
|
use crate::fmt;
|
||||||
use crate::io::{self, Error, ErrorKind, SeekFrom, IoSlice, IoSliceMut};
|
use crate::io::{self, Error, ErrorKind, SeekFrom, IoSlice, IoSliceMut};
|
||||||
|
@ -554,9 +555,14 @@ impl File {
|
||||||
return crate::sys::android::ftruncate64(self.0.raw(), size);
|
return crate::sys::android::ftruncate64(self.0.raw(), size);
|
||||||
|
|
||||||
#[cfg(not(target_os = "android"))]
|
#[cfg(not(target_os = "android"))]
|
||||||
return cvt_r(|| unsafe {
|
{
|
||||||
ftruncate64(self.0.raw(), size as off64_t)
|
let size: off64_t = size
|
||||||
}).map(|_| ());
|
.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> {
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
|
Loading…
Reference in a new issue