Skip to content

Commit 7a89210

Browse files
Throw BadImageFormatException when missing PE metadata (#6270)
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
1 parent c2e41ab commit 7a89210

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Tasks/AssemblyDependency/AssemblyInformation.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
@@ -361,6 +361,25 @@ private void CorePopulateMetadata()
361361
using (var stream = File.OpenRead(_sourceFile))
362362
using (var peFile = new PEReader(stream))
363363
{
364+
bool hasMetadata = false;
365+
try
366+
{
367+
// This can throw if the stream is too small, which means
368+
// the assembly doesn't have metadata.
369+
hasMetadata = peFile.HasMetadata;
370+
}
371+
finally
372+
{
373+
// If the file does not contain PE metadata, throw BadImageFormatException to preserve
374+
// behavior from AssemblyName.GetAssemblyName(). RAR will deal with this correctly.
375+
if (!hasMetadata)
376+
{
377+
throw new BadImageFormatException(string.Format(CultureInfo.CurrentCulture,
378+
AssemblyResources.GetString("ResolveAssemblyReference.AssemblyDoesNotContainPEMetadata"),
379+
_sourceFile));
380+
}
381+
}
382+
364383
var metadataReader = peFile.GetMetadataReader();
365384

366385
var assemblyReferences = metadataReader.AssemblyReferences;

0 commit comments

Comments
 (0)