From e807e3de05ef0434fec600f3cda8ca8461d3b947 Mon Sep 17 00:00:00 2001 From: Ian Hays Date: Mon, 24 Oct 2016 13:26:42 -0700 Subject: [PATCH] PR feedback for BinaryReader/Writer buffers --- src/mscorlib/src/System/IO/BinaryReader.cs | 12 +++++------- src/mscorlib/src/System/IO/BinaryWriter.cs | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/mscorlib/src/System/IO/BinaryReader.cs b/src/mscorlib/src/System/IO/BinaryReader.cs index 32e26c0..8accf0b 100644 --- a/src/mscorlib/src/System/IO/BinaryReader.cs +++ b/src/mscorlib/src/System/IO/BinaryReader.cs @@ -375,22 +375,20 @@ namespace System.IO { checked { - if (position < 0 || numBytes < 0 || position + numBytes > byteBuffer.Length) + if (position < 0 || numBytes < 0 || position > byteBuffer.Length - numBytes) { - throw new ArgumentOutOfRangeException(nameof(byteCount)); + throw new ArgumentOutOfRangeException(nameof(numBytes)); } - if (index < 0 || charsRemaining < 0 || index + charsRemaining > buffer.Length) + if (index < 0 || charsRemaining < 0 || index > buffer.Length - charsRemaining) { throw new ArgumentOutOfRangeException(nameof(charsRemaining)); } unsafe { fixed (byte* pBytes = byteBuffer) + fixed (char* pChars = buffer) { - fixed (char* pChars = buffer) - { - charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false); - } + charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, flush: false); } } } diff --git a/src/mscorlib/src/System/IO/BinaryWriter.cs b/src/mscorlib/src/System/IO/BinaryWriter.cs index 03fe51f..c775cbc 100644 --- a/src/mscorlib/src/System/IO/BinaryWriter.cs +++ b/src/mscorlib/src/System/IO/BinaryWriter.cs @@ -194,7 +194,7 @@ namespace System.IO { Contract.Assert(_encoding.GetMaxByteCount(1) <= 16, "_encoding.GetMaxByteCount(1) <= 16)"); int numBytes = 0; fixed(byte * pBytes = _buffer) { - numBytes = _encoder.GetBytes(&ch, 1, pBytes, _buffer.Length, true); + numBytes = _encoder.GetBytes(&ch, 1, pBytes, _buffer.Length, flush: true); } OutStream.Write(_buffer, 0, numBytes); } @@ -387,7 +387,7 @@ namespace System.IO { checked { - if (charStart < 0 || charCount < 0 || charStart + charCount > value.Length) + if (charStart < 0 || charCount < 0 || charStart > value.Length - charCount) { throw new ArgumentOutOfRangeException(nameof(charCount)); } -- 2.7.4