From 760c8f38678118734399c58c2dac981ea6e47046 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 14 Sep 2016 18:54:59 -0700 Subject: [PATCH] Ensure Append writes to the right page after Clear --- .../Internal/PagedCharBuffer.cs | 3 ++- .../Internal/PagedCharBufferTest.cs | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedCharBuffer.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedCharBuffer.cs index bb6509790b..7d13c34768 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedCharBuffer.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedCharBuffer.cs @@ -115,12 +115,13 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal } _charIndex = 0; + CurrentPage = Pages.Count > 0 ? Pages[0] : null; } private char[] GetCurrentPage() { if (CurrentPage == null || - _charIndex == CurrentPage.Length) + _charIndex == PageSize) { CurrentPage = NewPage(); _charIndex = 0; diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedCharBufferTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedCharBufferTest.cs index f8b9f7c7cc..ba43cd8ceb 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedCharBufferTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedCharBufferTest.cs @@ -206,5 +206,29 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal Assert.Equal(0, buffer.Length); bufferSource.Verify(s => s.Return(It.IsAny()), Times.Exactly(3)); } + + [Fact] + public void UseAfterClear_Works() + { + // Arrange + var buffer = new PagedCharBuffer(new CharArrayBufferSource()); + + // Act - 1 + buffer.Append(new string('a', PagedCharBuffer.PageSize)); + buffer.Append(new string('b', 10)); + buffer.Clear(); + + // Assert - 1 + Assert.Equal(0, buffer.Length); + Assert.Equal(1, buffer.Pages.Count); + + // Act - 2 + buffer.Append("efgh"); + + // Assert - 2 + Assert.Equal(4, buffer.Length); + Assert.Equal(1, buffer.Pages.Count); + Assert.Equal(new[] { 'e', 'f', 'g', 'h' }, buffer.Pages[0].Take(buffer.Length)); + } } }