-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ReadFile: avoid integer overflow when computing buffer capacity #29000
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
Changes from all commits
a661ed5
97d594c
8e27393
cf4a0b2
f2aa46d
1292506
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||||||||||||||||||||
| import { setSyntheticAllocationLimitForTesting } from "bun:internal-for-testing"; | ||||||||||||||||||||||||||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test"; | ||||||||||||||||||||||||||
| import { unlinkSync } from "fs"; | ||||||||||||||||||||||||||
| import { tempDirWithFiles } from "harness"; | ||||||||||||||||||||||||||
| import { isWindows, tempDirWithFiles } from "harness"; | ||||||||||||||||||||||||||
| import path from "path"; | ||||||||||||||||||||||||||
| describe("Memory", () => { | ||||||||||||||||||||||||||
| beforeAll(() => { | ||||||||||||||||||||||||||
|
|
@@ -142,3 +142,19 @@ describe("Bun.file", () => { | |||||||||||||||||||||||||
| expect(async () => await Bun.file(tmpFile).arrayBuffer()).not.toThrow(); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| describe.if(!isWindows)("Bun.file on character devices", () => { | ||||||||||||||||||||||||||
| test("slice() with a very large max length throws OOM without crashing", async () => { | ||||||||||||||||||||||||||
| // Regression test: reading a character device (e.g. /dev/null) with a | ||||||||||||||||||||||||||
| // max_length close to u52 max used to overflow `this.size + 16` in | ||||||||||||||||||||||||||
| // ReadFile.runAsyncWithFD and panic in debug builds. Also verifies that | ||||||||||||||||||||||||||
| // OOM from the buffer allocation is propagated to JS rather than | ||||||||||||||||||||||||||
| // silently returning an empty result. | ||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||
| async () => | ||||||||||||||||||||||||||
| await Bun.file("/dev/null") | ||||||||||||||||||||||||||
| .slice(0, 2 ** 52 - 15) | ||||||||||||||||||||||||||
| .bytes(), | ||||||||||||||||||||||||||
| ).toThrow(); | ||||||||||||||||||||||||||
|
Comment on lines
+153
to
+158
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Make this regression assertion OOM-specific.
Suggested assertion tightening- expect(
- async () =>
- await Bun.file("/dev/null")
- .slice(0, 2 ** 52 - 15)
- .bytes(),
- ).toThrow();
+ expect(
+ async () =>
+ await Bun.file("/dev/null")
+ .slice(0, 2 ** 52 - 15)
+ .bytes(),
+ ).toThrow(/out of memory|enomem|cannot allocate memory/i);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
Comment on lines
+151
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The new regression test at line 158 uses bare Extended reasoning...What the bug is: The test at The specific false-positive scenario: If the OOM propagation fix ( Why existing code doesn't prevent it: The Impact: The assertion specificity is inconsistent with the stated test comment. In practice the risk is low because How to fix: Replace Step-by-step proof of the weakness:
Addressing refutations: The refutation notes that the false-positive scenario (different exception + silent OOM simultaneously) is implausible for 🔬 also observed by coderabbitai |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.