Windows: guest fopen and freopen garble non-ASCII (UTF-8) file names #2329
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T15:41:51Z.
What
On Windows, the guest
fopenandfreopen(libc) garble file names that aren't ASCII. Guest paths are UTF-8, but both pass the resolved path to the narrow CRT asstd::string/const char*:fopen:std::fopen(abs_path.c_str(), ...)withabs_path = fpath.string()(core/libs/prx/libc/src/FileAndHeap.cpp, line 82);freopen:ResolvePath_nid_no_patch(filename).string()passed toFileStream::Reopen, which callsstd::freopen(filename, ...)(core/libs/prx/libc/include/FileStream.hpp, line 73).With MinGW's libstdc++,
path::string()itself yields UTF-8, but the narrow CRTfopen/freopenread the name in the ANSI code page, not UTF-8. A guest that writes/…/セーブ.datwithfopengets a host file namedセーブ.dat, andfopen(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
maind70b8998, 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:sceKernelOpen(O_CREAT)sceKernelMkdir,mkdirrename(libc),sceKernelRenameremove,sceKernelUnlink,accesson an existingセーブfopen(…, "w")セーブfreopen(…, "w", f)セーブfopen(…, "r")on an existingセーブEverything else goes through
std::filesystemor wide Win32 calls with thepathitself, which is why only these two are affected. libc'sopendir/readdirhad the same problem through MinGW's narrowdirent; 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)infopen, and_wfreopeninFileStream::Reopen(taking thestd::filesystem::pathinstead ofconst char*). A regression test can create and reopen au8"セーブ"file throughfopen_nid_postfixand check the host directory withstd::filesystem(wide), like the existingGuestFilesystemchecks.#1549, #2033 and #2170 all change the
fopenline, and #2170 also rewritesFileStream.hpp. As far as I can see none of them changes the encoding (#2170'sfopenstill callsstd::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.
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
_wfopenand freopen uses_wfreopenwith the path asstd::filesystem::pathon Windows; other platforms are unchanged. It reusesParseFileModefrom #2033. Thefile_streamstest has a new case (write, read back and freopen ofセーブé.tmp): on main it createsセーブé.tmpand 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_nanon an NVIDIA RTX 4080 SUPER, unrelated to libc, not run on an unmodified main), 5 skipped.tools/check_conventions.pypasses. 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.