diff options
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs b/src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs index 4f115fe9d..b675100b0 100644 --- a/src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs +++ b/src/System.Private.CoreLib/shared/System/Buffers/Utilities.cs @@ -12,14 +12,19 @@ namespace System.Buffers [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static int SelectBucketIndex(int bufferSize) { + Debug.Assert(bufferSize >= 0); + + // bufferSize of 0 will underflow here, causing a huge + // index which the caller will discard because it is not + // within the bounds of the bucket array. uint bitsRemaining = ((uint)bufferSize - 1) >> 4; int poolIndex = 0; if (bitsRemaining > 0xFFFF) { bitsRemaining >>= 16; poolIndex = 16; } - if (bitsRemaining > 0xFF) { bitsRemaining >>= 8; poolIndex += 8; } - if (bitsRemaining > 0xF) { bitsRemaining >>= 4; poolIndex += 4; } - if (bitsRemaining > 0x3) { bitsRemaining >>= 2; poolIndex += 2; } - if (bitsRemaining > 0x1) { bitsRemaining >>= 1; poolIndex += 1; } + if (bitsRemaining > 0xFF) { bitsRemaining >>= 8; poolIndex += 8; } + if (bitsRemaining > 0xF) { bitsRemaining >>= 4; poolIndex += 4; } + if (bitsRemaining > 0x3) { bitsRemaining >>= 2; poolIndex += 2; } + if (bitsRemaining > 0x1) { bitsRemaining >>= 1; poolIndex += 1; } return poolIndex + (int)bitsRemaining; } |