fix(libkernel): fail open of a file named with a trailing slash on Windows #5266
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5266"
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 DavidSamir on 2026-10-13T16:00:35Z.
What
On Windows,
sceKernelOpen,openandcreataccept a regular file named with a trailing slash.open("file/", O_WRONLY | O_TRUNC)andcreat("file/")empty the file, andopen("missing/", O_CREAT)creates a file calledmissing. On FreeBSD a name with a trailing slash must be a directory:lookupfails withENOTDIRwhen the path ends in a slash and the result is not a directory, and when the name does not exist and is being created it fails withENOENT("trailing slash only allowed for directories"). The Linux build fails in all of these cases too and leaves the file alone. The same happens forfile//,file/.andfile/./.Cause.
OpenNativepasses the resolved path, which keeps the trailing separator, toCreateFileW, and the Win32 open accepts a separator after a file's name: on this machineCreateFileW(L"c/", ...)on a regular filecopens it.file/.is the same path after the resolver normalizes it. This is the open-side twin of #5164 / #5165, which is aboutunlink.Observed on
maina252fcd75, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release.fileis a regular file of ten bytes andmissingdoes not exist:mainon WindowssceKernelOpen("file/", O_RDONLY), alsoO_WRONLY,O_RDWR,O_APPEND0x80020014(ENOTDIR)0x80020014sceKernelOpen("file/", O_WRONLY|O_TRUNC)0x80020014, file kept0x80020014, file keptsceKernelOpen("file/", O_CREAT|O_TRUNC),creat("file/")sceKernelOpen("file/", O_CREAT|O_EXCL)0x80020011(EEXIST)0x800200140x80020015(EISDIR)sceKernelOpen("missing/", O_WRONLY|O_CREAT),creat("missing/")missingcreatedsceKernelOpen("dir/", O_RDONLY)Where FreeBSD and Linux give different errors (the
O_CREATrows), this follows the FreeBSD lookup cited above.I have not found a title that stops on this. It came out of running one probe of the path functions on the Windows build and on the Linux build and comparing the two outputs.
Two commits:
fix(libkernel): fail open of a file named with a trailing slash on Windows:Open.cpp.test(libkernel): open names that end in a slash:guest_filesystem.Windows only, in
OpenNative, whichsceKernelOpen,open,creatandopenatreach. When the resolved path ends in a separator, the name without it is looked up before the host open: if it exists and is not a directory the call returnsENOTDIR; if it does not exist andO_CREATis set it returnsENOENT. Nothing is opened, truncated or created in those cases. The check runs only for paths that end in a separator, so an ordinary open makes the same host calls as before, and a directory named with a trailing slash opens as it does onmain.Not changed:
fopenopens its file itself inFileAndHeap.cppand still acceptsfile/(#4822 is changing its error handling);rename,truncate,utimesandlstatalso acceptfile/; and a path that runs through a regular file (file/x) still givesENOENTwhere FreeBSD givesENOTDIR. The check acts on the path the resolver returns; #4681 is about whether paths under a mount alias keep their trailing slash there.guest_filesystemgains checks next to the existingcreatones: for a file named with/,//,/.and/./,sceKernelOpenwithO_RDONLYand withO_WRONLY|O_TRUNCmust return0x80020014,openmust fail with errno 20,sceKernelOpenwithO_CREAT|O_TRUNCandcreatmust fail, and the file must keep its size;sceKernelOpenwithO_CREATandcreaton a missing name with a slash must fail and create nothing; and a directory with a trailing slash must open.Tested
Windows (
b5abbc1f0onmaina252fcd75, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):Open.cppas onmainand the test kept,guest_filesystemfails (exit 0xc0000409,Filesystem check failed at line 717). With the fix it passes 5/5 (--repeat until-fail:5), andguest_kernel_fcntl,guest_truncate,guest_dup,guest_file_vector,file_streams,guest_standard_streams,guest_posix_filesystem,guest_fdopendir,guest_kernel_renameandguest_path_casepass 3/3.main's libraries, the branch's and on the Linux build: twelve opens (sceKernelOpenwithO_RDONLY,O_WRONLY,O_RDWR,O_WRONLY|O_TRUNC,O_WRONLY|O_APPEND,O_WRONLY|O_CREAT,O_CREAT|O_TRUNC,O_CREAT|O_EXCLandO_DIRECTORY;openwithO_RDONLYand withO_WRONLY|O_CREAT|O_TRUNC;creat) on each of 13 names (a file, a directory and a missing name, each plain, with/and with/., plus a file in a subdirectory and a file reached through..), printing the result, the file's contents and whether the missing name now exists. Withmain's libraries 30 of the opens on a slash-ended name empty the file or create the missing one; with the branch's none do. The 60 lines for plain names and for directories are identical with both sets. Against the Linux build the branch still differs in two ways:sceKernelOpenreturns its error without setting errno, as onmain, and theO_CREATrows of the table.flock, 44 over pipe streams, 15 overfcntlstatus flags, 127 overfreopen, 130 over directory descriptors, 117 over streams used the other way, 144 over truncating opens, 164 overunlink, 313 overfdopenand 198 over paths that run through a regular file, run with both sets of libraries: the only lines that differ are two in the first probe and twelve in the last that open a file named with/or/., and a file size in thefdopenprobe that changes from run to run with either set.main's libraries and 77 to 78 µs with the branch's; a directory 81 to 98 and 77 to 85; a directory named with a trailing slash, the one case that makes the extra lookup, 85 to 99 and 86 to 96; a missing name 339 to 348 and 279 to 307. No slowdown I can separate from the spread.what()in either log and the same last kind of log line; SwanStationPS5 and PSXS5, whose logs are the longest, were run four times with each set (50 or 51 and 74 to 79 stderr lines, the spread being periodic statistics lines).ctest --parallel 4: 1074 of 1074 pass, as withmaina252fcd75built without this change.b30931231): full build, andguest_filesystem,guest_kernel_fcntl,guest_truncate,guest_posix_filesystemandfile_streamspass 3/3.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
libsbuilds, andguest_filesystemandguest_kernel_fcntlpass 3/3, so the new checks hold on the Linux build as it is. The changed code is Windows only.Not checked on a console or with a commercial title.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T04:42:02Z.
Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed