-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix integer overflow in ReadFile buffer pre-allocation #29207
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
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 |
|---|---|---|
|
|
@@ -384,7 +384,7 @@ pub const ReadFile = struct { | |
|
|
||
| // add an extra 16 bytes to the buffer to avoid having to resize it for trailing extra data | ||
| if (!this.could_block or (this.size > 0 and this.size != Blob.max_size)) | ||
| this.buffer = std.ArrayListUnmanaged(u8).initCapacity(bun.default_allocator, this.size + 16) catch |err| { | ||
| this.buffer = std.ArrayListUnmanaged(u8).initCapacity(bun.default_allocator, this.size +| 16) catch |err| { | ||
| this.errno = err; | ||
| this.onFinish(); | ||
| return; | ||
|
Comment on lines
384
to
390
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. 🔴 In Extended reasoning...What the bug is and how it manifests In this.errno = err;
this.onFinish();
return;It sets const system_error = this.system_error;
// ...
if (system_error) |err| {
cb(cb_ctx, ReadFileResultType{ .err = err });
return;
}
cb(cb_ctx, .{ .result = .{ .buf = buf, .total_size = total_size, .is_temporary = true } });Because The specific code path that triggers it
Why existing code doesn't prevent it The What the impact would be A caller reading a large How to fix it Add the missing line inside the catch block: this.buffer = std.ArrayListUnmanaged(u8).initCapacity(bun.default_allocator, this.size +| 16) catch |err| {
this.errno = err;
this.system_error = bun.sys.Error.fromCode(bun.sys.E.NOMEM, .read).toSystemError(); // <-- add this
this.onFinish();
return;
};Step-by-step proof
|
||
|
|
@@ -698,7 +698,7 @@ pub const ReadFileUV = struct { | |
| return; | ||
| } | ||
| // add an extra 16 bytes to the buffer to avoid having to resize it for trailing extra data | ||
| this.buffer.ensureTotalCapacityPrecise(this.byte_store.allocator, @min(this.size + 16, @as(usize, std.math.maxInt(bun.windows.ULONG)))) catch { | ||
| this.buffer.ensureTotalCapacityPrecise(this.byte_store.allocator, @min(this.size +| 16, @as(usize, std.math.maxInt(bun.windows.ULONG)))) catch { | ||
| this.errno = error.OutOfMemory; | ||
| this.system_error = bun.sys.Error.fromCode(bun.sys.E.NOMEM, .read).toSystemError(); | ||
| this.onFinish(); | ||
|
|
||
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.
Propagate OOM to
system_erroron the non-Windows path.At Line 387, the catch path sets
this.errnobut notthis.system_error;then()rejects only whensystem_errorexists, so allocation failure can surface as a successful empty read instead of an error.Suggested fix
🤖 Prompt for AI Agents