Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 760c8f3

Browse files
committed
Ensure Append writes to the right page after Clear
1 parent 42027b6 commit 760c8f3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/PagedCharBuffer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ public void Clear()
115115
}
116116

117117
_charIndex = 0;
118+
CurrentPage = Pages.Count > 0 ? Pages[0] : null;
118119
}
119120

120121
private char[] GetCurrentPage()
121122
{
122123
if (CurrentPage == null ||
123-
_charIndex == CurrentPage.Length)
124+
_charIndex == PageSize)
124125
{
125126
CurrentPage = NewPage();
126127
_charIndex = 0;

test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PagedCharBufferTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,29 @@ public void Clear_ReturnsPagesToBufferSource()
206206
Assert.Equal(0, buffer.Length);
207207
bufferSource.Verify(s => s.Return(It.IsAny<char[]>()), Times.Exactly(3));
208208
}
209+
210+
[Fact]
211+
public void UseAfterClear_Works()
212+
{
213+
// Arrange
214+
var buffer = new PagedCharBuffer(new CharArrayBufferSource());
215+
216+
// Act - 1
217+
buffer.Append(new string('a', PagedCharBuffer.PageSize));
218+
buffer.Append(new string('b', 10));
219+
buffer.Clear();
220+
221+
// Assert - 1
222+
Assert.Equal(0, buffer.Length);
223+
Assert.Equal(1, buffer.Pages.Count);
224+
225+
// Act - 2
226+
buffer.Append("efgh");
227+
228+
// Assert - 2
229+
Assert.Equal(4, buffer.Length);
230+
Assert.Equal(1, buffer.Pages.Count);
231+
Assert.Equal(new[] { 'e', 'f', 'g', 'h' }, buffer.Pages[0].Take(buffer.Length));
232+
}
209233
}
210234
}

0 commit comments

Comments
 (0)