Skip to content

Commit

Permalink
Fix integer overflow bug (#173)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Watson <[email protected]>
  • Loading branch information
benmwatson and Ben Watson authored Jun 18, 2021
1 parent 3695c10 commit b7396a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions UnitTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3596,6 +3596,19 @@ public override void AllMultiplesOrExponentialUpToMaxCanBePooled()
}
}

// Issue #171 - Infinite Loop when particular values are chosen causing an int overflow during
// validation of buffer sizes.
// https://github.com/microsoft/Microsoft.IO.RecyclableMemoryStream/issues/171
[Test, Timeout(10000)]
public void InvalidLargeBufferSizeDoesNotCauseOverflow()
{
const int BlockSize = 128;
const int LargeBufferMultiple = 1024;
const int MaxBufferSize = int.MaxValue;

Assert.Throws<ArgumentException>(()=>new RecyclableMemoryStreamManager(BlockSize, LargeBufferMultiple, MaxBufferSize, this.UseExponentialLargeBuffer) { AggressiveBufferReturn = this.AggressiveBufferRelease });
}

[Test]
public override void RequestTooLargeBufferAdjustsInUseCounter()
{
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<DebugType>Full</DebugType> <!-- NUnit in VS2017 needs this instead of the new slimmer PDBs -->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/RecyclableMemoryStreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private long RoundToLargeBufferSize(long requiredSize)
{
if (this.UseExponentialLargeBuffer)
{
int pow = 1;
long pow = 1;
while (this.LargeBufferMultiple * pow < requiredSize)
{
pow <<= 1;
Expand Down

0 comments on commit b7396a3

Please sign in to comment.