Skip to content

fix(libc): return invalid argument error on null buffer in basename_r - #10074

Closed
voidlakhan wants to merge 1 commit into
boykopovar:mainfrom
voidlakhan:fix/libc-basename-r-null-buffer
Closed

voidlakhan wants to merge 1 commit into
boykopovar:mainfrom
voidlakhan:fix/libc-basename-r-null-buffer

Conversation

@voidlakhan

Copy link
Copy Markdown
Contributor

What

basename_r threw std::invalid_argument across the PRX ABI boundary when passed a null output buffer. Throwing C++ exceptions across guest PRX boundaries causes a host crash rather than reporting standard libc error status to the calling guest code.

This change:

  • Sets errno to EINVAL (22) and returns nullptr when bname is nullptr, following standard POSIX reentrant conventions and sibling error handling patterns.
  • Adds regression test coverage in GuestStrings.cpp asserting that basename_r(path, nullptr) returns nullptr with errno == 22.
  • Records technical debt in docs/dev/debt/libc.md documenting standard POSIX EINVAL behavior on null buffer in basename_r.

Tested

  • MinGW-w64 GCC (Windows 11): ninja -C build-full guest_string_tests compiled cleanly (0 warnings/errors).
  • ctest --test-dir build-full -R guest_strings -V: passed (1/1 tests passed in 0.04s).
  • python tools/check_conventions.py --base upstream/main: passed locally with 0 findings.
  • git diff --check upstream/main..HEAD: clean (0 whitespace or format errors).

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
  • 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

Set errno to EINVAL (22) and return nullptr when buffer is null in basename_r, following POSIX reentrant conventions and preventing host exception crashes on invalid guest input. Add regression test covering null buffer handling in GuestStrings and record technical debt.
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 9087 docs/dev/debt/libc.md L48-50
PR 9613 docs/dev/debt/libc.md L48

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/GuestStrings.cpp
PR 7544 docs/dev/debt/libc.md
PR 7648 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8417 docs/dev/debt/libc.md
PR 8863 docs/dev/debt/libc.md
PR 8965 core/libs/tests/GuestStrings.cpp
docs/dev/debt/libc.md
PR 9038 docs/dev/debt/libc.md
PR 9232 docs/dev/debt/libc.md
PR 9408 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9527 docs/dev/debt/libc.md
PR 9552 docs/dev/debt/libc.md
PR 9833 docs/dev/debt/libc.md
PR 9863 docs/dev/debt/libc.md
PR 9867 docs/dev/debt/libc.md
PR 9873 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 9973 docs/dev/debt/libc.md
PR 10002 docs/dev/debt/libc.md
PR 10003 docs/dev/debt/libc.md
PR 10007 docs/dev/debt/libc.md
PR 10222 docs/dev/debt/libc.md
PR 10226 docs/dev/debt/libc.md
PR 10229 docs/dev/debt/libc.md
PR 10250 docs/dev/debt/libc.md
PR 10253 docs/dev/debt/libc.md
PR 10274 docs/dev/debt/libc.md
PR 10315 docs/dev/debt/libc.md
PR 10345 docs/dev/debt/libc.md
PR 10453 docs/dev/debt/libc.md

@voidlakhan

voidlakhan commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

The flagged line collision in docs/dev/debt/libc.md is simply an independent technical-debt documentation entry added at the end of the file. There is zero export or functional overlap with #9087 or #9613:

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@voidlakhan Could you provide a console measurement or relevant public ABI source establishing EINVAL for basename_r with a null output buffer? FreeBSD 11.4 basename.c:25–59 dereferences that buffer without a null/EINVAL branch, and generic POSIX conventions do not establish this BSD extension’s result. Otherwise, retain the existing fail-fast behaviour and remove the new return-code expectation. Please also remove the unsupported standard-POSIX and always-host-crash claims.

@voidlakhan

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Thanks for the pointer to FreeBSD 11.4 basename.c. I verified that the implementation dereferences the output buffer without an EINVAL check, and I couldn't establish the console's behavior from an authoritative source. I've restored the existing fail-fast behavior and removed the unsupported return-code test and claims, so I'm closing this PR rather than asserting behavior without evidence.

@voidlakhan voidlakhan closed this Oct 24, 2026
@voidlakhan
voidlakhan deleted the fix/libc-basename-r-null-buffer branch October 24, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants