rpf9dump: harden segment bounds and CLI option validation (RPF9DUMP-SEGMENTS-AND-CLI) #656

Merged
kirigayakazima merged 2 commits from upstream-pr/656 into main 2026-12-01 11:57:37 +00:00
kirigayakazima commented 2026-11-24 11:06:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-24T11:06:03Z.


Claimed in the coordination issue as: RPF9DUMP-SEGMENTS-AND-CLI (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7129604877)

What and why

Follow-up hardening to merged #6344 (now on main) across tools/rpf9dump/:

  • tools/rpf9dump/Segments.cs (RecoveredResource.Read):
    • Prevent Int64 overflow in block.Offset + block.StoredSize > file.Length by checking block.Offset > file.Length || block.StoredSize > file.Length - block.Offset (matching RecoveryIndexCache's subtraction guard). Previously, e.g. (Offset=32, StoredSize=long.MaxValue) wrapped negative, passed the bounds check, and read a valid compressed stream starting at offset 32.
    • Reject raw blocks where block.IsRaw && block.StoredSize != block.Size with InvalidDataException instead of copying a partial or mismatched slice.
  • tools/rpf9dump/Program.cs:
    • Check unknown CLI options (argv[i] is not ("--start" or "--length")) before validating the value argument so bare --bogus or --bogus notanumber reports Unknown option --bogus. rather than claiming it needs a non-negative byte count., and emit both diagnostics on Console.Error.
    • Parse --jobs with NumberStyles.None, CultureInfo.InvariantCulture (consistent with --start / --length) so +2 / whitespace / locale digits are rejected cleanly with exit code 2.
  • tests/Rpf9DumpDiagnostics/Program.cs & tests/test_rpf9dump_exit_codes.py:
    • Add diagnostics cases for overflowing StoredSize and mismatched raw-block StoredSize != Size (both > and <).
    • Add exit-code tests for unknown options and invalid/signed/non-positive --jobs values leaving output directories uncreated.

(Keeps tests/test_rpf9dump_exit_codes.py lines 87–120 untouched so this merges cleanly alongside #655.)

Tested

On Windows 11 x64 (.NET 8.0.401, Python 3.14.7) at c28b21aa6151a3f572d4b8ab066c96fe8fe72f94, synthetic fixtures only (no game data):

  • dotnet build tools/rpf9dump/rpf9dump.csproj -c Release — 0 warnings, 0 errors
  • dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release — all 6 suites passed (including new overflow and raw-block mismatch checks)
  • Filtered python -I tests/test_rpf9dump_exit_codes.py -v ... (all 8 non-blocker tests, including both new CLI tests) — 8 passed, 0 failed
  • Full python -I tests/test_rpf9dump_exit_codes.py — 8 passed, 2 pre-existing failures from main (test_failed_xma_write_exits_nonzero_and_rerun_completes & test_failed_texture_write_exits_nonzero_and_withholds_manifest due to b05999eb's Util.PartialName; fixed separately in #655)
  • python -I tests/test_ttpf_export_resume.py — 4 ran, OK
  • python -I tests/test_ci_registration.py — 15 ran, OK
  • python -I tests/test_text_write_line_endings.py — 3 ran, OK
  • python -I tests/test_check_no_game_data.py & python -I scripts/check_no_game_data.py — OK / exit 0
  • python -m compileall -q make_game.py tools tests — exit 0

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main (21625bca) and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — rpf9dump only)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5470948418 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/656). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-24T11:06:03Z. --- Claimed in the coordination issue as: RPF9DUMP-SEGMENTS-AND-CLI (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7129604877) ### What and why Follow-up hardening to merged #6344 (now on `main`) across `tools/rpf9dump/`: - `tools/rpf9dump/Segments.cs` (`RecoveredResource.Read`): - Prevent `Int64` overflow in `block.Offset + block.StoredSize > file.Length` by checking `block.Offset > file.Length || block.StoredSize > file.Length - block.Offset` (matching `RecoveryIndexCache`'s subtraction guard). Previously, e.g. `(Offset=32, StoredSize=long.MaxValue)` wrapped negative, passed the bounds check, and read a valid compressed stream starting at offset 32. - Reject raw blocks where `block.IsRaw && block.StoredSize != block.Size` with `InvalidDataException` instead of copying a partial or mismatched slice. - `tools/rpf9dump/Program.cs`: - Check unknown CLI options (`argv[i] is not ("--start" or "--length")`) **before** validating the value argument so bare `--bogus` or `--bogus notanumber` reports `Unknown option --bogus.` rather than claiming it `needs a non-negative byte count.`, and emit both diagnostics on `Console.Error`. - Parse `--jobs` with `NumberStyles.None, CultureInfo.InvariantCulture` (consistent with `--start` / `--length`) so `+2` / whitespace / locale digits are rejected cleanly with exit code 2. - `tests/Rpf9DumpDiagnostics/Program.cs` & `tests/test_rpf9dump_exit_codes.py`: - Add diagnostics cases for overflowing `StoredSize` and mismatched raw-block `StoredSize != Size` (both `>` and `<`). - Add exit-code tests for unknown options and invalid/signed/non-positive `--jobs` values leaving output directories uncreated. *(Keeps `tests/test_rpf9dump_exit_codes.py` lines 87–120 untouched so this merges cleanly alongside #655.)* ### Tested On Windows 11 x64 (`.NET 8.0.401`, `Python 3.14.7`) at `c28b21aa6151a3f572d4b8ab066c96fe8fe72f94`, synthetic fixtures only (no game data): - `dotnet build tools/rpf9dump/rpf9dump.csproj -c Release` — 0 warnings, 0 errors - `dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release` — all 6 suites passed (including new overflow and raw-block mismatch checks) - Filtered `python -I tests/test_rpf9dump_exit_codes.py -v ...` (all 8 non-blocker tests, including both new CLI tests) — 8 passed, 0 failed - Full `python -I tests/test_rpf9dump_exit_codes.py` — 8 passed, 2 pre-existing failures from `main` (`test_failed_xma_write_exits_nonzero_and_rerun_completes` & `test_failed_texture_write_exits_nonzero_and_withholds_manifest` due to `b05999eb`'s `Util.PartialName`; fixed separately in #655) - `python -I tests/test_ttpf_export_resume.py` — 4 ran, OK - `python -I tests/test_ci_registration.py` — 15 ran, OK - `python -I tests/test_text_write_line_endings.py` — 3 ran, OK - `python -I tests/test_check_no_game_data.py` & `python -I scripts/check_no_game_data.py` — OK / exit 0 - `python -m compileall -q make_game.py tools tests` — exit 0 GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` (`21625bca`) and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — `rpf9dump` only)
PierreEbele commented 2026-11-25 02:47:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by PierreEbele on 2026-11-25T02:47:07Z.


Reviewed c28b21aa: read the full diff. The new bounds check (Offset > file.Length || StoredSize > file.Length - Offset) can no longer overflow; on main, Offset = 32, StoredSize = long.MaxValue wraps Offset + StoredSize negative and passes the check. The raw-size check also turns main's EndOfStreamException / silent short read for raw blocks with StoredSize != Size into a clean InvalidDataException. --jobs now parses with NumberStyles.None, so +2 is rejected, and unknown options are reported as unknown on stderr instead of as a byte-count error on stdout. Checked with a Python model of both sides, plus compileall and test_dotnet_rollforward.py (OK). I have no dotnet SDK here, so I couldn't run the .NET tests. No findings in the code.

One blocker: none of the new tests have run yet. In CI run 45381032677, check was cancelled at about 15 minutes, before the dedicated RPF9DUMP_DLL steps (.github/workflows/ci.yml:101-110). The generic test loop skipped all 10 test_rpf9dump_exit_codes.py tests because RPF9DUMP_DLL isn't set there. Please rebase or re-run so that check completes. Also, can you confirm the recovery scan only ever emits raw blocks with StoredSize == Size?

AI-assisted review.

Recommendation: Fix first

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7151183738 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/656#issuecomment-7151183738). Originally posted by [PierreEbele](https://github.com/PierreEbele) on 2026-11-25T02:47:07Z. --- Reviewed c28b21aa: read the full diff. The new bounds check (`Offset > file.Length || StoredSize > file.Length - Offset`) can no longer overflow; on main, `Offset = 32, StoredSize = long.MaxValue` wraps `Offset + StoredSize` negative and passes the check. The raw-size check also turns main's `EndOfStreamException` / silent short read for raw blocks with `StoredSize != Size` into a clean `InvalidDataException`. `--jobs` now parses with `NumberStyles.None`, so `+2` is rejected, and unknown options are reported as unknown on stderr instead of as a byte-count error on stdout. Checked with a Python model of both sides, plus `compileall` and `test_dotnet_rollforward.py` (OK). I have no dotnet SDK here, so I couldn't run the .NET tests. No findings in the code. One blocker: none of the new tests have run yet. In CI run 45381032677, `check` was cancelled at about 15 minutes, before the dedicated `RPF9DUMP_DLL` steps (`.github/workflows/ci.yml:101-110`). The generic test loop skipped all 10 `test_rpf9dump_exit_codes.py` tests because `RPF9DUMP_DLL` isn't set there. Please rebase or re-run so that `check` completes. Also, can you confirm the recovery scan only ever emits raw blocks with `StoredSize == Size`? AI-assisted review. **Recommendation:** Fix first
zmasi commented 2026-11-28 22:00:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T22:00:48Z.


Native Windows 11 + .NET SDK 8.0.428 Verification (c28b21aa61)

Verified c28b21aa6151a3f572d4b8ab066c96fe8fe72f94 merged cleanly onto origin/main (c7753ed98a56677f10e34078b37eab0540e190ec, git merge-tree --write-tree rc=0) on native Windows 11 (26200) + .NET SDK 8.0.428 + Python 3.12.10:

  • Red (origin/main c7753ed98a):
    • dotnet run --project tests/Rpf9DumpDiagnostics: failed with unhandled CLR exception (rc=0xE0434352 — Offset = 32, StoredSize = long.MaxValue overflowed block.Offset + block.StoredSize negative past the bounds check, and raw blocks with StoredSize != Size were not rejected).
    • python -I tests/test_rpf9dump_exit_codes.py (with Release rpf9dump.dll): FAILED (failures=5) out of 10 tests (--bogus reported a byte-count message on stdout instead of Unknown option --bogus. on stderr, and --jobs +2 was accepted).
  • Green (origin/main + c28b21aa61):
    • dotnet run --project tests/Rpf9DumpDiagnostics: 6/6 diagnostic groups passed (rc=0) (Model decode, Atomic write, Scan count, Mesh key, Recovered segment, Cached index).
    • python -I tests/test_rpf9dump_exit_codes.py (with Release rpf9dump.dll): 10/10 OK (rc=0) with zero output directories created on invalid CLI options.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246433241 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/656#issuecomment-7246433241). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T22:00:48Z. --- ### Native Windows 11 + `.NET SDK 8.0.428` Verification (`c28b21aa61`) Verified `c28b21aa6151a3f572d4b8ab066c96fe8fe72f94` merged cleanly onto `origin/main` (`c7753ed98a56677f10e34078b37eab0540e190ec`, `git merge-tree --write-tree` `rc=0`) on native **Windows 11 (`26200`) + `.NET SDK 8.0.428` + Python `3.12.10`**: - **Red (`origin/main` `c7753ed98a`)**: - `dotnet run --project tests/Rpf9DumpDiagnostics`: failed with unhandled CLR exception (`rc=0xE0434352` — `Offset = 32, StoredSize = long.MaxValue` overflowed `block.Offset + block.StoredSize` negative past the bounds check, and raw blocks with `StoredSize != Size` were not rejected). - `python -I tests/test_rpf9dump_exit_codes.py` (with Release `rpf9dump.dll`): **`FAILED (failures=5)`** out of `10` tests (`--bogus` reported a byte-count message on `stdout` instead of `Unknown option --bogus.` on `stderr`, and `--jobs +2` was accepted). - **Green (`origin/main + c28b21aa61`)**: - `dotnet run --project tests/Rpf9DumpDiagnostics`: **`6/6` diagnostic groups passed (`rc=0`)** (`Model decode`, `Atomic write`, `Scan count`, `Mesh key`, `Recovered segment`, `Cached index`). - `python -I tests/test_rpf9dump_exit_codes.py` (with Release `rpf9dump.dll`): **`10/10 OK` (`rc=0`)** with zero output directories created on invalid CLI options. AI-assisted.
OreoFR commented 2026-11-29 01:40:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T01:40:23Z.


Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, .NET SDK 11.0.100, main 53b77df14 : red = main / green = main + this PR

  • Method: red = main's code and main's rpf9dump (git submodule update --init, dotnet build tools/rpf9dump/rpf9dump.csproj -c Release, RPF9DUMP_DLL set to that build) with the test file as merged; green = main + git merge --no-ff of this PR (clean), rpf9dump built from the merged tree.
  • Note: this branch is behind main, so its own copy of the test file is older than main's. Running that stale copy on main gives extra failures unrelated to this PR (main already changed those tests), so red uses the merged test file to isolate the tests this PR adds.

python -I tests/test_rpf9dump_exit_codes.py:

  • Red: 10 run, 3 failures, 0 skips:
    • test_jobs_rejects_signed_or_nonpositive_values (case --jobs +2): main accepts +2 then crashes with an unhandled "Truncated RDS5 header" exception, exit -6 (expected 2).
    • test_unknown_option_reports_unknown_option_before_creating_output (cases --bogus and --bogus notanumber): "Unknown option --bogus" missing from stderr; main prints option errors to stdout, and the two-argument case hits the byte-count check first.
  • Green: 10 run, OK, 0 skips.
  • MEASURED: failing tests alone, 3 runs each side: red 3/3 fail, green 3/3 pass.

tests/Rpf9DumpDiagnostics (dotnet run --project tests/Rpf9DumpDiagnostics/... -c Release, as CI runs it):

  • Red: fails "an overflowing block size is rejected" (Program.cs:107, exit 134).
  • Green: passes all sections ("Recovered segment tests passed", "Cached index tests passed").

No test skipped on macOS.

Verdict: READY.

AI-assisted: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7248938746 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/656#issuecomment-7248938746). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T01:40:23Z. --- Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, .NET SDK 11.0.100, main 53b77df14 : red = main / green = main + this PR - Method: red = main's code and main's rpf9dump (`git submodule update --init`, `dotnet build tools/rpf9dump/rpf9dump.csproj -c Release`, `RPF9DUMP_DLL` set to that build) with the test file as merged; green = main + `git merge --no-ff` of this PR (clean), rpf9dump built from the merged tree. - Note: this branch is behind main, so its own copy of the test file is older than main's. Running that stale copy on main gives extra failures unrelated to this PR (main already changed those tests), so red uses the merged test file to isolate the tests this PR adds. `python -I tests/test_rpf9dump_exit_codes.py`: - Red: 10 run, 3 failures, 0 skips: - `test_jobs_rejects_signed_or_nonpositive_values` (case `--jobs +2`): main accepts `+2` then crashes with an unhandled "Truncated RDS5 header" exception, exit -6 (expected 2). - `test_unknown_option_reports_unknown_option_before_creating_output` (cases `--bogus` and `--bogus notanumber`): "Unknown option --bogus" missing from stderr; main prints option errors to stdout, and the two-argument case hits the byte-count check first. - Green: 10 run, OK, 0 skips. - MEASURED: failing tests alone, 3 runs each side: red 3/3 fail, green 3/3 pass. `tests/Rpf9DumpDiagnostics` (`dotnet run --project tests/Rpf9DumpDiagnostics/... -c Release`, as CI runs it): - Red: fails "an overflowing block size is rejected" (`Program.cs:107`, exit 134). - Green: passes all sections ("Recovered segment tests passed", "Cached index tests passed"). No test skipped on macOS. **Verdict: READY.** AI-assisted: Claude Code (Claude Opus 5.5).
kirigayakazima commented 2026-12-01 11:18:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-12-01T11:18:24Z.


Merged upstream/main (3ee508b46) into this branch at b13b6b3d3 (no rebase, no force-push) so fresh CI runs on current main.

Thanks @PierreEbele, @zmasi, and @OreoFR for the detailed review and verification! Regarding @PierreEbele's question:

  • Yes, the recovery scan only ever emits raw segments with StoredSize == Size: Rpf9TextureRecovery (third_party/rpf9-explorer/Core/Rpf9TextureRecovery.cs) never emits IsRaw segments, and the only place IsRaw = true is set is Rpf9ModelRecovery.TryRawTail (third_party/rpf9-explorer/Core/Rpf9ModelRecovery.cs:237,261), where Packed equals tail.Length (Data.Length) and RecoverAtOffsets (:99) constructs new ArchiveSegment(block.Offset, block.Packed, block.Data.Length) { IsRaw = block.IsRaw }. Both ZlibSegmentsStream.cs:18 and RecoveryIndexCache.cs:114 also enforce segment.StoredSize == segment.Size when segment.IsRaw is true.

Verification at b13b6b3d3 (Windows 11, .NET SDK 8.0.401, Python 3.12, no game data)

  • dotnet build tools/rpf9dump/rpf9dump.csproj -c Release: 0 warnings, 0 errors
  • dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release: 6/6 diagnostic groups passed (Model decode, Atomic write, Scan count, Mesh key, Recovered segment, Cached index)
  • python -I tests/test_rpf9dump_exit_codes.py (with RPF9DUMP_DLL pointing to the Release build): 10/10 OK (0 skipped)
  • python -I tests/test_text_write_line_endings.py: 3 tests OK
  • python -I scripts/check_no_game_data.py & git diff --check upstream/main...HEAD: clean (exit 0)

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7309457634 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/656#issuecomment-7309457634). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-12-01T11:18:24Z. --- Merged `upstream/main` (`3ee508b46`) into this branch at `b13b6b3d3` (no rebase, no force-push) so fresh CI runs on current `main`. Thanks @PierreEbele, @zmasi, and @OreoFR for the detailed review and verification! Regarding @PierreEbele's question: - **Yes**, the recovery scan only ever emits raw segments with `StoredSize == Size`: `Rpf9TextureRecovery` (`third_party/rpf9-explorer/Core/Rpf9TextureRecovery.cs`) never emits `IsRaw` segments, and the only place `IsRaw = true` is set is `Rpf9ModelRecovery.TryRawTail` (`third_party/rpf9-explorer/Core/Rpf9ModelRecovery.cs:237,261`), where `Packed` equals `tail.Length` (`Data.Length`) and `RecoverAtOffsets` (`:99`) constructs `new ArchiveSegment(block.Offset, block.Packed, block.Data.Length) { IsRaw = block.IsRaw }`. Both `ZlibSegmentsStream.cs:18` and `RecoveryIndexCache.cs:114` also enforce `segment.StoredSize == segment.Size` when `segment.IsRaw` is true. ### Verification at `b13b6b3d3` (Windows 11, `.NET SDK 8.0.401`, Python 3.12, no game data) - `dotnet build tools/rpf9dump/rpf9dump.csproj -c Release`: **0 warnings, 0 errors** - `dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release`: **6/6 diagnostic groups passed** (`Model decode`, `Atomic write`, `Scan count`, `Mesh key`, `Recovered segment`, `Cached index`) - `python -I tests/test_rpf9dump_exit_codes.py` (with `RPF9DUMP_DLL` pointing to the Release build): **10/10 OK** (0 skipped) - `python -I tests/test_text_write_line_endings.py`: **3 tests OK** - `python -I scripts/check_no_game_data.py` & `git diff --check upstream/main...HEAD`: clean (exit 0) AI-assisted.
aslan2142 commented 2026-12-01 11:33:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:33:13Z.

Review: APPROVED


Reviewed the diff against main: tools/rpf9dump/Segments.cs:110-114 cleanly fixes integer overflow in the segment-bounds check and rejects mismatched raw StoredSize != Size, tools/rpf9dump/Program.cs:75-97 validates unknown options and --jobs with NumberStyles.None before creating output, and tests in tests/Rpf9DumpDiagnostics/Program.cs and tests/test_rpf9dump_exit_codes.py cover all edge cases. Clean-room verified. Approving; awaiting completion of in-progress CI checks before merge.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130381282 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/656#pullrequestreview-6130381282). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:33:13Z. **Review:** APPROVED --- Reviewed the diff against `main`: `tools/rpf9dump/Segments.cs:110-114` cleanly fixes integer overflow in the segment-bounds check and rejects mismatched raw `StoredSize != Size`, `tools/rpf9dump/Program.cs:75-97` validates unknown options and `--jobs` with `NumberStyles.None` before creating output, and tests in `tests/Rpf9DumpDiagnostics/Program.cs` and `tests/test_rpf9dump_exit_codes.py` cover all edge cases. Clean-room verified. Approving; awaiting completion of in-progress CI checks before merge. *(AI-assisted review.)*
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
ProjectLeonida/mandy!656
No description provided.