Skip to content

Commit

Permalink
Throw BadImageFormatException when missing PE metadata (#6270)
Browse files Browse the repository at this point in the history
Fixes #6200

Context
InvalidOperationException was being thrown by AssemblyInformation.CorePopulateMetadata, causing the ResolveAssemblyReference task to fail rather than disregarding the file.

Changes Made
I copied the code from AssemblyNameExtension to ensure there is metadata present before getting a metadata reader
  • Loading branch information
FiniteReality committed Mar 31, 2021
1 parent c2e41ab commit 7a89210
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Tasks/AssemblyDependency/AssemblyInformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand Down Expand Up @@ -361,6 +361,25 @@ private void CorePopulateMetadata()
using (var stream = File.OpenRead(_sourceFile))
using (var peFile = new PEReader(stream))
{
bool hasMetadata = false;
try
{
// This can throw if the stream is too small, which means
// the assembly doesn't have metadata.
hasMetadata = peFile.HasMetadata;
}
finally
{
// If the file does not contain PE metadata, throw BadImageFormatException to preserve
// behavior from AssemblyName.GetAssemblyName(). RAR will deal with this correctly.
if (!hasMetadata)
{
throw new BadImageFormatException(string.Format(CultureInfo.CurrentCulture,
AssemblyResources.GetString("ResolveAssemblyReference.AssemblyDoesNotContainPEMetadata"),
_sourceFile));
}
}

var metadataReader = peFile.GetMetadataReader();

var assemblyReferences = metadataReader.AssemblyReferences;
Expand Down

0 comments on commit 7a89210

Please sign in to comment.