Revert MemoryStream breaking change#128366
Open
alinpahontu2912 wants to merge 1 commit into
Open
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Contributor
There was a problem hiding this comment.
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
MemStreamMaxLengthupper-bound argument check fromMemoryStream(int capacity)so oversize requests fail via the array allocation path. - Change
MemoryStream.SetLength(long)to throwOutOfMemoryExceptionwhen the requested length exceeds the supported maximum. - Update
System.IOtests to expectOutOfMemoryExceptionfor 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); |
jkotas
reviewed
May 19, 2026
| 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(); |
Member
There was a problem hiding this comment.
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.
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.
Revert Memorystream breaking change mentioned here: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/11/memorystream-max-capacity#affected-apis
Base discussion here: #123709
Fixes #123440