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

Conversation

atykhyy
Copy link
Contributor

@atykhyy atykhyy commented Jun 14, 2023

Currently, HotChocolate.Utilities.ArrayWriter.GetMemory()/GetSpan() can throw in rare cases because EnsureBufferCapacity allocates insufficient memory. Example: suppose the array writer has a 16k buffer (_buffer.Length == 0x4000) to which 8k bytes is already written (_start == 0x2000) and GetMemory(0x7000) is invoked, in turn calling EnsureBufferCapacity(0x7000). It will double the size of the buffer to 0x8000 and think did it's job because 0x7000 < 0x8000, but then _buffer.AsSpan().Slice(0x2000,0x7000) throws.

The one-liner PR fixes this without danger of integer overflow by subtracting _start when comparing needed capacity to newly available capacity.

@michaelstaib
Copy link
Member

Hey @atykhyy thank you for the fix! Can you also write a small unit test for that?

@michaelstaib michaelstaib merged commit 18713a6 into ChilliCream:main Jun 28, 2023
@michaelstaib
Copy link
Member

Thank you for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants