Skip to content

fix(libc): recover representable snprintf counts at the host limit - #26118

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
IvanFogel:fix/20261123/snprintf-host-count-limit
Nov 24, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
IvanFogel:fix/20261123/snprintf-host-count-limit

Conversation

@IvanFogel

@IvanFogel IvanFogel commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

Recover a representable guest snprintf result when the host returns EOVERFLOW. Both snprintf and vsnprintf now share the bounded-output path. It saves the argument list before calling the host and retries that one error through the existing guest formatter, which can count the full output while writing only the bytes the caller requested. A total above INT_MAX still returns −1 with EOVERFLOW. Other host errors pass through. Formats with guest-specific conversions, including %n, already take the guest path before the host call.

Issue #26105 reports that the macOS host rejects a result of exactly INT_MAX, which the guest accepts. That makes guest_formatting fail at capacities 0, 1 and 16. The output tests now cover count-only, terminator-only, visible prefix plus padding, and the va_list entry point with a nonliteral width.

Tested

Linux x86-64: guest_formatting passed using the real host libc. I also ran the same CTest with a test-only host vsnprintf replacement that consumes the arguments and returns EOVERFLOW for exactly INT_MAX. The unmodified branch failed at the boundary test. The patch passed. An independent public-export check exercised a representable aggregate count (INT_MAX, written prefix “X ”), true overflow (−1 with EOVERFLOW), a different host error (−1 with EILSEQ), and a single %n write.

The MinGW-w64 build passed guest_formatting under Wine. The hosted native Windows job also passed guest_formatting on PR head 476e2e3b32 and passed all three tests it selected. Windows CI log.

#26105 originally reported the failure on macOS 27.0, Apple M4, in an x86-64 Release build under Rosetta 2 with #17425 and #11049 applied.

DiogoPCastelos independently reproduced the failure on main at f0b680ce06 on macOS 27.2.1, Apple M3, in an x86-64 Release build under Rosetta 2. With this PR’s commit applied, guest_formatting passed three consecutive runs. The same build also passed guest_wide_formatting, guest_sprintf_s_constraints, guest_avplayer_native_printf and guest_http_dbg_printf. Native macOS test report.

The overlap check listed older shared-file branches. I checked #11332's narrow field limits, #9087's secure formatting and scanning constraints, and the scanning changes in #9340, #18380 and #18405. Their bounded snprintf implementations still delegate to the host without handling its EOVERFLOW result.

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: Fixes macOS: snprintf returns -1 with EOVERFLOW for a result of exactly INT_MAX, so guest_formatting fails #26105
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@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 change; the targeted source review supports its stated handling of representable counts and true overflow.

What I ran

Reviewed head 476e2e3b32e3754aeefa78182a03a14f6c164baa. Read the prepared description, commits, manifest and diff, then the changed formatting functions, their guest formatter implementation, and the modified formatting tests using repo_read.py. No tests or builds were run.

Results

Check Result Note
Source review passed Both bounded entry points share the retry path.
Tests/build not run No commands were run.

Findings

Blocking

None.

Non-blocking

None.

Not verified

The complete texts of both changed files were available and read. files.json was unavailable through the reader; the manifest lists two changed files, both included and reviewed. Runtime behavior, test results reported in the PR description, and native macOS behavior were not independently verified.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot added libc fix error Closes an issue (Fixes #N) labels Nov 23, 2026
@DiogoPCastelos

Copy link
Copy Markdown
Contributor

Native macOS check, as the description asks: macOS 27.2.1, Apple M3, x86-64 Release under Rosetta 2, main f0b680c with this PR's commit applied. guest_formatting fails on main ("Formatting check failed", as in #26105) and passes with this PR in 3 of 3 runs. guest_wide_formatting, guest_sprintf_s_constraints, guest_avplayer_native_printf and guest_http_dbg_printf also pass with it.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

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 9087 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
PR 9340 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
PR 11332 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
PR 13589 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp

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

Labels

fix error Closes an issue (Fixes #N) libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: snprintf returns -1 with EOVERFLOW for a result of exactly INT_MAX, so guest_formatting fails

4 participants