Skip to content

MetadataReader throws OverflowException for a stream count >= 0x8000 #134612

Closed

Description

@pawlos

Description

The ECMA-335 metadata root's Streams field (II.24.2.1) is a 2-byte count of the stream headers that follow; CoreCLR's native metadata reader declares it USHORT iStreams. MetadataReader.ReadStreamHeaders reads it as Int16, so any value from 0x8000 up becomes negative and is passed straight to an array allocation. The result is OverflowException, where the documented failure mode is BadImageFormatException.

Reproduction Steps

using System.Reflection.Metadata;

byte[] bytes = Convert.FromHexString("4253 4a42 0100 0101 0064 0000 0000 0000 00ff fff7".Replace(" ",""));

using var provider = MetadataReaderProvider.FromPortablePdbStream(new MemoryStream(bytes));
_ = provider.GetMetadataReader();   // OverflowException

Expected behavior

A count of 0x8000 or more should be treated like any other count: parsed if the stream headers are actually present, BadImageFormatException if they are not.

Actual behavior

Overflow exception is thrown.

Regression?

No

Known Workarounds

None

Configuration

.NET 10.0.7./.NET 11.0.0-rc.1.26425.128
Ubuntu x64, Windows x64
Not specific to configuration

Other information

Root Cause:

private static StreamHeader[] ReadStreamHeaders(ref BlobReader memReader)
{
    // storage header:
    memReader.ReadUInt16();
    int streamCount = memReader.ReadInt16();

    var streamHeaders = new StreamHeader[streamCount];

newarr with a negative length throws OverflowException, which is what surfaces. The preceding Flags field on the line above is correctly read as UInt16; only the count is signed.

Activity

  1. added
    untriaged
    on Sep 24, 2026
  2. dotnet-policy-service commented on Sep 24, 2026

    @dotnet-policy-service
    Contributor

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

  3. Empiree commented on Sep 28, 2026

    @Empiree

    Hi @pawlos, are you planning to send a PR for this? If not, I'd like to take it

  4. pawlos commented on Sep 28, 2026

    @pawlos
    Author

    @Empiree go ahead

  5. added a commit that references this issue on Oct 13, 2026
    d8ad387
  6. removed
    untriaged
    on Oct 13, 2026
  7. locked and limited conversation to collaborators on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions