- 
                Notifications
    You must be signed in to change notification settings 
- 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
          
     Merged
      
      
    
      
        
          +31
        
        
          −11
        
        
          
        
      
    
  
  
     Merged
                    Update MemoryStream max capacity #119089
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      50f7abc
              
                cap memory stream max length and add unit test for max capacity
              
              
                alinpahontu2912 7d3947a
              
                Update src/libraries/System.Private.CoreLib/src/System/IO/MemoryStrea…
              
              
                alinpahontu2912 9995980
              
                fix failing tests
              
              
                alinpahontu2912 682275b
              
                Merge branch 'fix_update_mode_max_size' of https://github.com/alinpah…
              
              
                alinpahontu2912 ac77836
              
                Replace hardcoded value
              
              
                alinpahontu2912 dff632b
              
                update error message
              
              
                alinpahontu2912 0a7fe26
              
                Merge branch 'fix_update_mode_max_size' of https://github.com/alinpah…
              
              
                alinpahontu2912 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              | 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; | ||||||||
|  | ||||||||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  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.
  
    
  
    
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?