Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type parsing error returned for type names with invalid start of assembly name #84141

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
jkotas marked this conversation as resolved.
Show resolved Hide resolved
jkotas marked this conversation as resolved.
Show resolved Hide resolved
}
}

namespace MyNamespace1
Expand Down