Skip to content

Commit c659b1b

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 5e53619 + 3cd9f3f commit c659b1b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/libstd/fs.rs

+2
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

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::os::unix::prelude::*;
22

3+
use crate::convert::TryInto;
34
use crate::ffi::{CString, CStr, OsString, OsStr};
45
use crate::fmt;
56
use crate::io::{self, Error, ErrorKind, SeekFrom, IoSlice, IoSliceMut};
@@ -554,9 +555,14 @@ impl File {
554555
return crate::sys::android::ftruncate64(self.0.raw(), size);
555556

556557
#[cfg(not(target_os = "android"))]
557-
return cvt_r(|| unsafe {
558-
ftruncate64(self.0.raw(), size as off64_t)
559-
}).map(|_| ());
558+
{
559+
let size: off64_t = size
560+
.try_into()
561+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
562+
cvt_r(|| unsafe {
563+
ftruncate64(self.0.raw(), size)
564+
}).map(|_| ())
565+
}
560566
}
561567

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

0 commit comments

Comments
 (0)