From b9bf13ad644aa3d1c98575190ada082bfbccc162 Mon Sep 17 00:00:00 2001 From: Anton Tykhyy Date: Wed, 14 Jun 2023 22:29:27 +0300 Subject: [PATCH 1/2] ArrayWriter.EnsureBufferCapacity must account for _start --- src/HotChocolate/Utilities/src/Utilities/ArrayWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HotChocolate/Utilities/src/Utilities/ArrayWriter.cs b/src/HotChocolate/Utilities/src/Utilities/ArrayWriter.cs index b9c7f2e32b3..05b681ae39d 100644 --- a/src/HotChocolate/Utilities/src/Utilities/ArrayWriter.cs +++ b/src/HotChocolate/Utilities/src/Utilities/ArrayWriter.cs @@ -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; } From 8227eb63b46ff0aaa7c22b2caaf7348fe9a806f2 Mon Sep 17 00:00:00 2001 From: Anton Tykhyy Date: Fri, 16 Jun 2023 17:17:52 +0300 Subject: [PATCH 2/2] Add unit test --- .../test/Utilities.Tests/ArrayWriterTests.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/HotChocolate/Utilities/test/Utilities.Tests/ArrayWriterTests.cs b/src/HotChocolate/Utilities/test/Utilities.Tests/ArrayWriterTests.cs index 57bfd0c64a1..7135a0ab022 100644 --- a/src/HotChocolate/Utilities/test/Utilities.Tests/ArrayWriterTests.cs +++ b/src/HotChocolate/Utilities/test/Utilities.Tests/ArrayWriterTests.cs @@ -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 sizes are powers of 2 + writer.GetSpan (0x3000) ; + writer.Advance (0x2000) ; + writer.GetSpan (0x7000) ; + } }