Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 3afea82

Browse files
Eitan Har-Shoshanimchwarr
authored andcommitted
[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
1 parent 473e589 commit 3afea82

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ tag versions. The Bond compiler (`gbc`) and
1111
different versioning scheme, following the Haskell community's
1212
[package versioning policy](https://wiki.haskell.org/Package_versioning_policy).
1313

14+
## Unreleased ##
15+
16+
* IDL core version: TBD
17+
* C++ version: TBD
18+
* C# NuGet version: bug fix bump needed
19+
* `gbc` & compiler library: TBD
20+
21+
### C# ###
22+
23+
* Fixed a performance regression in `OutputBuffer.Grow`: it was incorrectly
24+
growing the buffer by one byte at a time instead of geometrically. ([Issue
25+
\#1065](https://github.com/microsoft/bond/issues/1065), [Pull request
26+
\#1066](https://github.com/microsoft/bond/pull/1066))
27+
1428
## 9.0.2: 2020-08-03 ##
1529
* IDL core version: 3.0
1630
* C++ version: 9.0.2

cs/src/core/io/safe/OutputBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public virtual void WriteString(Encoding encoding, string value, int size)
221221
internal virtual void Grow(int count)
222222
{
223223
int minLength = checked(position + count);
224-
length = checked(length + length >> 1);
224+
length = checked(length + (length >> 1));
225225

226226
const int ArrayIndexMaxValue = 0x7FFFFFC7;
227227
if ((uint)length > ArrayIndexMaxValue)

0 commit comments

Comments
 (0)