Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayWriter.EnsureBufferCapacity must account for _start #6266

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/HotChocolate/Utilities/src/Utilities/ArrayWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void EnsureBufferCapacity(int neededCapacity)

// if that new buffer size is not enough to satisfy the needed capacity
// we add the needed capacity to the doubled buffer capacity.
if (neededCapacity > newSize)
if (neededCapacity > newSize - _start)
{
newSize += neededCapacity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,18 @@ public void WriteBytesExceedingInitialBufferSize_ShouldExpandAndWriteCorrectly()
var writtenSpan = writer.GetWrittenSpan();
Assert.True(testData.SequenceEqual(writtenSpan.ToArray()));
}

[Fact]
public void ShouldAllocateSufficientMemory()
{
// Arrange
using var writer = new ArrayWriter();

// Act
// NB: ask for 0x3000 bytes because the initial 512 bytes buffer size is added
// to request when doubling is insufficient and ArrayPool<byte> sizes are powers of 2
writer.GetSpan (0x3000) ;
writer.Advance (0x2000) ;
writer.GetSpan (0x7000) ;
}
}