Skip to content

Revert MemoryStream breaking change#128366

Open
alinpahontu2912 wants to merge 1 commit into
dotnet:mainfrom
alinpahontu2912:revert_memstream_changes
Open

Revert MemoryStream breaking change#128366
alinpahontu2912 wants to merge 1 commit into
dotnet:mainfrom
alinpahontu2912:revert_memstream_changes

Conversation

@alinpahontu2912
Copy link
Copy Markdown
Member

@alinpahontu2912 alinpahontu2912 commented May 19, 2026

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Copilot AI left a 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 changes MemoryStream to throw OutOfMemoryException (instead of ArgumentOutOfRangeException) when exceeding the maximum supported capacity/length, and updates related tests to match.

Changes:

  • Remove the explicit MemStreamMaxLength upper-bound argument check from MemoryStream(int capacity) so oversize requests fail via the array allocation path.
  • Change MemoryStream.SetLength(long) to throw OutOfMemoryException when the requested length exceeds the supported maximum.
  • Update System.IO tests to expect OutOfMemoryException for the relevant boundary cases.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Runtime/tests/System.IO.Tests/MemoryStream/MemoryStreamTests.cs Updates capacity boundary test expectations to OutOfMemoryException (currently still CI-skipped due to large allocations).
src/libraries/System.Runtime/tests/System.IO.Tests/MemoryStream/MemoryStream.ConstructorTests.cs Updates invalid-capacity constructor test to expect OutOfMemoryException for int.MaxValue on platforms where it’s not supported.
src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs Removes upper-bound ctor validation and changes SetLength to throw OutOfMemoryException when length exceeds the maximum.

Comment on lines 552 to +557
EnsureWriteable();

// Origin wasn't publicly exposed above.
Debug.Assert(MemStreamMaxLength == Array.MaxLength); // Check parameter validation logic in this method if this fails.
if (value > (MemStreamMaxLength - _origin))
throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength));
throw new OutOfMemoryException();
Comment on lines 161 to +165
Assert.Equal(MaxSupportedLength, ms.Capacity);

Assert.Throws<ArgumentOutOfRangeException>(() => ms.Capacity = MaxSupportedLength + 1);
Assert.Throws<OutOfMemoryException>(() => ms.Capacity = MaxSupportedLength + 1);

Assert.Throws<ArgumentOutOfRangeException>(() => ms.Capacity = int.MaxValue);
Assert.Throws<OutOfMemoryException>(() => ms.Capacity = int.MaxValue);
throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength));

EnsureWriteable();

// Origin wasn't publicly exposed above.
Debug.Assert(MemStreamMaxLength == Array.MaxLength); // Check parameter validation logic in this method if this fails.
if (value > (MemStreamMaxLength - _origin))
throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength));
throw new OutOfMemoryException();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not going back to .NET 10 behavior.

var ms = new MemoryStream();
ms.SetLength(3 * (long)Int32.MaxValue);

throws ArgumentOutOfRangeException in .NET 10, but it is going to throw OutOfMemoryException with this change.

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.

3 participants