Skip to content

Commit 4fcb9e6

Browse files
committed
Stop throwing exception when writing 0 length buffers (#32277)
* Stop throwing exception when writing 0 length buffers Fixes: #31299 * Leverage runtime checks & add test https://github.com/dotnet/runtime/blob/7938f9d9fadc3952d792329be602668bef124f23/src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs#L159
1 parent 18a6f0c commit 4fcb9e6

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/Mvc/Mvc.ViewFeatures/src/Buffers/ViewBufferTextWriter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,21 @@ public override void Write(char[] buffer, int index, int count)
110110
throw new ArgumentNullException(nameof(buffer));
111111
}
112112

113-
if (index < 0 || index >= buffer.Length)
113+
if (index < 0)
114114
{
115115
throw new ArgumentOutOfRangeException(nameof(index));
116116
}
117117

118-
if (count < 0 || (buffer.Length - index < count))
118+
if (count < 0)
119119
{
120120
throw new ArgumentOutOfRangeException(nameof(count));
121121
}
122122

123+
if (buffer.Length - index < count)
124+
{
125+
throw new ArgumentOutOfRangeException(nameof(buffer.Length));
126+
}
127+
123128
Buffer.AppendHtml(new string(buffer, index, count));
124129
}
125130

@@ -326,4 +331,4 @@ public override async Task FlushAsync()
326331
await _inner.FlushAsync();
327332
}
328333
}
329-
}
334+
}

src/Mvc/Mvc.ViewFeatures/test/Buffers/ViewBufferTextWriterTest.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ public async Task WriteLines_WritesCharBuffer()
124124
Assert.Equal<object>(new[] { newLine, newLine }, actual);
125125
}
126126

127+
[Fact]
128+
public void Write_WritesEmptyCharBuffer()
129+
{
130+
// Arrange
131+
var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
132+
var writer = new ViewBufferTextWriter(buffer, Encoding.UTF8);
133+
var charBuffer = new char[0];
134+
135+
// Act
136+
writer.Write(charBuffer, 0, 0);
137+
138+
// Assert
139+
var actual = GetValues(buffer);
140+
Assert.Equal<object>(new[] { string.Empty }, actual);
141+
}
142+
127143
[Fact]
128144
public async Task Write_WritesStringBuffer()
129145
{
@@ -170,4 +186,4 @@ public override string ToString()
170186
}
171187
}
172188
}
173-
}
189+
}

0 commit comments

Comments
 (0)