This repository was archived by the owner on Apr 1, 2025. It is now read-only.
Commit 3afea82
[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-precedence1 parent 473e589 commit 3afea82
2 files changed
+15
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
14 | 28 | | |
15 | 29 | | |
16 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
224 | | - | |
| 224 | + | |
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| |||
0 commit comments