Stop throwing exception when writing 0 length buffers (#32277) (#32470)

## Description
Update guard logic to permit writing 0 length buffers. This can happen in certain globalized scenarios (ex. `¿` with Portugese) where the first char needs to be encoded.

Patch https://github.com/dotnet/aspnetcore/issues/31299 for 5.0 by cherry-picking the https://github.com/dotnet/aspnetcore/pull/32277 squash commit.


## Customer Impact
Writing 0-length char buffers leads to an ArgumentOutOfRangeException. This may occur in a globalization context with certain chars such as `¿`.

https://github.com/dotnet/aspnetcore/issues/31299#issuecomment-833078949

Also impacts Orchard: https://github.com/dotnet/aspnetcore/issues/31299#issue-842325059


## Regression?
- [ ] Yes
- [x] No

[If yes, specify the version the behavior has regressed from]

## Risk
- [ ] High
- [ ] Medium
- [x] Low

Low risk as we've just updated the logic slightly to permit 0 length buffers.

## Verification
- [x] Manual (required)
- [x] Automated

## Packaging changes reviewed?
- [ ] Yes
- [ ] No
- [x] N/A


Addresses https://github.com/dotnet/aspnetcore/issues/31299
This commit is contained in:
Tanay Parikh 2021-05-08 20:26:58 -07:00 committed by GitHub
parent e6998cb7bb
commit b124d508b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -110,16 +110,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
throw new ArgumentNullException(nameof(buffer)); throw new ArgumentNullException(nameof(buffer));
} }
if (index < 0 || index >= buffer.Length) if (index < 0)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
if (count < 0 || (buffer.Length - index < count)) if (count < 0)
{ {
throw new ArgumentOutOfRangeException(nameof(count)); throw new ArgumentOutOfRangeException(nameof(count));
} }
if (buffer.Length - index < count)
{
throw new ArgumentOutOfRangeException(nameof(buffer.Length));
}
Buffer.AppendHtml(new string(buffer, index, count)); Buffer.AppendHtml(new string(buffer, index, count));
} }
@ -326,4 +331,4 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
await _inner.FlushAsync(); await _inner.FlushAsync();
} }
} }
} }

View File

@ -124,6 +124,22 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
Assert.Equal<object>(new[] { newLine, newLine }, actual); Assert.Equal<object>(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<object>(new[] { string.Empty }, actual);
}
[Fact] [Fact]
public async Task Write_WritesStringBuffer() public async Task Write_WritesStringBuffer()
{ {
@ -170,4 +186,4 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
} }
} }
} }
} }