Skip to content

MetadataStringComparer needs to use the user-supplied MetadataStringDecoder. #13854

Closed

Description

@nguerrera

MetadataReader.GetString now uses the Encoding provided via MetadataStringDecoder.Encoding or falls back to Encoding.UTF8 if none is provided.

However, MetadataStringComparer still uses a custom UTF-8 implementation, which does not handle errors correctly and prevents any customization of the fallback behaviour.

Care must be taken when making this change to not allocate as that is essentially the entire point of MetadataStringComparer.

Activity

  1. changed the title [-][System.Reflection.Metadata] MetadataStringComparer needs to use the user-supplied MetadataStringDecoder.[/-] [+]MetadataStringComparer needs to use the user-supplied MetadataStringDecoder.[/+] on Nov 16, 2014
  2. pauldipietro commented on Feb 20, 2015

    @pauldipietro

    Could you possibly clarify the use of "not to allocate" in the last sentence? Assuming that this is a user-supplied MetadataStringDecoder and using one of the code examples in MetadataStringComparer, do you have something in mind such as:

    if (reader.StringComparer.Equals(typeDef.Namespace, "System", MetadataStringDecoder decoder) &&
        reader.StringComparer.Equals(typeDef.Name, "Object", MetadataStringDecoder decoder))
    {
        // found System.Object
    }
    
  3. nguerrera commented on Feb 20, 2015

    @nguerrera
    ContributorAuthor

    @orteipid The MetadataReader already has a MetadataStringDecoder and the StringComparer has a MetadataReader. There's no need to pass it in.

    The problem is that we don't use the decoder to compare UTF-8 values in metadata to UTF-16 System.Strings. Today, we only use it to create System.Strings from metadata. This is inconsistent but only observable for malformed UTF-8 in metadata.

    The entire point of MetadataStringComparer.Equals is to avoid allocation so we can't just create a string using the MetadataStringDecoder and call String.Equals. I think we'll have to decode to UTF-16 on the stack or pooled memory N characters at a time, but we should optimize the common ascii-only case to not bother copying the text anywhere.

  4. transferred this issue fromdotnet/corefxon Jan 31, 2020
  5. added this to the 1.0.0-rtm milestone on Jan 31, 2020
  6. ghost locked as resolved and limited conversation to collaborators on Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions