pe: read exports of an image without data directories as empty #1595
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1602"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T14:46:13Z.
Change
exports()intools/pe_inventory.pyreadpe.OPTIONAL_HEADER.DATA_DIRECTORY[0]unconditionally. An optional header withNumberOfRvaAndSizes = 0is allowed by the format, and pefile then exposes an emptyDATA_DIRECTORYlist.exports()raisedIndexError, andtools/pe_package_preflight.py(which catches onlyOSError,ValueError,KeyErrorandPEFormatError) printed a raw traceback instead of a report.exports()now returns[]whenDATA_DIRECTORYis empty, the same result as the existingif not start: return []for an empty entry.import_tables()in the same file and_directory()intools/compat_report.pyalready length-check before indexing, so this was the one unguarded index._parsed_system_exports()also callsexports(), so it takes the same path; I did not exercise it separately.main()and its except clause are unchanged.The input is a constructed header (a zero directory count written into the synthetic image), not something seen in a real image.
Verification
Environment: Windows 11, Python 3.14.5, pefile from
requirements-dev.txt. Base:origin/maina3ac47a9.Before: the synthetic image from
tests/compat_report_check.py(build_image(POSITIVE_IMPORTS)) with the 4-byte count at e_lfanew + 4 + 20 + 108 set to 0 (pefile reportslen(DATA_DIRECTORY) == 0), written aspkg1/app.exe:python -B tools/pe_package_preflight.py pkg1 app.exeended withIndexError: list index out of range, raised at theDATA_DIRECTORY[0]line inexports.After: the same command exits 0 and prints a normal report for
app.exewith"exports": 0,"imports": [],"warnings": 2and nothing on stderr. Imports are empty because the image has no import directory entry either.New test
test_image_without_data_directories_has_no_exportsintests/pe_package_preflight_check.pybuilds the image in a temp package. As a control it asserts through pefile that the unpatched image has 16 directories andexports(pe) == [], and thatparse_imagestill returns imports. It then patches the count to 0 and asserts that pefile sees 0 directories,exports(pe) == [],parse_image(...)[0]["exports"] == [], and that the CLI run exits 0 with noTracebackin stderr andimages["app.exe"]["exports"] == 0in the JSON report.python -B tests/pe_package_preflight_check.py:Ran 14 tests ... OK (skipped=1)(the skip was already present).tools/pe_inventory.pyrestored toorigin/main, the same command givesFAILED (errors=1, skipped=1), withIndexError: list index out of rangeattools/pe_inventory.pyline 134 (theDATA_DIRECTORY[0]line), reached from the new test'sexports(pe)call on the patched image. With the fix restored it passes.tests/pe_package_ordinal_plan_check.py,tests/compat_report_check.py,tests/intake/paths_check.py,tests/intake/routing_check.py,tests/intake/import_forms_check.py,tests/bidi_controls_check.py.Rebased onto
origin/mainata3ac47a9before opening this pull request and re-run there:tests/pe_package_preflight_check.pyRan 14 tests, OK (skipped=1);tests/compat_report_check.py,tests/pe_package_ordinal_plan_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pyall pass; withtools/pe_inventory.pytaken fromorigin/mainin a scratch copy the new test errors with IndexError.Not run: CMake build and ctest;
_parsed_system_exportsandscan()against real system DLLs.Evidence and follow-up
No open upstream PR touches
tools/pe_inventory.py,tools/pe_package_preflight.pyortests/pe_package_preflight_check.py(re-checked withgh pr listafter the rebase). No issue is linked. The test is already registered in the workflow,AGENTS.mdand CMake.AI-assisted.