Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
fixed FileLoadException when trying to do clr.AddReference('/full/lib…
…/path.dll')

Before trying to load an assembly by its full path we were trying to call `Assembly.Load` on it. `Assembly.Load` interprets its argument as a valid `AssemblyName`. However full paths are not valid assembly names, so that call would throw `FileLoadException`, which we did not handle.

reported in 9d5f579#commitcomment-57061082

Related: #1514
  • Loading branch information
lostmsu committed Sep 27, 2021
commit 5e68b1611a96f4b208c6d7cd6b923582b6231a76
14 changes: 12 additions & 2 deletions src/embed_tests/pyimport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;

using NUnit.Framework;
using Python.Runtime;

Expand Down Expand Up @@ -83,10 +85,18 @@ public void TestCastGlobalVar()
public void BadAssembly()
{
string path;
if (Python.Runtime.Runtime.IsWindows)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path = @"C:\Windows\System32\kernel32.dll";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
path = "/usr/lib/libc.dylib";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
path = "/usr/lib/locale/locale-archive";
}
else
{
Assert.Pass("TODO: add bad assembly location for other platforms");
Expand All @@ -98,7 +108,7 @@ import clr
clr.AddReference('{path}')
";

Assert.Throws<FileLoadException>(() => PythonEngine.Exec(code));
Assert.Throws<BadImageFormatException>(() => PythonEngine.Exec(code));
}
}
}
25 changes: 15 additions & 10 deletions src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ private static Assembly ResolveHandler(object ob, ResolveEventArgs args)
return LoadAssemblyPath(name.Name);
}

internal static AssemblyName? TryParseAssemblyName(string name)
{
try
{
return new AssemblyName(name);
}
catch (FileLoadException)
{
return null;
}
}


/// <summary>
/// We __really__ want to avoid using Python objects or APIs when
Expand Down Expand Up @@ -208,18 +220,11 @@ static IEnumerable<string> FindAssemblyCandidates(string name)

/// <summary>
/// Loads an assembly from the application directory or the GAC
/// given a simple assembly name. Returns the assembly if loaded.
/// given its name. Returns the assembly if loaded.
/// </summary>
public static Assembly LoadAssembly(string name)
public static Assembly LoadAssembly(AssemblyName name)
{
try
{
return Assembly.Load(name);
}
catch (FileNotFoundException)
{
return null;
}
return Assembly.Load(name);
}


Expand Down
4 changes: 2 additions & 2 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ public static Assembly AddReference(string name)
{
assembly = AssemblyManager.LoadAssemblyPath(name);
}
if (assembly == null)
if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName)
{
assembly = AssemblyManager.LoadAssembly(name);
assembly = AssemblyManager.LoadAssembly(parsedName);
}
if (assembly == null)
{
Expand Down