Skip to content

Commit

Permalink
Return null from GetManifestResourceInfo and GetManifestResourceStrea…
Browse files Browse the repository at this point in the history
…m when the resource is not found (dotnet#56602)
  • Loading branch information
jeffhandley authored Jul 31, 2021
1 parent 3afb043 commit fe671e1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Reflection.TypeLoading.Ecma
/// </summary>
internal sealed partial class EcmaAssembly
{
public sealed override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
public sealed override ManifestResourceInfo? GetManifestResourceInfo(string resourceName)
{
if (resourceName == null)
throw new ArgumentNullException(nameof(resourceName));
Expand All @@ -21,6 +21,11 @@ public sealed override ManifestResourceInfo GetManifestResourceInfo(string resou

InternalManifestResourceInfo internalManifestResourceInfo = GetEcmaManifestModule().GetInternalManifestResourceInfo(resourceName);

if (!internalManifestResourceInfo.Found)
{
return null;
}

if (internalManifestResourceInfo.ResourceLocation == ResourceLocation.ContainedInAnotherAssembly)
{
// Must get resource info from other assembly, and OR in the contained in another assembly information
Expand Down Expand Up @@ -62,6 +67,12 @@ public sealed override string[] GetManifestResourceNames()
throw new ArgumentException(nameof(name));

InternalManifestResourceInfo internalManifestResourceInfo = GetEcmaManifestModule().GetInternalManifestResourceInfo(name);

if (!internalManifestResourceInfo.Found)
{
return null;
}

if ((internalManifestResourceInfo.ResourceLocation & ResourceLocation.Embedded) != 0)
{
unsafe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public sealed override AssemblyName[] GetReferencedAssemblies()
public abstract override MethodInfo? EntryPoint { get; }

// Manifest resource support.
public abstract override ManifestResourceInfo GetManifestResourceInfo(string resourceName);
public abstract override ManifestResourceInfo? GetManifestResourceInfo(string resourceName);
public abstract override string[] GetManifestResourceNames();
public abstract override Stream? GetManifestResourceStream(string name);
public sealed override Stream? GetManifestResourceStream(Type type, string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal RoStubAssembly() : base(null!, 0) { }
public sealed override bool IsDynamic => throw null!;
public sealed override event ModuleResolveEventHandler? ModuleResolve { add { throw null!; } remove { throw null!; } }
public sealed override IEnumerable<CustomAttributeData> CustomAttributes => throw null!;
public sealed override ManifestResourceInfo GetManifestResourceInfo(string resourceName) => throw null!;
public sealed override ManifestResourceInfo? GetManifestResourceInfo(string resourceName) => throw null!;
public sealed override string[] GetManifestResourceNames() => throw null!;
public sealed override Stream GetManifestResourceStream(string name) => throw null!;
protected sealed override AssemblyNameData[] ComputeAssemblyReferences() => throw null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,5 +602,27 @@ public static void CrossAssemblyTypeRefToNestedType()
Assert.Equal(expected, bt);
}
}

[Fact]
public static void ResourceDoesNotExist_GetManifestResourceInfo_ReturnsNull()
{
using (MetadataLoadContext lc = new MetadataLoadContext(new SimpleAssemblyResolver()))
{
Assembly a = lc.LoadFromByteArray(TestData.s_AssemblyWithEmbeddedResourcesImage);
ManifestResourceInfo? r = a.GetManifestResourceInfo("ResourceThatDoesNotExist");
Assert.Null(r);
}
}

[Fact]
public static void ResourceDoesNotExist_GetManifestResourceStream_ReturnsNull()
{
using (MetadataLoadContext lc = new MetadataLoadContext(new SimpleAssemblyResolver()))
{
Assembly a = lc.LoadFromByteArray(TestData.s_AssemblyWithEmbeddedResourcesImage);
Stream? r = a.GetManifestResourceStream("ResourceThatDoesNotExist");
Assert.Null(r);
}
}
}
}

0 comments on commit fe671e1

Please sign in to comment.