Skip to content

rpf9dump: harden Util.Safe for cross-platform and Windows filenames - #2876

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
kirigayakazima:fix/rpf9dump-util-safe-names
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
kirigayakazima:fix/rpf9dump-util-safe-names

Conversation

@kirigayakazima

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RPF9DUMP-UTIL-SAFE-WINDOWS-NAMES (#2570 (comment))

What and why

Hardens Util.Safe(string s) in tools/rpf9dump/Util.cs against three filename edge cases when exporting archives/textures:

  1. Cross-platform Windows-illegal characters: previously only Path.GetInvalidFileNameChars() and control characters were replaced with '_'. On Linux/macOS that set only includes '\0' and '/', leaving <>:"/\|?* unsanitized across hosts. Util.Safe now sanitizes <>:"/\|?* uniformly everywhere.
  2. Trailing dots/spaces exposed by the 120-char limit: previously .Trim(' ', '.') ran only before [..Math.Min(120, v.Length)], so names like "a" * 119 + ".foo" or "a" + "." * 130 + "b" ended with a trailing . or ' ' after truncation (rejected/stripped on Windows). Util.Safe now trims trailing spaces/dots after truncation as well (returning "x" if empty).
  3. Windows reserved DOS device names: names whose base stem before '.' (trimmed of trailing spaces) is CON, PRN, AUX, NUL, COM1..COM9, or LPT1..LPT9 (case-insensitive) are prefixed with '_' and bounded to 120 chars without trailing dots/spaces.

Tested

Verified locally (Windows 11, .NET SDK 8.0.401, Python 3.14, no game data):

  • Red before fix: dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release failed on trailing dot after 120-char truncation.
  • Green after fix:
    • dotnet build tools/rpf9dump/rpf9dump.csproj -c Release: 0 warnings, 0 errors
    • dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release: all 7 test groups passed (Util.Safe filename tests passed.)
    • python -I tests/test_rpf9dump_exit_codes.py (with built RPF9DUMP_DLL): 10 passed
    • python -I scripts/check_no_game_data.py & git diff --check: clean

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main 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)

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanitising every component before Path.Combine, the post-truncation trim, and the reserved-name stem split (first ., spaces trimmed so CON .txt matches) all look right, and the nasty cases stay inert: ../../etc/passwd → _.._etc_passwd, ..\..\windows\system32 → _.._windows_system32, CON → _CON, foo./foo → foo, 300 chars → 120 with no trailing dot. COM0/COM10 are correctly allowed. Build clean, diagnostics all pass, test_rpf9dump_exit_codes.py 10/10.

Two real gaps:

  1. The headline cross-platform rule is untested. The explicit <>:"/\|?* list plus char.IsControl is now a strict superset of Windows' Path.GetInvalidFileNameChars(), so that call is dead code — and deleting the <>:"/\|?* clause leaves all tests green, because the diagnostics job runs only on windows-latest (.github/workflows/ci.yml:87, :140). Either drop the platform call so Safe is deterministic and the test becomes real, or run the diagnostics on a Unix runner too.
  2. The _ escape creates collisions. CON → _CON and a genuine _CON (likewise NUL.txt / _NUL.txt) map to the same file, and every exporter treats File.Exists as success (Program.cs:313, :327, :360, :157), so the loser is silently skipped and the manifest lists the same File/Dir twice. Prefer a non-colliding escape (CON_, %5F) or uniquify with the entry offset. The same class already exists for foo./foo, a:b/a*b/a_b, .../""/x, and truncation that drops the extension (119a + ".x" and 119a + ".y" both → 119a); index-prefixed call sites are immune, stem-derived directories are not.

Also worth fixing while here:

  • Truncation can split a surrogate pair: 119 × a + 😀 ends in a lone U+D83D. Cut on a code-point boundary.
  • The archive base name is never sanitised (Program.cs:70-71, outDir = Path.Combine(outRoot, Path.GetFileNameWithoutExtension(src))) — the one written component that skips Safe.
  • Minor: per-component 120 is bounded but the total path is not (\\?\ unused; nested models/<120>/<120>_tex/… can exceed MAX_PATH), and CONIN$ / CONOUT$ / COM¹ are still device names.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging (added to the merge queue). Head f2357fa merges cleanly with main c4ca576. Claim: RPF9DUMP-UTIL-SAFE-WINDOWS-NAMES (#2570 (comment)). Red/green: KelboLive review on this head (sanitising and reserved names checked case by case); tool-only change. I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto main c4ca576: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

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.

3 participants