Fix MemoryPoolIterator2.CopyTo's block traversal

- This fix prevents large request streams from being corrupted

#234
This commit is contained in:
Stephen Halter 2015-10-08 16:16:41 -07:00
parent 7e386ab576
commit 24c0a8e142
2 changed files with 36 additions and 0 deletions

View File

@ -442,6 +442,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
else
{
Buffer.BlockCopy(block.Array, index, array, offset, following);
offset += following;
remaining -= following;
block = block.Next;
index = block.Start;

View File

@ -117,6 +117,41 @@ namespace Microsoft.AspNet.Server.KestrelTests
}
}
[Fact]
public void CopyToCorrectlyTraversesBlocks()
{
using (var pool = new MemoryPool2())
{
var block1 = pool.Lease(128);
var block2 = block1.Next = pool.Lease(128);
for (int i = 0; i < 128; i++)
{
block1.Array[block1.End++] = (byte)i;
}
for (int i = 128; i < 256; i++)
{
block2.Array[block2.End++] = (byte)i;
}
var beginIterator = block1.GetIterator();
var array = new byte[256];
int actual;
var endIterator = beginIterator.CopyTo(array, 0, 256, out actual);
Assert.Equal(256, actual);
for (int i = 0; i < 256; i++)
{
Assert.Equal(i, array[i]);
}
endIterator.CopyTo(array, 0, 256, out actual);
Assert.Equal(0, actual);
}
}
private void AssertIterator(MemoryPoolIterator2 iter, MemoryPoolBlock2 block, int index)
{
Assert.Same(block, iter.Block);