Skip to content

fix(libc): check PATH_MAX before the segment in realpath, as realpath1 does - #24242

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:fix/libc-realpath-path-max-before-segment
Nov 20, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:fix/libc-realpath-path-max-before-segment

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

realpath resolved a path that FreeBSD refuses with ENAMETOOLONG: an empty, . or .. component that follows a resolved path of 1023 characters.

The debt entry says the function follows FreeBSD 11's realpath.c. In realpath1 the separator is appended, and its room checked, before the component is looked at:

		if (resolved[resolved_len - 1] != '/') {
			if (resolved_len + 1 >= PATH_MAX) {
				errno = ENAMETOOLONG;
				return (NULL);
			}
			resolved[resolved_len++] = '/';
			resolved[resolved_len] = '\0';
		}
		if (next_token[0] == '\0') {
			/* Handle consequential slashes. */
			continue;
		} else if (strcmp(next_token, ".") == 0) {
			continue;
		} else if (strcmp(next_token, "..") == 0) {

The port had the two in that order until #21264 (5f6ae93) moved the length check below the continues, so that it only applied to the remaining components. This PR puts it back.

With hop1 a link whose target resolves to a 1023-character path D (the case the existing test builds):

Path FreeBSD realpath1 before
hop1, hop1/ D D
hop1//, hop1/., hop1/./ null, ENAMETOOLONG, resolved holds D D
hop1/.. null, ENAMETOOLONG, resolved holds D the parent of D
hop1/../sub null, ENAMETOOLONG, resolved holds D D
hop1/x null, ENAMETOOLONG null, ENAMETOOLONG

The FreeBSD column is not read off the source only: I compiled the body of realpath1 from stable/11 against a small simulated file system holding the same links and ran the eight paths.

Tested

Windows 11 IoT Enterprise LTSC 2024 (10.0.26100), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

  • core/libs/tests/GuestRealpath.cpp (guest_realpath, already registered, no CMake change): the five lines that expected a result for hop1//, hop1/., hop1/./, hop1/.. and hop1/../sub now expect a null result, errno 63 and the 1023-character path left in the output buffer, as for hop1/x; hop1 and hop1/ are added and still resolve; the two path lengths the case relies on (1019 and 1023) are asserted.
  • Red: with the tests only, on main e825fa1492, guest_realpath aborts.
  • Green: with the fix, guest_realpath passes, and so do guest_path_case, guest_path_drive_component, guest_path_name_length, guest_filesystem and guest_filesystem_error. I did not rebuild the rest of the libc suite.
  • python tools/check_conventions.py --base origin/main passes.
  • python tools/pr_overlap.py --names realpath: no open pull request. python tools/pr_overlap.py --branch fix/libc-realpath-path-max-before-segment: no overlap with open pull requests.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

…1 does

FreeBSD's realpath1 appends the separator, and fails with ENAMETOOLONG
when the resolved path has no room for it, before it looks at the next
component. So an empty, "." or ".." component after a 1023-character
resolved path fails. Since 5f6ae93 the port ran that check only for
the other components and resolved those paths.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added the libc label Nov 20, 2026

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against b7394815f7:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_realpath_tests built cleanly.
  • CTest (guest_realpath):
    1/1 Test #1311: guest_realpath ...................   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/RealPath.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1311: guest_realpath ...................Subprocess aborted***Exception:   1.17 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked RealPath.cpp:ResolveGuest length check: result.size() > 1 && result.size() + 1 >= PathMax correctly mirrors FreeBSD 11 realpath1 separator pre-check before consuming next token
    • Verified error code semantics: NameTooLong (63, ENAMETOOLONG) returned on overflow, matching FreeBSD realpath1 and POSIX specs
    • Checked edge cases where result is root ('/') or length 1, ensuring no false-positive trigger since result.size() > 1 guard is present
    • Verified GuestRealpath.cpp test additions: assertions properly verify both successful resolution (hop1, hop1/) and boundary failure with errno 63 and preserved resolved prefix for //, /., /./, /.., /../sub, /x
    • Checked memory safety, pointer validation, and AGENTS.md rules (no comments, conventional commits, clean diff)

Looks Good To Me

@MarcZX10 MarcZX10 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.

Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head b7394815f716eb4846471813eebdc3da8affd488 using the prepared diff and repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and source review passed Changed behavior and tests inspected
Automated checks not run Author-reported results were not rerun

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author's reported Windows build and test results were not independently verified. No files were excluded by the manifest. Read both changed files in full: core/libs/prx/libc/src/RealPath.cpp and core/libs/tests/GuestRealpath.cpp.

To get this to a good state

Nothing is required.

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head b7394815f7, applied with git apply on main 86d2ba2 together with the other seven PRs of one batch (#24262, #24225, #24224, #24216, #24145, #24276, #24300). All eight applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon".

test RTX 3080
guest_realpath (with the reworked PATH_MAX cases) pass

Whole batch: the full ctest -j 12 (6496 tests) on the RTX 3080 has 6 failures. Four also fail on main 86d2ba2 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). agc_driver_store_over_untracked failed once in 12 runs alone (known as flaky in #22670). agc_driver hit the 120 s ctest timeout under -j 12 and passes alone in 23 to 39 s. The 2097 agc_/video tests on the iGPU fail only agc_driver_stencil_plane_memory.

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@ge9x0

ge9x0 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Linux run (the Tested section lists Windows only).

Setup: Fedora 44, kernel 7.3.4, GCC 16.2.1, glibc from F44, Release. Head b7394815f7 merged with --no-ff onto main e481b13555 (clean); the baseline is e481b13555 in the same build directory.

Result: guest_realpath: all passed on the merge (1/1). The same pattern on main passed as well (1 of these exist there), and the whole pattern run (tests of this and 14 other PRs) had no failures on either arm.

AI-assisted: yes

@oneandonlydean
oneandonlydean merged commit e77a782 into boykopovar:main Nov 20, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants