Skip to content

Commit 76938ab

Browse files
authored
Rollup merge of rust-lang#63332 - marmistrz:truncate, r=alexcrichton
Add an overflow check in truncate implementation for Unix. Closes rust-lang#63326. cc @alexcrichton
2 parents 8fe2d9c + 3adbf63 commit 76938ab

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/libstd/fs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ impl File {
468468
/// # Errors
469469
///
470470
/// This function will return an error if the file is not opened for writing.
471+
/// Also, std::io::ErrorKind::InvalidInput will be returned if the desired
472+
/// length would cause an overflow due to the implementation specifics.
471473
///
472474
/// # Examples
473475
///

src/libstd/sys/unix/fs.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,15 @@ impl File {
557557
return crate::sys::android::ftruncate64(self.0.raw(), size);
558558

559559
#[cfg(not(target_os = "android"))]
560-
return cvt_r(|| unsafe {
561-
ftruncate64(self.0.raw(), size as off64_t)
562-
}).map(|_| ());
560+
{
561+
use crate::convert::TryInto;
562+
let size: off64_t = size
563+
.try_into()
564+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
565+
cvt_r(|| unsafe {
566+
ftruncate64(self.0.raw(), size)
567+
}).map(|_| ())
568+
}
563569
}
564570

565571
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {

0 commit comments

Comments
 (0)