Fix integer overflow in BufferedStream.WriteToUnderlyingStreamAsync for large buffers#122944
Merged
stephentoub merged 3 commits intomainfrom Jan 8, 2026
Merged
Conversation
…nd add test Co-authored-by: stephentoub <[email protected]>
Copilot
AI
changed the title
[WIP] Fix integer overflow in BufferedStream.WriteToUnderlyingStreamAsync
Fix integer overflow in BufferedStream.WriteToUnderlyingStreamAsync for large buffers
Jan 6, 2026
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Member
|
/azp run runtime-libraries-coreclr outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
stephentoub
approved these changes
Jan 6, 2026
Contributor
There was a problem hiding this comment.
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 |
This was referenced Jan 7, 2026
Open
adamsitnik
approved these changes
Jan 7, 2026
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]>
This was referenced Jan 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #117789
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.