Skip to content

fix(lib-std-fs): handle usize overflow in read_to_string #143462

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Rudxain
Copy link
Contributor

@Rudxain Rudxain commented Jul 4, 2025

I assume this is a non-breaking change, as there would be an OOM panic anyways. This patch ensures a fast-fail when there's not enough memory to load the file. This only changes behavior on platforms where usize is smaller than 64bits

@rustbot
Copy link
Collaborator

rustbot commented Jul 4, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 4, 2025
@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
##[endgroup]
[TIMING] core::build_steps::tool::ToolBuild { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu, tool: "tidy", path: "src/tools/tidy", mode: ToolBootstrap, source_type: InTree, extra_features: [], allow_features: "", cargo_args: [], artifact_kind: Binary } -- 9.130
[TIMING] core::build_steps::tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/library/std/src/fs.rs:346:
 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| usize::try_from(m.len()).unwrap_or(usize::MAX))
-            .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)?;
fmt: checked 6143 files
Build completed unsuccessfully in 0:00:44
  local time: Fri Jul  4 23:22:05 UTC 2025
  network time: Fri, 04 Jul 2025 23:22:05 GMT
##[error]Process completed with exit code 1.

@a1phyr
Copy link
Contributor

a1phyr commented Jul 5, 2025

Shouldn't this fail with ErrorKind::OutOfMemory directly instead?

Also modifications to fs::read_to_string should be made to fs::read too, as they are essentially the the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants