Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw BadImageFormatException when missing PE metadata #6270

Merged
merged 2 commits into from
Mar 31, 2021
Merged
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
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;
KirillOsenkov marked this conversation as resolved.
Show resolved Hide resolved
}
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,
KirillOsenkov marked this conversation as resolved.
Show resolved Hide resolved
AssemblyResources.GetString("ResolveAssemblyReference.AssemblyDoesNotContainPEMetadata"),
_sourceFile));
}
}

var metadataReader = peFile.GetMetadataReader();

var assemblyReferences = metadataReader.AssemblyReferences;
Expand Down