Skip to content

Add an overflow check in truncate implementation for Unix. #63332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
12 changes: 9 additions & 3 deletions src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,15 @@ 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(|_| ());
{
Copy link
Contributor

@tesuji tesuji Aug 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import use crate::convert::TryInto; only to this block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

use crate::convert::TryInto;
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> {
Expand Down