Repository navigation
Description
Description
In .NET 11.0.0, BigInteger's left shift no longer enforces the type's maximum length. BigInteger.One << int.MaxValue returns a 2,147,483,648-bit value. .NET 10 throws OverflowException for that shift.
The resulting value is over the limit that every other operation still checks. So a later, unrelated operation fails instead of the shift that made the value:
x + 1,x * 1and evenx >> 1throwOverflowException.BigInteger.RotateLeft(x, 1)returns1.
Reproduction Steps
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
</PropertyGroup>
</Project>using System;
using System.Numerics;
Console.WriteLine($".NET {Environment.Version}");
static void Run(string what, Func<object> f)
{
try { Console.WriteLine($" {what} = {f()}"); }
catch (Exception e) { Console.WriteLine($" {what} -> {e.GetType().Name}"); }
}
BigInteger x = BigInteger.Zero;
Run("(BigInteger.One << int.MaxValue).GetBitLength()", () => (x = BigInteger.One << int.MaxValue).GetBitLength());
Run("(x >> 1).GetBitLength()", () => (x >> 1).GetBitLength());
Run("(x + 1).GetBitLength()", () => (x + 1).GetBitLength());
Run("(x * 1).GetBitLength()", () => (x * 1).GetBitLength());
Run("BigInteger.RotateLeft(x, 1) == (x << 1)", () => BigInteger.RotateLeft(x, 1) == (x << 1));
Run("BigInteger.RotateLeft(x, 1)", () => BigInteger.RotateLeft(x, 1));Build with dotnet build -c Release, then run bin/Release/net10.0/<name>.dll and bin/Release/net11.0/<name>.dll.
Expected behavior
The same as .NET 10.0.15. The shift throws because the result would exceed the maximum length, so x stays 0:
.NET 10.0.15
(BigInteger.One << int.MaxValue).GetBitLength() -> OverflowException
(x >> 1).GetBitLength() = 0
(x + 1).GetBitLength() = 1
(x * 1).GetBitLength() = 0
BigInteger.RotateLeft(x, 1) == (x << 1) = True
BigInteger.RotateLeft(x, 1) = 0
Actual behavior
.NET 11.0.0
(BigInteger.One << int.MaxValue).GetBitLength() = 2147483648
(x >> 1).GetBitLength() -> OverflowException
(x + 1).GetBitLength() -> OverflowException
(x * 1).GetBitLength() -> OverflowException
BigInteger.RotateLeft(x, 1) == (x << 1) = False
BigInteger.RotateLeft(x, 1) = 1
- The shift succeeds and produces a value over the maximum length.
x * 1andx >> 1then throw, although neither result is larger thanx.RotateLeft(x, 1)returns1, which looks like anintoverflow in the value's bit width (just over 2^31), not an exception.x << 64also succeeds, making the value larger still.
Regression?
Yes. .NET 10.0.12 and 10.0.15 throw OverflowException from the shift; .NET 11.0.0 does not.
I haven't bisected it. A likely candidate is #125799 ("Rewrite BigInteger internals to use native-width (nuint) limbs"), since the length check counts limbs and the limb size changed.
Known Workarounds
Check the result size before shifting (for example value.GetBitLength() + shift) and throw yourself.
Configuration
- .NET SDK 11.0.100, runtime 11.0.0; compared with runtimes 10.0.12 and 10.0.15.
- Linux x64 (Debian 13).
- Not checked on other architectures.
Other information
I found this through the test suite of IronRuby, which runs Ruby's Integer on BigInteger:
- Ruby's
1 << 2**31succeeded on .NET 11, and the next operation on the result raised a rawSystem.OverflowException. - One test spent over 20 minutes building and walking the oversized value.
IronRuby now checks the size before shifting. I don't think this is a security issue: the value takes about 256 MB, about the same as the largest valid value, and the later failures are managed exceptions. I didn't see a crash or memory corruption across about 20 operations on it.
Activity
dotnet-policy-service commentedon Nov 11, 2026
Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.
lilinus commentedon Nov 12, 2026
Looks like the issue was fixed in main in #132709. Unfortunately it didn't make it into .NET 11.
[release/11.0] Enforce BigInteger maximum length for shifts (#137427)