pe: read exports of an image without data directories as empty #1595

Merged
RenzoWit35 merged 1 commit from upstream-pr/1602 into main 2026-11-24 14:56:08 +00:00
RenzoWit35 commented 2026-11-24 14:46:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T14:46:13Z.


Change

exports() in tools/pe_inventory.py read pe.OPTIONAL_HEADER.DATA_DIRECTORY[0] unconditionally. An optional header with NumberOfRvaAndSizes = 0 is allowed by the format, and pefile then exposes an empty DATA_DIRECTORY list. exports() raised IndexError, and tools/pe_package_preflight.py (which catches only OSError, ValueError, KeyError and PEFormatError) printed a raw traceback instead of a report.

  • exports() now returns [] when DATA_DIRECTORY is empty, the same result as the existing if not start: return [] for an empty entry. import_tables() in the same file and _directory() in tools/compat_report.py already length-check before indexing, so this was the one unguarded index.
  • _parsed_system_exports() also calls exports(), 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/main a3ac47a9.

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 reports len(DATA_DIRECTORY) == 0), written as pkg1/app.exe:

python -B tools/pe_package_preflight.py pkg1 app.exe ended with IndexError: list index out of range, raised at the DATA_DIRECTORY[0] line in exports.

After: the same command exits 0 and prints a normal report for app.exe with "exports": 0, "imports": [], "warnings": 2 and nothing on stderr. Imports are empty because the image has no import directory entry either.

New test test_image_without_data_directories_has_no_exports in tests/pe_package_preflight_check.py builds the image in a temp package. As a control it asserts through pefile that the unpatched image has 16 directories and exports(pe) == [], and that parse_image still 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 no Traceback in stderr and images["app.exe"]["exports"] == 0 in the JSON report.

  • python -B tests/pe_package_preflight_check.py: Ran 14 tests ... OK (skipped=1) (the skip was already present).
  • Negative control: with only tools/pe_inventory.py restored to origin/main, the same command gives FAILED (errors=1, skipped=1), with IndexError: list index out of range at tools/pe_inventory.py line 134 (the DATA_DIRECTORY[0] line), reached from the new test's exports(pe) call on the patched image. With the fix restored it passes.
  • Also pass: 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/main at a3ac47a9 before opening this pull request and re-run there: tests/pe_package_preflight_check.py Ran 14 tests, OK (skipped=1); tests/compat_report_check.py, tests/pe_package_ordinal_plan_check.py, tests/docs_links_check.py and tests/bidi_controls_check.py all pass; with tools/pe_inventory.py taken from origin/main in a scratch copy the new test errors with IndexError.

Not run: CMake build and ctest; _parsed_system_exports and scan() against real system DLLs.

Evidence and follow-up

No open upstream PR touches tools/pe_inventory.py, tools/pe_package_preflight.py or tests/pe_package_preflight_check.py (re-checked with gh pr list after the rebase). No issue is linked. The test is already registered in the workflow, AGENTS.md and CMake.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6584434719 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1602). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T14:46:13Z. --- ## Change `exports()` in `tools/pe_inventory.py` read `pe.OPTIONAL_HEADER.DATA_DIRECTORY[0]` unconditionally. An optional header with `NumberOfRvaAndSizes = 0` is allowed by the format, and pefile then exposes an empty `DATA_DIRECTORY` list. `exports()` raised `IndexError`, and `tools/pe_package_preflight.py` (which catches only `OSError`, `ValueError`, `KeyError` and `PEFormatError`) printed a raw traceback instead of a report. - `exports()` now returns `[]` when `DATA_DIRECTORY` is empty, the same result as the existing `if not start: return []` for an empty entry. `import_tables()` in the same file and `_directory()` in `tools/compat_report.py` already length-check before indexing, so this was the one unguarded index. - `_parsed_system_exports()` also calls `exports()`, 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/main` a3ac47a9. 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 reports `len(DATA_DIRECTORY) == 0`), written as `pkg1/app.exe`: `python -B tools/pe_package_preflight.py pkg1 app.exe` ended with `IndexError: list index out of range`, raised at the `DATA_DIRECTORY[0]` line in `exports`. After: the same command exits 0 and prints a normal report for `app.exe` with `"exports": 0`, `"imports": []`, `"warnings": 2` and nothing on stderr. Imports are empty because the image has no import directory entry either. New test `test_image_without_data_directories_has_no_exports` in `tests/pe_package_preflight_check.py` builds the image in a temp package. As a control it asserts through pefile that the unpatched image has 16 directories and `exports(pe) == []`, and that `parse_image` still 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 no `Traceback` in stderr and `images["app.exe"]["exports"] == 0` in the JSON report. - `python -B tests/pe_package_preflight_check.py`: `Ran 14 tests ... OK (skipped=1)` (the skip was already present). - Negative control: with only `tools/pe_inventory.py` restored to `origin/main`, the same command gives `FAILED (errors=1, skipped=1)`, with `IndexError: list index out of range` at `tools/pe_inventory.py` line 134 (the `DATA_DIRECTORY[0]` line), reached from the new test's `exports(pe)` call on the patched image. With the fix restored it passes. - Also pass: `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/main` at `a3ac47a9` before opening this pull request and re-run there: `tests/pe_package_preflight_check.py` Ran 14 tests, OK (skipped=1); `tests/compat_report_check.py`, `tests/pe_package_ordinal_plan_check.py`, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` all pass; with `tools/pe_inventory.py` taken from `origin/main` in a scratch copy the new test errors with IndexError. Not run: CMake build and ctest; `_parsed_system_exports` and `scan()` against real system DLLs. ## Evidence and follow-up No open upstream PR touches `tools/pe_inventory.py`, `tools/pe_package_preflight.py` or `tests/pe_package_preflight_check.py` (re-checked with `gh pr list` after the rebase). No issue is linked. The test is already registered in the workflow, `AGENTS.md` and CMake. AI-assisted.
upstream-sync closed this pull request 2026-11-24 15:02:17 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1595
No description provided.