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

Commit 26d4004

Browse files
author
Ian Hays
committed
PR feedback for BinaryReader/Writer buffers
1 parent 13ea22b commit 26d4004

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,22 +375,20 @@ private int InternalReadChars(char[] buffer, int index, int count) {
375375

376376
checked
377377
{
378-
if (position < 0 || numBytes < 0 || position + numBytes > byteBuffer.Length)
378+
if (position < 0 || numBytes < 0 || position > byteBuffer.Length - numBytes)
379379
{
380-
throw new ArgumentOutOfRangeException(nameof(byteCount));
380+
throw new ArgumentOutOfRangeException(nameof(numBytes));
381381
}
382-
if (index < 0 || charsRemaining < 0 || index + charsRemaining > buffer.Length)
382+
if (index < 0 || charsRemaining < 0 || index > buffer.Length - charsRemaining)
383383
{
384384
throw new ArgumentOutOfRangeException(nameof(charsRemaining));
385385
}
386386
unsafe
387387
{
388388
fixed (byte* pBytes = byteBuffer)
389+
fixed (char* pChars = buffer)
389390
{
390-
fixed (char* pChars = buffer)
391-
{
392-
charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false);
393-
}
391+
charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, flush: false);
394392
}
395393
}
396394
}

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

Lines changed: 2 additions & 2 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, _buffer.Length, true);
197+
numBytes = _encoder.GetBytes(&ch, 1, pBytes, _buffer.Length, flush: true);
198198
}
199199
OutStream.Write(_buffer, 0, numBytes);
200200
}
@@ -387,7 +387,7 @@ public unsafe virtual void Write(String value)
387387

388388
checked
389389
{
390-
if (charStart < 0 || charCount < 0 || charStart + charCount > value.Length)
390+
if (charStart < 0 || charCount < 0 || charStart > value.Length - charCount)
391391
{
392392
throw new ArgumentOutOfRangeException(nameof(charCount));
393393
}

0 commit comments

Comments
 (0)