@@ -213,28 +213,37 @@ private bool TryEnsureBytes(int count)
213213 {
214214 if ( _stream == null )
215215 return false ;
216- DiagnosticUtility . DebugAssert ( _offset <= int . MaxValue - count , "" ) ;
217- int newOffsetMax = _offset + count ;
218- if ( newOffsetMax < _offsetMax )
219- return true ;
220- DiagnosticUtility . DebugAssert ( newOffsetMax <= _windowOffsetMax , "" ) ;
221- if ( newOffsetMax > _buffer . Length )
222- {
223- byte [ ] newBuffer = new byte [ Math . Max ( newOffsetMax , _buffer . Length * 2 ) ] ;
224- System . Buffer . BlockCopy ( _buffer , 0 , newBuffer , 0 , _offsetMax ) ;
225- _buffer = newBuffer ;
226- _streamBuffer = newBuffer ;
227- }
228- int needed = newOffsetMax - _offsetMax ;
229- while ( needed > 0 )
216+
217+ // The data could be coming from an untrusted source, so we use a standard
218+ // "multiply by 2" growth algorithm to avoid overly large memory utilization.
219+ // Constant value of 256 comes from MemoryStream implementation.
220+
221+ do
230222 {
231- int actual = _stream . Read ( _buffer , _offsetMax , needed ) ;
232- if ( actual == 0 )
233- return false ;
234- _offsetMax += actual ;
235- needed -= actual ;
236- }
237- return true ;
223+ DiagnosticUtility . DebugAssert ( _offset <= int . MaxValue - count , "" ) ;
224+ int newOffsetMax = _offset + count ;
225+ if ( newOffsetMax <= _offsetMax )
226+ return true ;
227+ DiagnosticUtility . DebugAssert ( newOffsetMax <= _windowOffsetMax , "" ) ;
228+ if ( newOffsetMax > _buffer . Length )
229+ {
230+ byte [ ] newBuffer = new byte [ Math . Max ( 256 , _buffer . Length * 2 ) ] ;
231+ System . Buffer . BlockCopy ( _buffer , 0 , newBuffer , 0 , _offsetMax ) ;
232+ newOffsetMax = Math . Min ( newOffsetMax , newBuffer . Length ) ;
233+ _buffer = newBuffer ;
234+ _streamBuffer = newBuffer ;
235+ }
236+ int needed = newOffsetMax - _offsetMax ;
237+ DiagnosticUtility . DebugAssert ( needed > 0 , "" ) ;
238+ do
239+ {
240+ int actual = _stream . Read ( _buffer , _offsetMax , needed ) ;
241+ if ( actual == 0 )
242+ return false ;
243+ _offsetMax += actual ;
244+ needed -= actual ;
245+ } while ( needed > 0 ) ;
246+ } while ( true ) ;
238247 }
239248
240249 public void Advance ( int count )
0 commit comments