Skip to content

Commit 70357d6

Browse files
jhonabreulclaude
andcommitted
Load assembly from full path before parsing it as an assembly name
clr.AddReference with a rooted path to a non-managed file (e.g. a native library) should surface a BadImageFormatException. On Windows that happened because new AssemblyName(@"C:\...\kernel32.dll") fails to parse, so the code fell through to LoadAssemblyFullPath -> Assembly.LoadFrom -> BadImageFormat. On Linux the path "/.../libpython3.10.so.1.0" parses fine as an AssemblyName, so Assembly.Load(name) ran first and threw FileNotFoundException ("The system cannot find the file specified") before LoadAssemblyFullPath was reached. This broke the BadAssembly embedding test on Ubuntu. Try LoadAssemblyFullPath (which loads an existing file from disk) before the parse-as-assembly-name path, so a real file on disk yields BadImageFormatException consistently across platforms. Non-rooted names are unaffected and still fall through to Assembly.Load. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ccba56d commit 70357d6

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/runtime/Types/ModuleObject.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,19 @@ public static Assembly AddReference(string name)
505505
{
506506
assembly = AssemblyManager.LoadAssemblyPath(name);
507507
}
508-
if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName)
509-
{
510-
assembly = AssemblyManager.LoadAssembly(parsedName);
511-
}
508+
// Try loading an existing file on disk before parsing the name as an
509+
// assembly name. A rooted path (e.g. a native library) can parse as a
510+
// valid AssemblyName on some platforms, which would make Assembly.Load
511+
// throw FileNotFoundException instead of letting Assembly.LoadFrom open
512+
// the file and surface the real BadImageFormatException.
512513
if (assembly == null)
513514
{
514515
assembly = AssemblyManager.LoadAssemblyFullPath(name);
515516
}
517+
if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName)
518+
{
519+
assembly = AssemblyManager.LoadAssembly(parsedName);
520+
}
516521
if (assembly == null)
517522
{
518523
throw new FileNotFoundException($"Unable to find assembly '{name}'.");

0 commit comments

Comments
 (0)