Skip to content

BigInteger left shift no longer enforces the maximum length in .NET 11 #137327

Open

Description

@Largo

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 * 1 and even x >> 1 throw OverflowException.
  • BigInteger.RotateLeft(x, 1) returns 1.

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 * 1 and x >> 1 then throw, although neither result is larger than x.
  • RotateLeft(x, 1) returns 1, which looks like an int overflow in the value's bit width (just over 2^31), not an exception.
  • x << 64 also 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**31 succeeded on .NET 11, and the next operation on the result raised a raw System.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

  1. added
    untriaged
    on Nov 11, 2026
  2. dotnet-policy-service commented on Nov 11, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics
    See info in area-owners.md if you want to be subscribed.

  3. lilinus commented on Nov 12, 2026

    @lilinus
    Contributor

    Looks like the issue was fixed in main in #132709. Unfortunately it didn't make it into .NET 11.

  4. added a commit that references this issue on Nov 16, 2026
    8a9c164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Numericsuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions