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

Commit 24c0a8e

Browse files
committed
Fix MemoryPoolIterator2.CopyTo's block traversal
- This fix prevents large request streams from being corrupted #234
1 parent 7e386ab commit 24c0a8e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public MemoryPoolIterator2 CopyTo(byte[] array, int offset, int count, out int a
442442
else
443443
{
444444
Buffer.BlockCopy(block.Array, index, array, offset, following);
445+
offset += following;
445446
remaining -= following;
446447
block = block.Next;
447448
index = block.Start;

test/Microsoft.AspNet.Server.KestrelTests/MemoryPoolBlock2Tests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,41 @@ public void AddDoesNotAdvanceAtEndOfCurrentBlock()
117117
}
118118
}
119119

120+
[Fact]
121+
public void CopyToCorrectlyTraversesBlocks()
122+
{
123+
using (var pool = new MemoryPool2())
124+
{
125+
var block1 = pool.Lease(128);
126+
var block2 = block1.Next = pool.Lease(128);
127+
128+
for (int i = 0; i < 128; i++)
129+
{
130+
block1.Array[block1.End++] = (byte)i;
131+
}
132+
for (int i = 128; i < 256; i++)
133+
{
134+
block2.Array[block2.End++] = (byte)i;
135+
}
136+
137+
var beginIterator = block1.GetIterator();
138+
139+
var array = new byte[256];
140+
int actual;
141+
var endIterator = beginIterator.CopyTo(array, 0, 256, out actual);
142+
143+
Assert.Equal(256, actual);
144+
145+
for (int i = 0; i < 256; i++)
146+
{
147+
Assert.Equal(i, array[i]);
148+
}
149+
150+
endIterator.CopyTo(array, 0, 256, out actual);
151+
Assert.Equal(0, actual);
152+
}
153+
}
154+
120155
private void AssertIterator(MemoryPoolIterator2 iter, MemoryPoolBlock2 block, int index)
121156
{
122157
Assert.Same(block, iter.Block);

0 commit comments

Comments
 (0)