Skip to content

BigInteger.TryParse out-of-bounds access #28652

@Metalnem

Description

@Metalnem

BigInteger.TryParse overloads accepting ReadOnlySpan<char> are ignoring the length of the span, and are reading beyond its end if there are more digits available. Here's the program to reproduce this:

using System;
using System.Numerics;

namespace BigInt
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var s = "123456789";
      var span = s.AsSpan(0, 1);

      if (BigInteger.TryParse(span, out var result))
      {
        Console.WriteLine(result);
      }
    }
  }
}

This program should be printing 1, but it's printing 123456789 instead. My running environment looks like this:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.103
 Commit:    8edbc2570a

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.2.103/

Found via SharpFuzz.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions