Skip to content

Commit

Permalink
[mono] Fix assembly name parser to accommodate non-ASCII UTF8 strings (
Browse files Browse the repository at this point in the history
…#103363)

Fixes #103276

Added an automated test that the testcase failed in the above issue. It now passes with this change.

Ideally, Mono should share the same assembly name parsing logic as CoreCLR, which is `AssemblyNameParser.TryParse`.
  • Loading branch information
fanyang-mono authored Jun 14, 2024
1 parent d7ae8c6 commit 39c4905
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ public void TestTypeIdentifierAttribute()
Assert.Equal(42, mi.Invoke(null, args));
}

[Fact]
public void TestAssemblyNameWithInternationalChar()
{
Type testObj = typeof(Hello工程123.Program);
var t = Type.GetType(testObj.AssemblyQualifiedName);
Assert.NotNull(t);
}

[Fact]
public void IgnoreLeadingDotForTypeNamesWithoutNamespace()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace System.Reflection.Hello工程123
{
public class Program
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Hello工程123.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<ProjectReference Include="UnloadableAssembly\UnloadableAssembly.csproj" />
<ProjectReference Include="TestExe\System.Reflection.TestExe.csproj" />
<ProjectReference Include="TestAssembly\TestAssembly.csproj" />
<ProjectReference Include="Hello工程123\Hello工程123.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetOS)' == 'browser'">
<WasmFilesToIncludeFromPublishDir Include="$(AssemblyName).dll" />
Expand All @@ -87,5 +88,6 @@

<!-- Assemblies that should be excluded from the bundle -->
<__ExcludeFromBundle Include="TestAssembly.dll" />
<__ExcludeFromBundle Include="Hello工程123.dll" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/mono/mono/eglib/eglib-remap.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
#define g_wtf8_to_utf16 monoeg_g_wtf8_to_utf16
#define g_utf8_to_utf16_custom_alloc monoeg_g_utf8_to_utf16_custom_alloc
#define g_utf8_to_utf16le_custom_alloc monoeg_g_utf8_to_utf16le_custom_alloc
#define g_utf8_validate_part monoeg_g_utf8_validate_part
#define g_utf8_validate monoeg_g_utf8_validate
#define g_vasprintf monoeg_g_vasprintf
#define g_assertion_disable_global monoeg_assertion_disable_global
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/eglib/glib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ g_async_safe_printf (gchar const *format, ...)
*/
extern const guchar g_utf8_jump_table[256];

gboolean g_utf8_validate_part (const unsigned char *inptr, size_t len);
gboolean g_utf8_validate (const gchar *str, gssize max_len, const gchar **end);
glong g_utf8_strlen (const gchar *str, gssize max);

Expand Down
8 changes: 4 additions & 4 deletions src/mono/mono/eglib/gutf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const guchar g_utf8_jump_table[256] = {
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
};

static gboolean
utf8_validate (const unsigned char *inptr, size_t len)
gboolean
g_utf8_validate_part (const unsigned char *inptr, size_t len)
{
const unsigned char *ptr = inptr + len;
unsigned char c;
Expand Down Expand Up @@ -105,7 +105,7 @@ g_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
if (max_len < 0) {
while (*inptr != 0) {
length = g_utf8_jump_table[*inptr];
if (!utf8_validate (inptr, length)) {
if (!g_utf8_validate_part (inptr, length)) {
valid = FALSE;
break;
}
Expand All @@ -124,7 +124,7 @@ g_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
length = g_utf8_jump_table[*inptr];
min = MIN (length, GSSIZE_TO_UINT (max_len - n));

if (!utf8_validate (inptr, min)) {
if (!g_utf8_validate_part (inptr, min)) {
valid = FALSE;
break;
}
Expand Down
15 changes: 12 additions & 3 deletions src/mono/mono/metadata/reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,17 @@ assembly_name_to_aname (MonoAssemblyName *assembly, char *p)
}
assembly->name = p;
s = p;
while (*p && (isalnum (*p) || *p == '.' || *p == '-' || *p == '_' || *p == '$' || *p == '@' || g_ascii_isspace (*p)))
p++;
guchar *inptr = (guchar *) p;
while (*p && (*p != ',') && (*p != '\0')) {
if (quoted && (*p == '"'))
break;
guint length = g_utf8_jump_table[*inptr];
if (!g_utf8_validate_part (inptr, length)) {
return 0;
}
p += length;
inptr += length;
}
if (quoted) {
if (*p != '"')
return 1;
Expand Down Expand Up @@ -1648,7 +1657,7 @@ assembly_name_to_aname (MonoAssemblyName *assembly, char *p)
found_sep = 1;
continue;
}
/* failed */
/* Done processing */
if (!found_sep)
return 1;
}
Expand Down

0 comments on commit 39c4905

Please sign in to comment.