Repository navigation
Read metadata stream count as unsigned in MetadataReader - #135644
Merged
Merged
Conversation
The count was read as Int16, so 0x8000 and up threw OverflowException. Fix dotnet#134612
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-reflection-metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.