Skip to content

fix(libc): convert a wide printf %c like btowc, as titles' libc.prx does - #9964

Merged
oneandonlydean merged 4 commits into
boykopovar:mainfrom
JJenkx:windows/wprintf-narrow-char
Nov 4, 2026
Merged

oneandonlydean merged 4 commits into
boykopovar:mainfrom
JJenkx:windows/wprintf-narrow-char

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What

The wide printf family wrote a narrow %c (no l, or with h/hh) as the argument's low byte taken as a code unit, so 0xE9 became U+00E9. The libc.prx that titles ship converts it the way its btowc does in the UTF-8 C locale (#8855 measures the same btowc):

Call Bundled libc.prx main
x%cy of 0x80 -1, EILSEQ, buffer x 3: x, U+0080, y
[%3c] of 0xE9 -1, errno unchanged, buffer [ 5
%lc|%c of U+20AC, 0xFF -1, EILSEQ, buffer €| 4
[%c] of -1 -1, errno unchanged 3, with U+00FF

This builds on the WideEncodingError and boolean AppendUtf16 decoder merged in #9953. The single error class retains main's message and partial output; its discriminator distinguishes %c failures that preserve errno. %lc and %C retain main's behavior.

Tested

Current head c22807ee5f65c395d0cca0d544a35d2bd669541f merges main 4f64f81ef7b4bca975b4b9f3dfd15dfff56f716f into the existing PR, as requested. No rebase or force-push.

Linux, Release, independent fresh source/dependency checkout and configuration; built guest_wide_formatting_tests, guest_formatting_tests and guest_stdio_variadic_tests with their full libc dependency. guest_wide_formatting, guest_formatting, guest_stdio_variadic and public_headers pass 4/4, zero skips.

Negative control on prior merge 2c8453337e (candidate formatting source and fixture, and main control source, unchanged in this refresh): the same frozen wide-formatting executable fails at %c of a continuation byte fails with EILSEQ with only FormattingWide.cpp replaced by current main's version. Restoring the candidate rebuilds libc byte-for-byte identically; the four checks pass again. Conventions, whitespace and clean-worktree checks pass.

Linux logs, JUnit, resolved diff and hashes, SHA-256 a6ec6ec866ade26fd53db504dcdd0c4d9514463ebc5ad6c17a3d881b80d2cce9.

Earlier reference measurements on Windows 11/WinLibs GCC15.2 at head 7868aaf64a against #9953 head 8bffaa164a: 44 %c cases match the bundled libc.prx (24 differences on the old main), and a 349,085-format sweep has no differences on PPSA07923/PPSA05916/PPSA13934 (2,843 old-main %c differences). These remain historical measurements of the byte-conversion rule, not a new Windows run of this merge. Console execution and streamed/secure %c error output remain unverified in debt.

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 (CPU-only libc change)
  • 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 the exact PR head 7868aaf against its declared dependency, #9953 at 8bffaa1. The implementation and tests cover the measured title behavior for narrow %c, including low-byte conversion, partial swprintf output, and the observed EILSEQ cases; I found no blocking issue in the diff.

I also overlaid the exact PR versions of FormattingWide.cpp and GuestWideFormatting.cpp (plus the debt update) into the existing configured Linux build tree, rebuilt guest_wide_formatting_tests, and ran ctest -R '^guest_wide_formatting$': 1/1 passed. This reused the existing build configuration and prerequisites rather than a fresh PR checkout. git diff --check and the repository convention checker against #9953 passed. Hosted Linux and Windows product jobs are skipped; the author's Windows differential and repeated focused-suite results are recorded in the PR description.

@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 14588 docs/dev/debt/libc.md L89-91

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 core/libs/prx/libc/src/FormattingWide.cpp
core/libs/tests/GuestWideFormatting.cpp
docs/dev/debt/libc.md
PR 6819 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10987 core/libs/tests/GuestWideFormatting.cpp
docs/dev/debt/libc.md
PR 11260 docs/dev/debt/libc.md
PR 11262 docs/dev/debt/libc.md
PR 11266 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11632 docs/dev/debt/libc.md
PR 11634 docs/dev/debt/libc.md
PR 11724 docs/dev/debt/libc.md
PR 11726 docs/dev/debt/libc.md
PR 11831 docs/dev/debt/libc.md
PR 12560 docs/dev/debt/libc.md
PR 12682 docs/dev/debt/libc.md
PR 12688 docs/dev/debt/libc.md
PR 12700 docs/dev/debt/libc.md
PR 12965 docs/dev/debt/libc.md
PR 13390 docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md
PR 14585 docs/dev/debt/libc.md
PR 14610 docs/dev/debt/libc.md

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 25, 2026
@JJenkx
JJenkx force-pushed the windows/wprintf-narrow-char branch from 2e3e810 to ef2bee9 Compare October 27, 2026 00:18
@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Conflict refresh, 2026-10-27 UTC: exact head ef2bee984eff7fe9675a80a68de4548f44f228af on tested main 74acfad65b3f0fd4eab653f3a3989d298fba1157. Retains refreshed #9953 as its prerequisite; the own wide %c production patch is unchanged.

Linux Release focused libraries/executables rebuilt. Three groups (wide formatting, narrow formatting, public headers) each pass three times: 9 initial and 9 restored passes. Restoring only the #9953 parent production file fails the intended wide-formatting group; restored libc is byte-identical. Zero skips; conventions and whitespace pass. This is focused Linux evidence, without new Windows, console, title, GPU or full-suite validation. Earlier measurements retain their original exact pins.

Source/dependency pins, production preservation, verbose build/test/control/restoration logs, JUnit, hashes and lease-push receipts. SHA-256: 594bf1dd3a57728c23dd0752926f501f34a8be8e31c8d924e6bec3723d029a82; downloaded copy verified.

AI-assisted: yes.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 27, 2026
JJenkx and others added 2 commits October 27, 2026 06:20
…on invalid UTF-8

The wide printf family decoded its narrow %s argument as UTF-8 of up to
4 bytes and fell back to Latin-1 for bytes that did not form a
character. The libc.prx that titles ship decodes at most 3 bytes per
character, accepts overlong forms and encoded surrogates, ends the
string at a decoded NUL, and fails the whole call with EILSEQ on a
byte that cannot begin a character, a 4-byte lead or an incomplete
character, keeping the output written before that conversion. A
precision stops decoding after that many characters but always decodes
the first. Follow those rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The wide printf family wrote a narrow %c as the byte's own code unit,
so 0xE9 became U+00E9. The libc.prx that titles ship converts the low
byte of the argument as btowc does in its UTF-8 C locale: a byte below
0x80 is that character, and a byte of 0x80 or above fails the whole
call with -1, keeping the output written before it and setting EILSEQ
only for 0x80-0xBF and 0xF0-0xFF. Follow that, using the failure path
the narrow %s already has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/wprintf-narrow-char branch from ef2bee9 to 85de0b5 Compare October 27, 2026 10:23
@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Main conflict refresh, 2026-10-27 10:18 UTC pass: exact head 85de0b5ff191e7410b6e322d16192558892de64b on tested main 3fea2a937ba9fbab2f06c51691fe0c5763659aa3. The topic production/test edits and current main's includes and checked-pointer behavior are preserved.

Linux Release focused rebuild: Narrow/wide formatting, stdio variadic and public headers each pass three times: 12 passes, zero skips. Conventions, whitespace and merge checks pass. Every refreshed head's submodule pointers match the pinned main; the private dependency checkouts used for the gates are aligned. No new negative-control, Windows, console, title, GPU, full-suite or performance run is claimed. Earlier measurements below retain their original exact pins. Fulfilled merge-order dependencies have been cleared from the checklist; open prerequisites remain.

Exact source/dependency pins, patch-preservation and gitlink proofs, builds, verbose tests, JUnit and lease-push receipts. SHA-256: 702c4110ee9dde421c2dd5e057ed53a2df05e0cf7b01f67e8fb9cb779e332f7c; downloaded copy verified.

AI-assisted: yes.

@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Integration note for the eventual rebase. I merged this PR locally with #11893, #11907, #12397 and #9953 on main 319621ba2e.

Conflict: it conflicts in FormattingWide.cpp with #12397, which makes wide %C an unknown conversion. The resolution that keeps both behaviours:

Result on Windows (WinLibs GCC 15.2.0 UCRT r7, Release): guest_formatting, guest_wide_formatting, guest_stdio_variadic, guest_scan_count, guest_strings, guest_wide_conversion and guest_safe_errors pass. Against PPSA07923's libc.prx:

  • 200,000 random narrow snprintf formats: 0 differences. These are integer, character, string and pointer conversions with flags (including '), widths, precisions and length modifiers.
  • 200,000 random wide swprintf formats: 0 differences.
  • Floating %e/%f/%g: the only remaining differences (342 double, 84 long double of 200,000) are the documented extended-precision digits beyond 17 significant digits, the same count as plain main.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 2, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi JJenkx, your #9953 merged today and this PR no longer merges cleanly with main. #9953 added a WideEncodingError (with partial) and the bool AppendUtf16 loop to FormattingWide.cpp, plus overlapping swprintf UTF-8 tests in GuestWideFormatting.cpp. Your version adds invalidSequence and a different message.

Could you merge main into this branch, build on the version #9953 put on main rather than adding a second one, and keep any tests of yours that #9953 doesn't already cover? A merge commit is fine; no need to rebase or force-push. Once it's pushed we'll re-review the merge.

@JJenkx

JJenkx commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Main is merged, without a rebase or force-push. Head c22807ee5f65c395d0cca0d544a35d2bd669541f includes main 4f64f81ef7b4bca975b4b9f3dfd15dfff56f716f.

The resolution builds on #9953: one WideEncodingError, retaining main’s message and partial, with the invalidSequence discriminator needed by narrow %c; AppendUtf16 is unchanged from main. Main’s UTF-8 tests are retained, with five distinct narrow %c assertions from this PR. The debt entries are reconciled.

Fresh Linux Release build and focused checks: 4/4 pass, zero skips (guest_wide_formatting, guest_formatting, guest_stdio_variadic, public_headers). Conventions and whitespace checks pass. A frozen-fixture negative control fails at the continuation-byte %c assertion with main’s production source, then passes after restoring the candidate; restoration reproduced libc byte-for-byte. That control ran on the preceding merge; both the candidate formatting source/test fixture and main’s control source are unchanged in the latest refresh. No new Windows or console run is claimed.

Logs, JUnit, resolved diff, exact pins and control receipts, SHA-256 a6ec6ec866ade26fd53db504dcdd0c4d9514463ebc5ad6c17a3d881b80d2cce9.

Ready for re-review. AI-assisted: yes.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 2, 2026
@oneandonlydean
oneandonlydean merged commit 9c9f2b5 into boykopovar:main Nov 4, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants