Skip to content

Commit

Permalink
Fix type parsing error returned for type names with invalid start of …
Browse files Browse the repository at this point in the history
…assembly name

Fixes #84118
  • Loading branch information
jkotas committed Mar 30, 2023
1 parent ef15cfb commit 3883c36
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/libraries/Common/src/System/Reflection/TypeNameParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ private TypeNameParser(ReadOnlySpan<char> name)
return null;

assemblyName = GetNextAssemblyName();
if (assemblyName is null)
return null;
Debug.Assert(Peek == TokenType.End);
}

Expand Down Expand Up @@ -126,7 +128,7 @@ private TypeNameParser(ReadOnlySpan<char> name)
return null;

// Because "[" is used both for generic arguments and array indexes, we must peek two characters deep.
if (!(Peek == TokenType.OpenSqBracket && (PeekSecond == TokenType.Other || PeekSecond == TokenType.OpenSqBracket)))
if (!(Peek is TokenType.OpenSqBracket && (PeekSecond is TokenType.Other or TokenType.OpenSqBracket)))
return namedType;

Skip();
Expand Down Expand Up @@ -328,9 +330,10 @@ private TokenType GetNextToken()
// Lex the next segment as the assembly name at the end of an assembly-qualified type name. (Do not use for
// assembly names embedded inside generic type arguments.)
//
private string GetNextAssemblyName()
private string? GetNextAssemblyName()
{
SkipWhiteSpace();
if (!StartAssemblyName())
return null;

string assemblyName = new string(_input.Slice(_index));
_index = _input.Length;
Expand All @@ -344,7 +347,8 @@ private string GetNextAssemblyName()
//
private string? GetNextEmbeddedAssemblyName()
{
SkipWhiteSpace();
if (!StartAssemblyName())
return null;

ValueStringBuilder sb = new ValueStringBuilder(stackalloc char[64]);

Expand Down Expand Up @@ -382,6 +386,18 @@ private string GetNextAssemblyName()
return sb.ToString();
}

private bool StartAssemblyName()
{
// Compat: Treat invalid starting token of assembly name as type name parsing error instead of assembly name parsing error. This only affects
// exception returned by the parser.
if (Peek is TokenType.End or TokenType.Comma)
{
ParseError();
return false;
}
return true;
}

//
// Classify a character as a TokenType. (Fortunately, all tokens in type name strings other than identifiers are single-character tokens.)
//
Expand Down
9 changes: 9 additions & 0 deletions src/libraries/System.Reflection/tests/GetTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ public void GetType_GenericTypeArgumentList()
Assert.Equal(typeof(System.Reflection.Tests.GenericClass<System.String>), Type.GetType("System.Reflection.Tests.GenericClass`1[[System.String, System.Private.CoreLib]]", throwOnError: true));
Assert.Throws<FileNotFoundException>(() => Type.GetType("System.Reflection.Tests.GenericClass`1[[Bogus, BogusAssembly]]", throwOnError: true));
}

[Fact]
public void GetType_InvalidAssemblyName()
{
Assert.Null(Type.GetType("MissingAssemblyName, "));
Assert.Null(Type.GetType("ExtraComma, ,"));
Assert.Null(Type.GetType("ExtraComma, , System.Runtime"));
Assert.Throws<FileLoadException>(() => Type.GetType("System.Object, System.Runtime, Version=x.y"));
}
}

namespace MyNamespace1
Expand Down

0 comments on commit 3883c36

Please sign in to comment.