@@ -194,7 +194,7 @@ public unsafe virtual void Write(char ch) {
194
194
Contract . Assert ( _encoding . GetMaxByteCount ( 1 ) <= 16 , "_encoding.GetMaxByteCount(1) <= 16)" ) ;
195
195
int numBytes = 0 ;
196
196
fixed( byte * pBytes = _buffer ) {
197
- numBytes = _encoder . GetBytes ( & ch , 1 , pBytes , 16 , true ) ;
197
+ numBytes = _encoder . GetBytes ( & ch , 1 , pBytes , _buffer . Length , flush : true ) ;
198
198
}
199
199
OutStream . Write ( _buffer , 0 , numBytes ) ;
200
200
}
@@ -361,10 +361,11 @@ public unsafe virtual void Write(String value)
361
361
362
362
if ( _largeByteBuffer == null ) {
363
363
_largeByteBuffer = new byte [ LargeByteBufferSize ] ;
364
- _maxChars = LargeByteBufferSize / _encoding . GetMaxByteCount ( 1 ) ;
364
+ _maxChars = _largeByteBuffer . Length / _encoding . GetMaxByteCount ( 1 ) ;
365
365
}
366
366
367
- if ( len <= LargeByteBufferSize ) {
367
+ if ( len <= _largeByteBuffer . Length )
368
+ {
368
369
//Contract.Assert(len == _encoding.GetBytes(chars, 0, chars.Length, _largeByteBuffer, 0), "encoding's GetByteCount & GetBytes gave different answers! encoding type: "+_encoding.GetType().Name);
369
370
_encoding . GetBytes ( value , 0 , value . Length , _largeByteBuffer , 0 ) ;
370
371
OutStream. Write ( _largeByteBuffer , 0 , len ) ;
@@ -383,14 +384,24 @@ public unsafe virtual void Write(String value)
383
384
// Figure out how many chars to process this round.
384
385
int charCount = ( numLeft > _maxChars) ? _maxChars : numLeft;
385
386
int byteLen;
386
- fixed( char * pChars = value ) {
387
- fixed( byte * pBytes = _largeByteBuffer ) {
388
- byteLen = _encoder . GetBytes ( pChars + charStart , charCount , pBytes , LargeByteBufferSize , charCount = = numLeft ) ;
387
+
388
+ checked
389
+ {
390
+ if ( charStart < 0 || charCount < 0 || charStart > value . Length - charCount )
391
+ {
392
+ throw new ArgumentOutOfRangeException ( nameof ( charCount ) ) ;
393
+ }
394
+ fixed ( char * pChars = value )
395
+ {
396
+ fixed ( byte * pBytes = _largeByteBuffer )
397
+ {
398
+ byteLen = _encoder . GetBytes ( pChars + charStart , charCount , pBytes , _largeByteBuffer . Length , charCount == numLeft ) ;
399
+ }
389
400
}
390
401
}
391
402
#if _DEBUG
392
403
totalBytes += byteLen;
393
- Contract . Assert ( totalBytes < = len && byteLen <= LargeByteBufferSize , "BinaryWriter::Write(String) - More bytes encoded than expected!" ) ;
404
+ Contract. Assert ( totalBytes <= len && byteLen <= _largeByteBuffer . Length , "BinaryWriter::Write(String) - More bytes encoded than expected!" ) ;
394
405
#endif
395
406
OutStream. Write ( _largeByteBuffer , 0 , byteLen ) ;
396
407
charStart += charCount;
0 commit comments