diff --git a/src/Mvc/Mvc.ViewFeatures/src/Buffers/ViewBufferTextWriter.cs b/src/Mvc/Mvc.ViewFeatures/src/Buffers/ViewBufferTextWriter.cs index 6eca8ddcb40e..3a7380d87a18 100644 --- a/src/Mvc/Mvc.ViewFeatures/src/Buffers/ViewBufferTextWriter.cs +++ b/src/Mvc/Mvc.ViewFeatures/src/Buffers/ViewBufferTextWriter.cs @@ -110,16 +110,21 @@ public override void Write(char[] buffer, int index, int count) throw new ArgumentNullException(nameof(buffer)); } - if (index < 0 || index >= buffer.Length) + if (index < 0) { throw new ArgumentOutOfRangeException(nameof(index)); } - if (count < 0 || (buffer.Length - index < count)) + if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count)); } + if (buffer.Length - index < count) + { + throw new ArgumentOutOfRangeException(nameof(buffer.Length)); + } + Buffer.AppendHtml(new string(buffer, index, count)); } @@ -326,4 +331,4 @@ public override async Task FlushAsync() await _inner.FlushAsync(); } } -} \ No newline at end of file +} diff --git a/src/Mvc/Mvc.ViewFeatures/test/Buffers/ViewBufferTextWriterTest.cs b/src/Mvc/Mvc.ViewFeatures/test/Buffers/ViewBufferTextWriterTest.cs index 7a0c5bb46c2e..63583495a97f 100644 --- a/src/Mvc/Mvc.ViewFeatures/test/Buffers/ViewBufferTextWriterTest.cs +++ b/src/Mvc/Mvc.ViewFeatures/test/Buffers/ViewBufferTextWriterTest.cs @@ -124,6 +124,22 @@ public async Task WriteLines_WritesCharBuffer() Assert.Equal(new[] { newLine, newLine }, actual); } + [Fact] + public void Write_WritesEmptyCharBuffer() + { + // Arrange + var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4); + var writer = new ViewBufferTextWriter(buffer, Encoding.UTF8); + var charBuffer = new char[0]; + + // Act + writer.Write(charBuffer, 0, 0); + + // Assert + var actual = GetValues(buffer); + Assert.Equal(new[] { string.Empty }, actual); + } + [Fact] public async Task Write_WritesStringBuffer() { @@ -170,4 +186,4 @@ public override string ToString() } } } -} \ No newline at end of file +}