-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix integer overflow in BufferedStream.WriteToUnderlyingStreamAsync for large buffers #122944
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
Conversation
…nd add test Co-authored-by: stephentoub <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-system-io |
|
/azp run runtime-libraries-coreclr outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
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.
Pull request overview
This PR fixes an integer overflow bug in BufferedStream.WriteToUnderlyingStreamAsync that causes an OverflowException when writing buffers larger than ~1GB. The synchronous Write methods were previously fixed with (uint) casting, but the async path was overlooked. This change brings the async implementation in line with the synchronous methods.
Key Changes:
- Added
(uint)cast to the buffer size comparison inWriteToUnderlyingStreamAsyncto prevent signed integer overflow - Added test coverage for async writes with large buffers (>int.MaxValue/2)
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/IO/BufferedStream.cs | Applied (uint) cast to totalUserBytes + buffer.Length comparison in WriteToUnderlyingStreamAsync (line 1082) to match the existing fix in synchronous Write methods, preventing overflow for buffers larger than 1GB |
| src/libraries/System.Runtime/tests/System.IO.Tests/BufferedStream/BufferedStreamTests.cs | Added WriteAsyncFromMemory_InputSizeLargerThanHalfOfMaxInt_ShouldSuccess test to verify the async write path correctly handles large buffers without throwing OverflowException |
adamsitnik
left a comment
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.
LGTM!
src/libraries/System.Runtime/tests/System.IO.Tests/BufferedStream/BufferedStreamTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.Tests/BufferedStream/BufferedStreamTests.cs
Show resolved
Hide resolved
…eam/BufferedStreamTests.cs Co-authored-by: Adam Sitnik <[email protected]>
Description
BufferedStream.WriteToUnderlyingStreamAsyncthrowsOverflowExceptionwhen writing buffers larger than ~1GB due to signed integer overflow in the buffer size calculation. The synchronousWritemethods were already fixed with(uint)casting, but the async path was missed.Changes:
(uint)cast tototalUserBytes + buffer.Lengthcomparison inWriteToUnderlyingStreamAsyncto match the existing fix in synchronousWritemethods (lines 871, 942)WriteAsyncFromMemory_InputSizeLargerThanHalfOfMaxInt_ShouldSuccesstest to verify async path handles large buffers correctlyCustomer Impact
Applications that write large buffers (>1GB) to
BufferedStreamvia async methods fail withOverflowException. Common scenario:MemoryStream.CopyToAsynctoBufferedStreamwith large content.Regression
No, existing code path bug. Sync methods already fixed, async path oversight.
Testing
Risk
Low. Single-line arithmetic fix matching proven pattern already in use for synchronous methods. Overflow prevention is safer than overflow.
Original prompt
This pull request was created from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.