Resolving infinite loop with buffers longer than MaxArrayLength #345
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 returnfalse
.Specifically, if the stream was longer than 1 GB, and
UseExponentialLargeBuffer
was set totrue
, it would try to pick the next available buffer size, which is 2 GB. Because of the way pool index is calculated, there was anInt32
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 inlong
, 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 inGetLargeBuffer
.