Skip to content

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

This comment has been minimized.

@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.

@Rudxain Rudxain force-pushed the read_to_string_usize branch 2 times, most recently from c72c762 to c03b767 Compare July 5, 2025 22:06
@Rudxain Rudxain changed the title fix(lib-std-fs): handle usize overflow in read_to_string fix(lib-std-fs): handle usize overflow in read* Jul 5, 2025
@Rudxain Rudxain force-pushed the read_to_string_usize branch from c03b767 to a320810 Compare July 5, 2025 22:11
@Enselic
Copy link
Member

Enselic commented Aug 29, 2025

Hi, is there any particular reason you have not added regression tests for this fix? Regression tests are useful for many reasons, for example:

  • Makes it easier to understand the use case
  • Makes it easier to evaluate the impact of existing code
  • Makes it easier to do code review
  • Avoid that the fix accidentally disappears in the future

@Rudxain
Copy link
Contributor Author

Rudxain commented Aug 29, 2025

Thanks for the reminder! I do agree that tests would be a good idea. I'll have to re-read the contrib-docs, because IDK how to do it properly

@joboet
Copy link
Member

joboet commented Aug 30, 2025

I don't think a test here is necessary. The previous behaviour wasn't a bug, just a suboptimal implementation. And you'd probably have to track the amount of read syscalls in order to test this, which is way to overkill for such a simple fix.

@bors r+
r? joboet

@bors
Copy link
Collaborator

bors commented Aug 30, 2025

📌 Commit a320810 has been approved by joboet

It is now in the queue for this repository.

@rustbot rustbot assigned joboet and unassigned thomcc Aug 30, 2025
@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 30, 2025
bors added a commit that referenced this pull request Aug 30, 2025
Rollup of 5 pull requests

Successful merges:

 - #143462 (fix(lib-std-fs): handle `usize` overflow in `read*`)
 - #144651 (Implementation: `#[feature(nonpoison_condvar)]`)
 - #145465 (Stabilize `array_repeat` feature)
 - #145776 (Optimize `.ilog({2,10})` to `.ilog{2,10}()`)
 - #145969 (Add Duration::from_nanos_u128)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6421031 into rust-lang:master Aug 30, 2025
11 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 30, 2025
rust-timer added a commit that referenced this pull request Aug 30, 2025
Rollup merge of #143462 - Rudxain:read_to_string_usize, r=joboet

fix(lib-std-fs): handle `usize` overflow in `read*`

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
@bors
Copy link
Collaborator

bors commented Aug 30, 2025

⌛ Testing commit a320810 with merge e95db59...

@Rudxain Rudxain deleted the read_to_string_usize branch September 1, 2025 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

8 participants