Skip to content

pe_inspect: name the header a short file stops before, not a missing signature - #2557

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
josevictorp:josevictorp/leonida-pe-inspect-mz-curto
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
josevictorp:josevictorp/leonida-pe-inspect-mz-curto

Conversation

@josevictorp

@josevictorp josevictorp commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

New BUILD item, claimed here.

What was wrong

tools/pe_inspect.py checked a signature and a length in one condition and reported both as the missing signature:

File Before Now
starts with MZ, shorter than 64 bytes (a truncated copy) no MZ header file is N bytes, shorter than the 64-byte MS-DOS header
e_lfanew points at or past the end, or the file is cut inside the PE header (even right after PE\0\0) no PE signature file is N bytes, too short to hold the PE header at 0x…
no MZ at the start (including empty and 1-byte files) no MZ header unchanged
long enough, wrong bytes where PE\0\0 should be no PE signature unchanged

The first two sent the reader looking for a wrong signature when bytes were missing. Nothing else in the parser changes; every case is still a PeFormatError and exit 1 from the CLI.

Overlap

#2456 (@Dom-in, run-time load candidates) touches the same two files elsewhere; git merge-tree of the two branches is clean.

Tested

Linux 6.8, Python 3.12.3, CPU only; no GPU, no game files.

  • python -I tests/test_pe_inspect.py: 50 tests: 49 pass, 1 Windows-only skip. The new ShortHeaderTest (6 tests, hand-written bytes) checks both PeImage and the CLI.
  • Negative control: with tools/pe_inspect.py from main, 4 of the 6 fail (the other 2 assert the unchanged messages).
  • tests/test_pe_shader_containers.py and tests/test_bad_input_errors.py still pass.

AI-assisted: written by Qwen (model qwen3.8-gx10-claude-compatible), reviewed and re-run with Claude Code.

…signature

A 4-byte truncated copy that starts with MZ was rejected as "no MZ header",
but the signature is there: the file is just shorter than the 64-byte MS-DOS
header that carries e_lfanew at 0x3c. The same class of wrong cause sat one
check later, where a cut before or inside the PE header said "no PE signature"
even when PE\0\0 was present and only the bytes after it were missing. The
signature and the length are now checked apart, each reporting its real cause.
Tested with the new ShortHeaderTest (hand-built bytes, parser and CLI paths):
4 of its 6 cases fail without the parser change; the suite runs 50 tests, all
passing, 1 Windows-only skip.

AI-assisted: written by Qwen (model qwen3.8-gx10-claude-compatible).
Co-Authored-By: Qwen qwen3.8-gx10-claude-compatible <noreply@qwen.ai>
@veloraxxx

Copy link
Copy Markdown
Contributor

Tested e739047d4da8212463454fd5b0dc0e239151b6dd on Windows with Python 3.10.11 (CPU-only). All checks passed.

I checked PeImage and the CLI entry point (main, with in-memory input) against 147 synthetic malformed images. The parser raised PeFormatError, and the CLI returned status 1 with the expected diagnostic in every case. Compared with base 49475796, 136 truncation cases now name the incomplete header, and 11 other malformed-input cases retain their original diagnostics. A valid synthetic PE32+ image parsed on both revisions.

AI-assisted verification.

@TurboSosiska304

Copy link
Copy Markdown

Verified head e739047 on Linux (CachyOS), Python 3.14.10, CPU only, hand-written bytes only.

Ran: python3 -I tests/test_pe_inspect.py -> 50 run, 1 skipped ("system DLL classification needs Windows"), OK; python3 -I tests/test_pe_shader_containers.py -> 9 OK; python3 -I tests/test_bad_input_errors.py -> 5 OK; python3 -m compileall -q tools tests -> ok.

Own probes through PeImage and the CLI (each checked for exit 1, one not a PE image (...) stderr line, no traceback, report dir still created):

  • 0/1/4-byte and M only: "no MZ header".
  • MZ at 2, 4, 16, 63 bytes: new length message with the right count.
  • 64-byte MZ, e_lfanew 0x3C/0x40/0x1000/0x7FFFFFFF/0xFFFFFFFF: "too short to hold the PE header at 0x…", no overflow, no "no PE signature".
  • 64-byte MZ with e_lfanew 0: "no PE signature" (offset is inside the file, correctly unchanged).
  • 87 bytes: short at 0x40; 88 bytes with XX\0\0: "no PE signature"; 68 bytes with PE\0\0 then cut: short at 0x40.

The same probes on origin/main reproduce the before-column exactly. No issues found; only gap is that e_lfanew 0 is not in the new tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants