Skip to content

fix(libc): print a non-ASCII unit after % in the wide printf family instead of failing - #23884

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:fix/libc-wide-printf-non-ascii-conversion
Nov 19, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:fix/libc-wide-printf-non-ascii-conversion

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

What

In the wide printf family (swprintf, vswprintf, wprintf, fwprintf and their _s forms, all through FormatWideInto), a % followed by a 16-bit unit from 0x80 up made the whole call fail: FormatWideInto threw Unsupported format conversion, which vswprintf turns into a failure, so swprintf(buf, n, L"100%完") returned a negative value and left the buffer empty. A title that puts a literal percent sign in front of a kana, an ideograph or an accented letter in a wide format string got no text at all.

The same function already prints an unknown conversion as literal text: % and the character, padded to the width. That is the rule measured on the console's libSceLibcInternal.sprx for %y, %m, %D and others (#5303), and the tests extend it to a high surrogate (%😀 prints %😀, a lone 0xd83d prints as itself). The branch that implements it tested conversion < 0x80 && unknown or 0xd800 <= conversion < 0xdc00, which left every other unit from 0x80 up, a lone low surrogate included, to the final throw. The narrow family has no such hole: FormatWindows treats any byte that is not a known conversion as unknown.

This PR makes every unit from 0x80 up take the unknown-conversion branch. Nothing else changes: the known conversions, the trailing-% and incomplete-conversion rejections and %n are decided before or by the same strchr test as before (a NUL conversion is rejected earlier, at Incomplete format conversion).

Not measured on a console: like the surrogate case and the other forms its debt entry lists as "by extension", this follows the measured rule for unknown ASCII letters. The The wide printf family debt entry now says so.

Tested

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

  • Five checks added to core/libs/tests/GuestWideFormatting.cpp next to the surrogate ones: an ideograph after 100%, a right-aligned %4é, a left-aligned %-4で, two real %d arguments around the non-ASCII unknown conversions %\x80 and %¥ (the arguments still line up), and a lone low surrogate 0xdc00.
  • Red: with the new test file on main 01b7264270 and FormattingWide.cpp unchanged, guest_wide_formatting fails at the first new check, swprintf ideograph as an unknown conversion.
  • Green: with the fix (branch based on main fd6d6f4a84), guest_wide_formatting passes, and ctest -R "^guest_(wide|.*format|.*printf|.*scan|standard_stream|wcs)" passes 35 of 35.
  • python tools/check_conventions.py --base origin/main and python tools/debt_fold.py --check pass.
  • python tools/pr_overlap.py --branch could not compare fix(libc): write a 36-byte struct tm as the console does #10409, which conflicts with main on GuestWideFormatting.cpp; compared by hand, it removes one line of that test for its struct tm change and does not touch the wide formatter.

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

…nstead of failing

A % followed by a unit from 0x80 up, such as a kana or an ideograph,
made the whole wide printf call fail, while an unknown ASCII letter and
a high surrogate were already printed as literal text. Treat every unit
from 0x80 up as the unknown conversion it is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@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 002c12f88d:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_wide_formatting_tests built cleanly.
  • CTest (guest_wide_formatting):
    1/1 Test #1161: guest_wide_formatting ............   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/FormattingWide.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1161: guest_wide_formatting ............Subprocess aborted***Exception:   0.80 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • FormattingWide.cpp: verified condition 'conversion == u'S' || conversion >= 0x80 || !std::strchr(LibcDetail::KnownConversions, static_cast(conversion))' correctly handles non-ASCII units >= 0x80 without invoking std::strchr with out-of-range char values
    • FormattingWide.cpp: verified lone surrogates, surrogate pairs, ideographs, and Latin-1/kana non-ASCII units take the fallback literal branch and preserve formatting/arguments correctly
    • FormattingWide.cpp: verified trailing % and incomplete conversion handling (throws std::invalid_argument) remain fully protected before reaching this branch
    • GuestWideFormatting.cpp: verified test assertions properly exercise non-ASCII conversions, width padding, alignment, argument ordering, and lone surrogates
    • docs/dev/debt/changes/fix-libc-wide-printf-non-ascii-conversion.md: verified debt fragment adheres to format and documents console probe behavior

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit db8edcf into boykopovar:main Nov 19, 2026
22 checks passed
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.

3 participants