Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 95d75b6

Browse files
author
Ian Hays
authored
Merge pull request #7784 from ianhays/port_binaryreadwriter
Port to Release: Add some extra checks to BinaryReader/Writer buffers
2 parents c73eb10 + 26d4004 commit 95d75b6

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/mscorlib/src/System/IO/BinaryReader.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,24 @@ private int InternalReadChars(char[] buffer, int index, int count) {
372372
}
373373

374374
Contract.Assert(byteBuffer != null, "expected byteBuffer to be non-null");
375-
unsafe {
376-
fixed (byte* pBytes = byteBuffer)
377-
fixed (char* pChars = buffer) {
378-
charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false);
375+
376+
checked
377+
{
378+
if (position < 0 || numBytes < 0 || position > byteBuffer.Length - numBytes)
379+
{
380+
throw new ArgumentOutOfRangeException(nameof(numBytes));
381+
}
382+
if (index < 0 || charsRemaining < 0 || index > buffer.Length - charsRemaining)
383+
{
384+
throw new ArgumentOutOfRangeException(nameof(charsRemaining));
385+
}
386+
unsafe
387+
{
388+
fixed (byte* pBytes = byteBuffer)
389+
fixed (char* pChars = buffer)
390+
{
391+
charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, flush: false);
392+
}
379393
}
380394
}
381395

src/mscorlib/src/System/IO/BinaryWriter.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public unsafe virtual void Write(char ch) {
194194
Contract.Assert(_encoding.GetMaxByteCount(1) <= 16, "_encoding.GetMaxByteCount(1) <= 16)");
195195
int numBytes = 0;
196196
fixed(byte * pBytes = _buffer) {
197-
numBytes = _encoder.GetBytes(&ch, 1, pBytes, 16, true);
197+
numBytes = _encoder.GetBytes(&ch, 1, pBytes, _buffer.Length, flush: true);
198198
}
199199
OutStream.Write(_buffer, 0, numBytes);
200200
}
@@ -361,10 +361,11 @@ public unsafe virtual void Write(String value)
361361

362362
if (_largeByteBuffer == null) {
363363
_largeByteBuffer = new byte[LargeByteBufferSize];
364-
_maxChars = LargeByteBufferSize / _encoding.GetMaxByteCount(1);
364+
_maxChars = _largeByteBuffer.Length / _encoding.GetMaxByteCount(1);
365365
}
366366

367-
if (len <= LargeByteBufferSize) {
367+
if (len <= _largeByteBuffer.Length)
368+
{
368369
//Contract.Assert(len == _encoding.GetBytes(chars, 0, chars.Length, _largeByteBuffer, 0), "encoding's GetByteCount & GetBytes gave different answers! encoding type: "+_encoding.GetType().Name);
369370
_encoding.GetBytes(value, 0, value.Length, _largeByteBuffer, 0);
370371
OutStream.Write(_largeByteBuffer, 0, len);
@@ -383,14 +384,24 @@ public unsafe virtual void Write(String value)
383384
// Figure out how many chars to process this round.
384385
int charCount = (numLeft > _maxChars) ? _maxChars : numLeft;
385386
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+
}
389400
}
390401
}
391402
#if _DEBUG
392403
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!");
394405
#endif
395406
OutStream.Write(_largeByteBuffer, 0, byteLen);
396407
charStart += charCount;

0 commit comments

Comments
 (0)