Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public MemoryStream()
public MemoryStream(int capacity)
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
ArgumentOutOfRangeException.ThrowIfGreaterThan(capacity, MemStreamMaxLength);

_buffer = capacity != 0 ? new byte[capacity] : [];
_capacity = capacity;
Expand Down Expand Up @@ -547,15 +546,15 @@ private long SeekCore(long offset, int loc)
//
public override void SetLength(long value)
{
if (value < 0 || value > MemStreamMaxLength)
if (value < 0)
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();
Comment on lines 552 to +557
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.


int newLength = _origin + (int)value;
bool allocatedNewArray = EnsureCapacity(newLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void MemoryStream_Ctor_InvalidCapacities()
Assert.Throws<ArgumentOutOfRangeException>(() => new MemoryStream(-1));
if (PlatformDetection.IsNotIntMaxValueArrayIndexSupported)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new MemoryStream(int.MaxValue));
Assert.Throws<OutOfMemoryException>(() => new MemoryStream(int.MaxValue));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public void MemoryStream_CapacityBoundaryChecks()
ms.Capacity = MaxSupportedLength;
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);
Comment on lines 161 to +165
}
}

Expand Down
Loading