{"record":{"id":"b6a58c6c229a2abb","repo":"QL-Win/QuickLook","slug":"dos-signature-not-found","errorCode":null,"errorMessage":"DOS signature not found.","messagePattern":"DOS signature not found\\.","errorType":"exception","errorClass":"PEImageParseException","httpStatus":null,"severity":"error","filePath":"QuickLook.Plugin/QuickLook.Plugin.PEViewer/PEImageParser/PEImage.cs","lineNumber":51,"sourceCode":"\n    /// <summary>\n    /// Gets the optional header of this PE image file.\n    /// </summary>\n    public ImageOptionalHeader OptionalHeader { get; private set; }\n\n    /// <summary>\n    /// Gets the collection of section headers and data of this PE image file.\n    /// </summary>\n    public ImageSection[] Sections { get; private set; }\n\n    private PEImage(byte[] originalImage)\n    {\n        OriginalImage = originalImage;\n\n        using BinaryReader reader = new(new MemoryStream(OriginalImage));\n\n        // MZ\n        if (reader.BaseStream.Length < 2) throw new PEImageParseException(0, \"DOS signature not found.\");\n        if (reader.ReadUInt16() != 0x5a4d) throw new PEImageParseException(0, \"DOS header not found.\");\n\n        // DOS Header\n        if (reader.BaseStream.Length - reader.BaseStream.Position < 64) throw new PEImageParseException((int)reader.BaseStream.Position, \"DOS header incomplete.\");\n\n        DosHeader = new()\n        {\n            LastPageSize = reader.ReadUInt16(),\n            PageCount = reader.ReadUInt16(),\n            RelocationCount = reader.ReadUInt16(),\n            HeaderSize = reader.ReadUInt16(),\n            MinAlloc = reader.ReadUInt16(),\n            MaxAlloc = reader.ReadUInt16(),\n            InitialSS = reader.ReadUInt16(),\n            InitialSP = reader.ReadUInt16(),\n            Checksum = reader.ReadUInt16(),\n            InitialIP = reader.ReadUInt16(),\n            InitialCS = reader.ReadUInt16(),","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/QL-Win/QuickLook/blob/cb5d9c429c81d9796fac469da2a68efb5626946d/QuickLook.Plugin/QuickLook.Plugin.PEViewer/PEImageParser/PEImage.cs#L33-L69","documentation":"First of the PE parsing guards in PEImage's constructor: if the byte array is shorter than 2 bytes there is no room even for the 'MZ' magic, so PEImageParseException(0, ...) is thrown. This is the earliest possible rejection — the file is too small to be any kind of executable.","triggerScenarios":"PEImage is constructed (e.g. via PEImage.FromFile or the byte[] constructor) with a buffer of length 0 or 1, so reader.BaseStream.Length < 2.","commonSituations":"An empty or near-empty file routed to the PE viewer; a 0/1-byte placeholder; a download that failed and produced an empty body; a file with a misleading .exe/.dll extension.","solutions":["Pre-check the file length >= 64 (the DOS header) before constructing PEImage.","Use a detector that reads the first 2 bytes ('MZ') before committing to PE parsing.","Catch PEImageParseException at the plugin boundary and report 'not a valid executable'.","Re-acquire the file if emptiness is unexpected."],"exampleFix":"// before\nif (reader.BaseStream.Length < 2) throw new PEImageParseException(0, \"DOS signature not found.\");\n\n// after — caller-side size guard before parsing\nif (new FileInfo(path).Length < 64) return null;","handlingStrategy":"validation","validationCode":"if (!File.Exists(path) || new FileInfo(path).Length < 64) return; // too small to be a PE","typeGuard":"static bool CouldBePe(long len) => len >= 64;","tryCatchPattern":"try { var img = PEImage.FromFile(path); }\ncatch (PEImageParseException ex) when (ex.Message.Contains(\"DOS signature\")) { /* not a PE */ }","preventionTips":["Gate PEViewer on a minimum size (>=64 bytes) and 'MZ' sniff.","Use a detector before constructing PEImage.","Catch PEImageParseException at the plugin boundary."],"tags":["pe","exe","file-size","binary-parsing","dos"],"backgroundTag":null,"analyzedSha":"cb5d9c429c81d9796fac469da2a68efb5626946d","analyzedAt":"2026-08-13T11:51:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}