Open
Description
Background and motivation
When PEReader
reads an image that does not have a PE header, it tries to read it as a COFF file. However, because COFF files do not have a signature, the reader will not immediately fail when reading files that are not COFF files, which has misleading behavior by not failing, and wastes memory to store section headers. This has affected both first-party (#112653 (comment)) and third-party users (#48419).
I am proposing to add an option to cause PEReader
to immediately throw if the image does not contain a PE header and not attempt to further read it.
API Proposal
namespace System.Reflection.PortableExecutable;
public enum PEStreamOptions
{
RequirePEHeader = 16,
}
API Usage
using var pe = new PEReader(File.OpenRead("MyFile.dll"), PEStreamOptions.RequirePEHeader);
Alternative Designs
- The name of the flag could be different.
- Should we also add more overloads to cover reading images from contiguous memory?
- Should we also add a
PEHeaders(Stream peStream, int size, bool isLoadedImage, bool requirePEHeader)
constructor overload?
Risks
The API might be too niche or too hard to discover. Users can do the PE signature validation themselves before creating the PEReader
; it's quite simple to implement.