diff options
| author | Ricardo Fernández Serrata <76864299+Rudxain@users.noreply.github.com> | 2025-07-04 19:10:39 -0400 |
|---|---|---|
| committer | Ricardo Fernández Serrata <76864299+Rudxain@users.noreply.github.com> | 2025-07-05 22:10:10 +0000 |
| commit | a3208108b0b2b98004dac023aaeb794c1ec9149a (patch) | |
| tree | 78bb5e42e41412f03a0bfb1496800269e3542c3a | |
| parent | 175e04331be56c5b4bdf77478434b1a5e0556770 (diff) | |
fix(lib-std-fs): handle `usize` overflow in `read` & `read_to_string`
| -rw-r--r-- | library/std/src/fs.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 72ad7c244ee..9fbfd92eaf8 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -304,7 +304,7 @@ pub struct DirBuilder { pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { fn inner(path: &Path) -> io::Result<Vec<u8>> { let mut file = File::open(path)?; - let size = file.metadata().map(|m| m.len() as usize).ok(); + let size = file.metadata().map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)).ok(); let mut bytes = Vec::try_with_capacity(size.unwrap_or(0))?; io::default_read_to_end(&mut file, &mut bytes, size)?; Ok(bytes) @@ -346,7 +346,7 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> { fn inner(path: &Path) -> io::Result<String> { let mut file = File::open(path)?; - let size = file.metadata().map(|m| m.len() as usize).ok(); + let size = file.metadata().map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)).ok(); let mut string = String::new(); string.try_reserve_exact(size.unwrap_or(0))?; io::default_read_to_string(&mut file, &mut string, size)?; |
