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

Resolving infinite loop with buffers longer than MaxArrayLength #345

Merged
merged 2 commits into from
Jun 11, 2024

Conversation

benmwatson
Copy link
Member

Resolves #344

As reported in #344 , there is an infinite loop that can happen when trying to call TryGetBuffer on a very large stream. The correct response is to return false.

Specifically, if the stream was longer than 1 GB, and UseExponentialLargeBuffer was set to true, it would try to pick the next available buffer size, which is 2 GB. Because of the way pool index is calculated, there was an Int32 underflow, which always compared false to the loop exit condition, thus the infinite loop.

At first, I fixed the GetPoolIndex method to do all its math in long, but this just pushes the error further down the call stack because you can't actually allocate an array that big. Rather than add new exception conditions in multiple methods, the simplest way to resolve the issue was to just move the existing array length check to be after the rounding step in GetLargeBuffer.

@benmwatson benmwatson merged commit 7a74201 into master Jun 11, 2024
5 checks passed
@benmwatson benmwatson deleted the infinite_loop-fix branch June 11, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RecyclableMemoryStream.TryGetBuffer causes infinite loop when UseExponentialLargeBuffer is true
1 participant