Open
Fix integer overflow infinite loop in WriteStateInfoBase.EnsureSpaceInBuffer#131860
Conversation
|
Azure Pipelines: 16 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
…nBuffer Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix infinite loop in EnsureSpaceInBuffer due to integer overflow
Fix integer overflow infinite loop in WriteStateInfoBase.EnsureSpaceInBuffer
Aug 5, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/libraries/System.Net.Mail/src/System/Net/Mime/WriteStateInfoBase.cs:61
_currentBufferUsed + moreBytesis still evaluated asint, so it can overflow before the comparison (breaking the resizing logic), and thelong-based doubling can overshoot pastint/Array.MaxLengthand then fail the allocation cast even when the required size would fit. Consider computing the required size inlong, clamping growth toArray.MaxLength, and keeping the final allocation size as anintthat is guaranteed to be > required and <=Array.MaxLength(also handle the edge case of a 0-length buffer, which would otherwise loop forever).
long newsize = Buffer.Length;
while (_currentBufferUsed + moreBytes >= newsize)
{
newsize *= 2;
}
src/libraries/System.Net.Mail/src/System/Net/Mime/WriteStateInfoBase.cs:58
- This change targets an extreme-size overflow/infinite-loop scenario, but the existing unit tests only cover small-buffer growth. Adding a targeted test for the overflow/near-
Array.MaxLengthbehavior would help prevent regressions; since this code is hard to exercise without huge allocations, consider factoring the growth calculation into a small internal helper that can be unit-tested with large numeric inputs.
private void EnsureSpaceInBuffer(int moreBytes)
{
long newsize = Buffer.Length;
while (_currentBufferUsed + moreBytes >= newsize)
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
rzikm
marked this pull request as ready for review
August 5, 2026 11:59
rzikm
approved these changes
Aug 5, 2026
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
wfurt
approved these changes
Aug 5, 2026
rzikm
enabled auto-merge (squash)
August 5, 2026 12:47
jkotas
reviewed
Aug 5, 2026
jkotas
reviewed
Aug 5, 2026
Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
auto-merge was automatically disabled
August 5, 2026 13:56
Head branch was pushed to by a user without write access
jkotas
approved these changes
Aug 5, 2026
rzikm
enabled auto-merge (squash)
August 5, 2026 15:12
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
EnsureSpaceInBufferdoubled the buffer size using anint, which could overflow to a negative value for buffers approaching ~1GB, turning the growth loop's termination condition (positive >= negative) permanently true and hanging the thread.Changes
longaccumulator so the loop condition remains correct regardless of how large the buffer grows.int(viachecked) only when allocating the new buffer, so an actual out-of-range allocation still fails fast with anOverflowExceptioninstead of silently wrapping.