diff --git a/samples/MvcSandbox/Startup.cs b/samples/MvcSandbox/Startup.cs index c6bf6981eb..659ebea209 100644 --- a/samples/MvcSandbox/Startup.cs +++ b/samples/MvcSandbox/Startup.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -34,6 +35,7 @@ namespace MvcSandbox { var host = new WebHostBuilder() .UseDefaultHostingConfiguration(args) + .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISPlatformHandlerUrl() .UseKestrel() .UseStartup() diff --git a/samples/MvcSandbox/project.json b/samples/MvcSandbox/project.json index 39101c9817..3e8e1cb540 100644 --- a/samples/MvcSandbox/project.json +++ b/samples/MvcSandbox/project.json @@ -18,6 +18,9 @@ "Microsoft.Extensions.Logging.Console": "1.0.0-*", "Microsoft.NETCore.Platforms": "1.0.1-*" }, + "content": [ + "Views" + ], "tools": { "dotnet-razor-tooling": "1.0.0-*" }, diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedBufferedTextWriter.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedBufferedTextWriter.cs index 93c26b01e9..c0eaac468a 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedBufferedTextWriter.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedBufferedTextWriter.cs @@ -83,11 +83,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public override void Write(char[] buffer) { + if (buffer == null) + { + return; + } + Write(buffer, 0, buffer.Length); } public override void Write(char[] buffer, int index, int count) { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + while (count > 0) { var page = GetCurrentPage(); @@ -110,6 +120,11 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public override void Write(string value) { + if (value == null) + { + return; + } + var index = 0; var count = value.Length; diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedBufferedStringWriterTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedBufferedTextWriterTest.cs similarity index 78% rename from test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedBufferedStringWriterTest.cs rename to test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedBufferedTextWriterTest.cs index f22f0d6ed1..f3681d7714 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedBufferedStringWriterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedBufferedTextWriterTest.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Buffers; using System.Collections.Generic; using System.IO; @@ -9,11 +10,11 @@ using Xunit; namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal { - public class PagedBufferedStringWriterTest + public class PagedBufferedTextWriterTest { private static readonly char[] Content; - static PagedBufferedStringWriterTest() + static PagedBufferedTextWriterTest() { Content = new char[4 * PagedBufferedTextWriter.PageSize]; for (var i = 0; i < Content.Length; i++) @@ -43,6 +44,24 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal Assert.Equal(Content, inner.ToString().ToCharArray()); } + [Fact] + public async Task Write_CharArray_Null() + { + // Arrange + var pool = new TestArrayPool(); + var inner = new StringWriter(); + + var writer = new PagedBufferedTextWriter(pool, inner); + + // Act + writer.Write((char[])null); + + await writer.FlushAsync(); + + // Assert + Assert.Empty(inner.ToString()); + } + [Fact] public async Task Write_CharArray() { @@ -82,6 +101,19 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal Assert.Equal(Content, inner.ToString().ToCharArray()); } + [Fact] + public void Write_CharArray_Bounded_Null() + { + // Arrange + var pool = new TestArrayPool(); + var inner = new StringWriter(); + + var writer = new PagedBufferedTextWriter(pool, inner); + + // Act & Assert + Assert.Throws("buffer", () => writer.Write(null, 0, 0)); + } + [Fact] public async Task Write_CharArray_Bounded() { @@ -109,6 +141,24 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal Assert.Equal(Content, inner.ToString().ToCharArray()); } + [Fact] + public async Task Write_String_Null() + { + // Arrange + var pool = new TestArrayPool(); + var inner = new StringWriter(); + + var writer = new PagedBufferedTextWriter(pool, inner); + + // Act + writer.Write((string)null); + + await writer.FlushAsync(); + + // Assert + Assert.Empty(inner.ToString()); + } + [Fact] public async Task Write_String() { @@ -158,7 +208,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal Assert.Equal(3, pool.Returned.Count); } - private class TestArrayPool: ArrayPool + private class TestArrayPool : ArrayPool { public IList Returned { get; } = new List();