Skip to content

Read metadata stream count as unsigned in MetadataReader - #135644

Merged
jkotas merged 1 commit into
dotnet:mainfrom
Empiree:fix/metadatareader-stream-count
Oct 13, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
Empiree:fix/metadatareader-stream-count

Conversation

@Empiree

@Empiree Empiree commented Oct 12, 2026

Copy link
Copy Markdown

MetadataReader reads the stream count from the metadata root with ReadInt16. In ECMA-335 (II.24.2.1) this is a 2 byte count, and the CoreCLR native reader reads it as USHORT. So any count of 0x8000 or more becomes negative and the array allocation throws OverflowException. The constructor documents BadImageFormatException for a bad metadata header, so I think that one is expected here.

The fix reads the count with ReadUInt16. I did the same change in ObfuscateWithExtraData helper in the tests because it parses the stream headers the same way.

The new test checks 0x8000 and 0xFFFF and expects BadImageFormatException. Without the fix it fails with OverflowException. I also tried CoreLib metadata with extra empty stream headers added, so the count is 0x8000 and 0xFFFF. With the fix it reads the same as the original, without the fix it throws OverflowException.

Other ReadInt16 calls in the library look fine to me. The CoffHeader ones match the short type in the public API, and a negative NumberOfSections is already rejected in PEHeaders.

Resolves #134612

Note

This PR was prepared with help from an AI assistant.

The count was read as Int16, so 0x8000 and up threw OverflowException.

Fix dotnet#134612
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 12, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jkotas
jkotas merged commit d8ad387 into dotnet:main Oct 13, 2026
98 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Reflection.Metadata community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MetadataReader throws OverflowException for a stream count >= 0x8000

2 participants