{"record":{"id":"eb98476bac8ded4d","repo":"QL-Win/QuickLook","slug":"dos-header-incomplete","errorCode":null,"errorMessage":"DOS header incomplete.","messagePattern":"DOS header incomplete\\.","errorType":"exception","errorClass":"PEImageParseException","httpStatus":null,"severity":"error","filePath":"QuickLook.Plugin/QuickLook.Plugin.PEViewer/PEImageParser/PEImage.cs","lineNumber":55,"sourceCode":"    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(),\n            RelocationOffset = reader.ReadUInt16(),\n            OverlayNumber = reader.ReadUInt16(),\n            Reserved1 = reader.ReadUInt16(),\n            Reserved2 = reader.ReadUInt16(),","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/QL-Win/QuickLook/blob/cb5d9c429c81d9796fac469da2a68efb5626946d/QuickLook.Plugin/QuickLook.Plugin.PEViewer/PEImageParser/PEImage.cs#L37-L73","documentation":"Thrown when, after the 'MZ' mark, fewer than 64 bytes remain in the stream — insufficient to hold the full IMAGE_DOS_HEADER (which is 64 bytes). PEImageParseException records the current position. The file started as an executable but is too short to contain the DOS header it claims to need.","triggerScenarios":"reader.BaseStream.Length - reader.BaseStream.Position < 64 immediately after consuming the 2-byte 'MZ' magic — e.g. a 3–65 byte file, or a stream truncated just past the magic.","commonSituations":"A stub/sentinel file that begins with 'MZ' but is incomplete; a PE truncated during copy; a deliberately malformed 'MZ' file used as a marker; an SFX header fragment.","solutions":["Pre-check that the file is at least 64 bytes before constructing PEImage.","If the file is expected to be a real PE, re-acquire it — truncation here means it cannot be valid.","Catch PEImageParseException and report 'incomplete DOS header / not a valid PE'.","Validate download integrity (size/hash) before parsing."],"exampleFix":"// before\nif (reader.BaseStream.Length - reader.BaseStream.Position < 64) throw new PEImageParseException((int)reader.BaseStream.Position, \"DOS header incomplete.\");\n\n// after — single upfront size guard\nif (reader.BaseStream.Length < 64) return null;","handlingStrategy":"validation","validationCode":"if (new FileInfo(path).Length < 64) return; // cannot contain a full DOS header","typeGuard":"static bool HasFullDosHeader(long len) => len >= 64;","tryCatchPattern":"try { var img = PEImage.FromFile(path); }\ncatch (PEImageParseException ex) when (ex.Message.Contains(\"DOS header incomplete\")) { /* truncated PE */ }","preventionTips":["Pre-check file length >= 64 before parsing.","Validate download size/hash before opening.","Re-acquire truncated executables."],"tags":["pe","exe","dos","truncation","binary-parsing"],"backgroundTag":null,"analyzedSha":"cb5d9c429c81d9796fac469da2a68efb5626946d","analyzedAt":"2026-08-13T11:51:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}