Skip to content

Fix integer overflow infinite loop in WriteStateInfoBase.EnsureSpaceInBuffer - #131860

Merged
rzikm merged 3 commits into
mainfrom
copilot/fix-infinite-loop-buffer-overflow
Aug 6, 2026
Merged

Fix integer overflow infinite loop in WriteStateInfoBase.EnsureSpaceInBuffer#131860
rzikm merged 3 commits into
mainfrom
copilot/fix-infinite-loop-buffer-overflow

Conversation

Copilot AI commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

EnsureSpaceInBuffer doubled the buffer size using an int, 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

  • Compute the doubled size using a long accumulator so the loop condition remains correct regardless of how large the buffer grows.
  • Cast back to int (via checked) only when allocating the new buffer, so an actual out-of-range allocation still fails fast with an OverflowException instead of silently wrapping.
private void EnsureSpaceInBuffer(int moreBytes)
{
    long newsize = Buffer.Length;
    while (_currentBufferUsed + moreBytes >= newsize)
    {
        newsize *= 2;
    }

    if (newsize > Buffer.Length)
    {
        byte[] tempBuffer = new byte[checked((int)newsize)];
        _buffer.CopyTo(tempBuffer, 0);
        _buffer = tempBuffer;
    }
}

Copilot AI review requested due to automatic review settings August 5, 2026 09:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.

@azure-pipelines

Copy link
Copy Markdown
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 review requested due to automatic review settings August 5, 2026 09:09
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
Copilot AI requested a review from rzikm August 5, 2026 09:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 + moreBytes is still evaluated as int, so it can overflow before the comparison (breaking the resizing logic), and the long-based doubling can overshoot past int/Array.MaxLength and then fail the allocation cast even when the required size would fit. Consider computing the required size in long, clamping growth to Array.MaxLength, and keeping the final allocation size as an int that 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.MaxLength behavior 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)

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@rzikm
rzikm marked this pull request as ready for review August 5, 2026 11:59
@rzikm
rzikm requested a review from a team August 5, 2026 12:00
@rzikm
rzikm requested a review from a team August 5, 2026 12:00
@azure-pipelines

Copy link
Copy Markdown
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.

@rzikm
rzikm enabled auto-merge (squash) August 5, 2026 12:47
Comment thread src/libraries/System.Net.Mail/src/System/Net/Mime/WriteStateInfoBase.cs Outdated
Comment thread src/libraries/System.Net.Mail/src/System/Net/Mime/WriteStateInfoBase.cs Outdated
Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 5, 2026 13:56
auto-merge was automatically disabled August 5, 2026 13:56

Head branch was pushed to by a user without write access

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

@rzikm
rzikm enabled auto-merge (squash) August 5, 2026 15:12
@MihaZupan

Copy link
Copy Markdown
Member

/ba-g Test failure is #130458

@rzikm
rzikm merged commit cdd58f8 into main Aug 6, 2026
84 of 86 checks passed
@rzikm
rzikm deleted the copilot/fix-infinite-loop-buffer-overflow branch August 6, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WriteStateInfoBase.EnsureSpaceInBuffer infinite loop via integer overflow

6 participants