Windows: guest fopen and freopen garble non-ASCII (UTF-8) file names #2329

Closed
opened 2026-10-09 15:41:51 +00:00 by reyani-dev · 1 comment
reyani-dev commented 2026-10-09 15:41:51 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T15:41:51Z.


What

On Windows, the guest fopen and freopen (libc) garble file names that aren't ASCII. Guest paths are UTF-8, but both pass the resolved path to the narrow CRT as std::string/const char*:

  • fopen: std::fopen(abs_path.c_str(), ...) with abs_path = fpath.string() (core/libs/prx/libc/src/FileAndHeap.cpp, line 82);
  • freopen: ResolvePath_nid_no_patch(filename).string() passed to FileStream::Reopen, which calls std::freopen(filename, ...) (core/libs/prx/libc/include/FileStream.hpp, line 73).

With MinGW's libstdc++, path::string() itself yields UTF-8, but the narrow CRT fopen/freopen read the name in the ANSI code page, not UTF-8. A guest that writes /…/セーブ.dat with fopen gets a host file named セーブ.dat, and fopen(name, "r") on an existing non-ASCII file returns NULL (ENOENT). So saves or caches with Japanese, Korean or accented names can be written and then never read back on Windows. Linux is unaffected.

Measured

main d70b8998, Windows 11 (ANSI code page 1252), WinLibs GCC 15.2.0 r7. For each call, the guest path was <dir>/セーブ (UTF-8), and I checked the host directory afterwards:

Guest call Host result
sceKernelOpen(O_CREAT) correct name
sceKernelMkdir, mkdir correct name
rename (libc), sceKernelRename correct name
remove, sceKernelUnlink, access on an existing セーブ work
fopen(…, "w") file created as セーブ
freopen(…, "w", f) file created as セーブ
fopen(…, "r") on an existing セーブ NULL (not found)

Everything else goes through std::filesystem or wide Win32 calls with the path itself, which is why only these two are affected. libc's opendir/readdir had the same problem through MinGW's narrow dirent; that one is in a file no open PR touches, so I've sent a fix separately.

Proposal

On Windows, open through the wide CRT with the path: _wfopen(fpath.c_str(), wideMode) in fopen, and _wfreopen in FileStream::Reopen (taking the std::filesystem::path instead of const char*). A regression test can create and reopen a u8"セーブ" file through fopen_nid_postfix and check the host directory with std::filesystem (wide), like the existing GuestFilesystem checks.

#1549, #2033 and #2170 all change the fopen line, and #2170 also rewrites FileStream.hpp. As far as I can see none of them changes the encoding (#2170's fopen still calls std::fopen(path.string().c_str(), …)). To avoid a fourth PR on the same line, I'm reporting it here; it can be folded into whichever lands first, or I can send it once they've settled.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5780763379 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2329). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T15:41:51Z. --- ### What On Windows, the guest `fopen` and `freopen` (libc) garble file names that aren't ASCII. Guest paths are UTF-8, but both pass the resolved path to the narrow CRT as `std::string`/`const char*`: - `fopen`: `std::fopen(abs_path.c_str(), ...)` with `abs_path = fpath.string()` (`core/libs/prx/libc/src/FileAndHeap.cpp`, line 82); - `freopen`: `ResolvePath_nid_no_patch(filename).string()` passed to `FileStream::Reopen`, which calls `std::freopen(filename, ...)` (`core/libs/prx/libc/include/FileStream.hpp`, line 73). With MinGW's libstdc++, `path::string()` itself yields UTF-8, but the narrow CRT `fopen`/`freopen` read the name in the ANSI code page, not UTF-8. A guest that writes `/…/セーブ.dat` with `fopen` gets a host file named `セーブ.dat`, and `fopen(name, "r")` on an existing non-ASCII file returns NULL (`ENOENT`). So saves or caches with Japanese, Korean or accented names can be written and then never read back on Windows. Linux is unaffected. ### Measured `main` `d70b8998`, Windows 11 (ANSI code page 1252), WinLibs GCC 15.2.0 r7. For each call, the guest path was `<dir>/セーブ` (UTF-8), and I checked the host directory afterwards: | Guest call | Host result | |---|---| | `sceKernelOpen(O_CREAT)` | correct name | | `sceKernelMkdir`, `mkdir` | correct name | | `rename` (libc), `sceKernelRename` | correct name | | `remove`, `sceKernelUnlink`, `access` on an existing `セーブ` | work | | **`fopen(…, "w")`** | file created as `セーブ` | | **`freopen(…, "w", f)`** | file created as `セーブ` | | **`fopen(…, "r")`** on an existing `セーブ` | NULL (not found) | Everything else goes through `std::filesystem` or wide Win32 calls with the `path` itself, which is why only these two are affected. libc's `opendir`/`readdir` had the same problem through MinGW's narrow `dirent`; that one is in a file no open PR touches, so I've sent a fix separately. ### Proposal On Windows, open through the wide CRT with the `path`: `_wfopen(fpath.c_str(), wideMode)` in `fopen`, and `_wfreopen` in `FileStream::Reopen` (taking the `std::filesystem::path` instead of `const char*`). A regression test can create and reopen a `u8"セーブ"` file through `fopen_nid_postfix` and check the host directory with `std::filesystem` (wide), like the existing `GuestFilesystem` checks. #1549, #2033 and #2170 all change the `fopen` line, and #2170 also rewrites `FileStream.hpp`. As far as I can see none of them changes the encoding (#2170's `fopen` still calls `std::fopen(path.string().c_str(), …)`). To avoid a fourth PR on the same line, I'm reporting it here; it can be folded into whichever lands first, or I can send it once they've settled. AI-assisted: yes.
xXPBSXx commented 2026-10-10 07:40:21 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-10T07:40:21Z.


A tested fix is on my fork, based on current main (3d8de32b):
https://github.com/xXPBSXx/AnyPS5/tree/fix/libc-fopen-wide-paths

fopen uses _wfopen and freopen uses _wfreopen with the path as std::filesystem::path on Windows; other platforms are unchanged. It reuses ParseFileMode from #2033. The file_streams test has a new case (write, read back and freopen of セーブé.tmp): on main it creates セーブé.tmp and fails, with the change it passes.

Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release: full ctest 644 tests, 1 failed (agc_driver_f32_arithmetic_nan on an NVIDIA RTX 4080 SUPER, unrelated to libc, not run on an unmodified main), 5 skipped. tools/check_conventions.py passes. Linux and titles not tested.

I opened it as #2588, which the intake rule closed automatically. If it is useful, someone can reopen it or use the branch. AI-assisted.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095211376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2329#issuecomment-6095211376). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-10T07:40:21Z. --- A tested fix is on my fork, based on current main (3d8de32b): https://github.com/xXPBSXx/AnyPS5/tree/fix/libc-fopen-wide-paths fopen uses `_wfopen` and freopen uses `_wfreopen` with the path as `std::filesystem::path` on Windows; other platforms are unchanged. It reuses `ParseFileMode` from #2033. The `file_streams` test has a new case (write, read back and freopen of `セーブé.tmp`): on main it creates `セーブé.tmp` and fails, with the change it passes. Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release: full ctest 644 tests, 1 failed (`agc_driver_f32_arithmetic_nan` on an NVIDIA RTX 4080 SUPER, unrelated to libc, not run on an unmodified main), 5 skipped. `tools/check_conventions.py` passes. Linux and titles not tested. I opened it as #2588, which the intake rule closed automatically. If it is useful, someone can reopen it or use the branch. AI-assisted.
Sign in to join this conversation.
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
boykopovar/AnyPS5#2329
No description provided.