Skip to content

Commit a320810

Browse files
committed
fix(lib-std-fs): handle usize overflow in read & read_to_string
1 parent 175e043 commit a320810

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

library/std/src/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub struct DirBuilder {
304304
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
305305
fn inner(path: &Path) -> io::Result<Vec<u8>> {
306306
let mut file = File::open(path)?;
307-
let size = file.metadata().map(|m| m.len() as usize).ok();
307+
let size = file.metadata().map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)).ok();
308308
let mut bytes = Vec::try_with_capacity(size.unwrap_or(0))?;
309309
io::default_read_to_end(&mut file, &mut bytes, size)?;
310310
Ok(bytes)
@@ -346,7 +346,7 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
346346
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
347347
fn inner(path: &Path) -> io::Result<String> {
348348
let mut file = File::open(path)?;
349-
let size = file.metadata().map(|m| m.len() as usize).ok();
349+
let size = file.metadata().map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)).ok();
350350
let mut string = String::new();
351351
string.try_reserve_exact(size.unwrap_or(0))?;
352352
io::default_read_to_string(&mut file, &mut string, size)?;

0 commit comments

Comments
 (0)