-
Couldn't load subscription status.
- Fork 5.2k
Update MemoryStream max capacity #119089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update MemoryStream max capacity #119089
Changes from all commits
50f7abc
7d3947a
9995980
682275b
ac77836
dff632b
0a7fe26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||
|
|
||||||||
| using System.Buffers; | ||||||||
|
|
@@ -34,7 +34,7 @@ public class MemoryStream : Stream | |||||||
|
|
||||||||
| private CachedCompletedInt32Task _lastReadTask; // The last successful task returned from ReadAsync | ||||||||
|
|
||||||||
| private const int MemStreamMaxLength = int.MaxValue; | ||||||||
| private static int MemStreamMaxLength => Array.MaxLength; | ||||||||
|
|
||||||||
| public MemoryStream() | ||||||||
| : this(0) | ||||||||
|
|
@@ -536,24 +536,24 @@ private long SeekCore(long offset, int loc) | |||||||
|
|
||||||||
| // Sets the length of the stream to a given value. The new | ||||||||
alinpahontu2912 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| // value must be nonnegative and less than the space remaining in | ||||||||
| // the array, int.MaxValue - origin | ||||||||
| // the array, MemStreamMaxLength - 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 (int.MaxValue). | ||||||||
| // array (MemStreamMaxLength). | ||||||||
| // | ||||||||
| public override void SetLength(long value) | ||||||||
| { | ||||||||
| if (value < 0 || value > int.MaxValue) | ||||||||
| if (value < 0 || value > MemStreamMaxLength) | ||||||||
| throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_StreamLength); | ||||||||
alinpahontu2912 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| EnsureWriteable(); | ||||||||
|
|
||||||||
| // Origin wasn't publicly exposed above. | ||||||||
| Debug.Assert(MemStreamMaxLength == int.MaxValue); // Check parameter validation logic in this method if this fails. | ||||||||
| if (value > (int.MaxValue - _origin)) | ||||||||
| Debug.Assert(MemStreamMaxLength == 0x7FFFFFC7); // Check parameter validation logic in this method if this fails. | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| if (value > (MemStreamMaxLength - _origin)) | ||||||||
| throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_StreamLength); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we will also need to update the ctor that takes capacity because it will keep throwing OOM while we throw OutOfRange everywhere else. |
||||||||
|
|
||||||||
| int newLength = _origin + (int)value; | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's been efforts to not hardcode 0x7FFFFFC7 and instead use Array.MaxLength. Can we pass
$"0x{Array.MaxLength:X}"using SR.Format?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this do something like the following instead, to help clarify that the value may change over time:
Perhaps we could just explicitly use the text
Array.MaxLengthinstead?