Skip to content
Merged
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
16 changes: 6 additions & 10 deletions src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public class MemoryStream : Stream

private CachedCompletedInt32Task _lastReadTask; // The last successful task returned from ReadAsync

private static int MemStreamMaxLength => Array.MaxLength;

public MemoryStream()
: this(0)
{
Expand All @@ -44,7 +42,6 @@ public MemoryStream()
public MemoryStream(int capacity)
Comment thread
alinpahontu2912 marked this conversation as resolved.
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
ArgumentOutOfRangeException.ThrowIfGreaterThan(capacity, MemStreamMaxLength);

_buffer = capacity != 0 ? new byte[capacity] : [];
_capacity = capacity;
Expand Down Expand Up @@ -311,7 +308,7 @@ public override long Position
ArgumentOutOfRangeException.ThrowIfNegative(value);
EnsureNotClosed();

if (value > MemStreamMaxLength - _origin)
if (value > int.MaxValue - _origin)
Comment thread
alinpahontu2912 marked this conversation as resolved.
Comment thread
alinpahontu2912 marked this conversation as resolved.
throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength));
Comment thread
jkotas marked this conversation as resolved.
_position = _origin + (int)value;
}
Expand Down Expand Up @@ -524,7 +521,7 @@ public override long Seek(long offset, SeekOrigin loc)

private long SeekCore(long offset, int loc)
{
if (offset > MemStreamMaxLength - loc)
if (offset > int.MaxValue - loc)
Comment thread
alinpahontu2912 marked this conversation as resolved.
throw new ArgumentOutOfRangeException(nameof(offset), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength));
int tempPosition = unchecked(loc + (int)offset);
if (unchecked(loc + offset) < _origin || tempPosition < _origin)
Expand All @@ -537,24 +534,23 @@ private long SeekCore(long offset, int loc)

// Sets the length of the stream to a given value. The new
// value must be nonnegative and less than the space remaining in
// the array, MemStreamMaxLength - origin
// the array, int.MaxValue - origin
// Origin is 0 in all cases other than a MemoryStream created on
// top of an existing array and a specific starting offset was passed
// into the MemoryStream constructor. The upper bounds prevents any
// situations where a stream may be created on top of an array then
// the stream is made longer than the maximum possible length of the
// array (MemStreamMaxLength).
// array (int.MaxValue).
//
public override void SetLength(long value)
{
if (value < 0 || value > MemStreamMaxLength)
if (value < 0 || value > 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))
if (value > (int.MaxValue - _origin))
throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength));
Comment thread
alinpahontu2912 marked this conversation as resolved.

int newLength = _origin + (int)value;
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 @@ -105,8 +105,8 @@ public void MemoryStream_SeekOverflow_Throws(SeekMode mode, int bufferSize, int
byte[] buffer = new byte[bufferSize];
using (MemoryStream ms = new MemoryStream(buffer, origin, buffer.Length - origin, true))
{
Seek(mode, ms, Array.MaxLength - origin);
Assert.Throws<ArgumentOutOfRangeException>(() => Seek(mode, ms, (long)Array.MaxLength - origin + 1));
Seek(mode, ms, int.MaxValue - origin);
Assert.Throws<ArgumentOutOfRangeException>(() => Seek(mode, ms, (long)int.MaxValue - origin + 1));
Assert.ThrowsAny<Exception>(() => Seek(mode, ms, long.MinValue + 1));
Assert.ThrowsAny<Exception>(() => Seek(mode, ms, long.MaxValue - 1));
}
Expand Down 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 thread
alinpahontu2912 marked this conversation as resolved.
}
}

Expand Down
Loading