Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c#] Fix OutputBuffer.Grow geometric growth regression
Recently, in order to mitigate CVE-2020-1469, among other changes in b0fd4a1 ([c#] Fix handling of large container lengths, 2020-07-06), `OutputBuffer.Grow` changed from `length += length >> 1` to `length = checked(length + length >> 1)`. This actually doesn't change the value of `length`, because `+` has [higher precedence][1] than `>>`. `length` gets doubled and then divided by two, so it remains unchanged (except when overflow occurs). This causes serious performance regression as the underlying buffer is then grown by single byte at a time instead of growing by half of current size. The fix is to use `length = checked(length + (length >> 1))` to explicitly perform the shift before the addition. Fixes #1065 Closes #1066 [1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/#operator-precedence
- Loading branch information