libc: treat negative dynamic printf precision as omitted #503

Closed
github-bot wants to merge 1 commit from gh-pr/503/head into gh-pr/503/base
Owner

@⁠Mininaut on GitHub · 2026-09-07 14:00 UTC · original
Mininaut:fix/guest-printf-negative-precision → main · +205 −1 · 3 files · 1 commits · closed without merging

Summary

Treat negative precision arguments supplied through .* as omitted in guest printf. Clear FLAGS_PRECISION for negative values; preserve explicit zero and positive precision. No snprintf truncation changes.

The printf contract states: “A negative precision is taken as if the precision were omitted.” Previously the parser clamped the value to zero while retaining the explicit-precision flag. This suppressed zero integers, emptied strings, disabled integer zero-padding and selected zero rather than default floating precision.

Regression coverage

Add guest_printf_tests, registered as CTest guest_printf and included in kyty_tests. Tests call GetGuestSnprintfCtxFunc with a real VaContext, comparing output, returned lengths, termination and untouched buffer tails against host snprintf in the C locale.

440 comparisons cover negative precision (including INT_MIN), explicit zero/positive precision, zero integers, empty strings/format, integer and f/F/e/E conversions, widths, sign/zero-padding, per-conversion state reset, GP register boundaries and interleaved GP/FP stack overflow. Argument consumption is checked separately.

Validation

Same test source against the entire actual production guestPrintf.cpp:

  • Upstream 6aa6e390ee2930d7eeba7351c38bd342b783d933: 440 cases, 151 failures, executable exit 1 / CTest exit 8.
  • Parser patch only: 440 cases, 0 failures, executable and CTest exit 0.
  • Zero/positive precision controls pass on both.

These were focused CMake/Ninja/CTest unit runs on Linux x86-64 using Zig 0.13 / Clang 18 and libc++. No formatter or argument-accessor copies were used. The verification driver supplies fail-fast link fixtures only for unused logging/fatal hooks and uses real fmt headers; the checked-in target links common. Objects were compiled in Release mode; a link-only -O0 option reused the bootstrapped Zig runtime.

The normal project configure was attempted but blocked by Zig rejecting -Wl,-v during compiler ABI detection. Consequently the root-project target itself has not been built locally. With the normal project configured, run:

cmake --build build --target guest_printf_tests -j1
ctest --test-dir build -R '^guest_printf$' --output-on-failure

No full emulator/game compatibility, Windows/macOS validation, truncation/zero-size-buffer behavior or comprehensive floating-point conformance is claimed.

Developed and verified with AI assistance.

**@⁠Mininaut** on GitHub · 2026-09-07 14:00 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/503) `Mininaut:fix/guest-printf-negative-precision` → `main` · +205 −1 · 3 files · 1 commits · closed without merging ## Summary Treat negative precision arguments supplied through `.*` as omitted in guest printf. Clear `FLAGS_PRECISION` for negative values; preserve explicit zero and positive precision. No snprintf truncation changes. The [printf contract](https://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html) states: “A negative precision is taken as if the precision were omitted.” Previously the parser clamped the value to zero while retaining the explicit-precision flag. This suppressed zero integers, emptied strings, disabled integer zero-padding and selected zero rather than default floating precision. ## Regression coverage Add `guest_printf_tests`, registered as CTest `guest_printf` and included in `kyty_tests`. Tests call `GetGuestSnprintfCtxFunc` with a real `VaContext`, comparing output, returned lengths, termination and untouched buffer tails against host `snprintf` in the C locale. 440 comparisons cover negative precision (including `INT_MIN`), explicit zero/positive precision, zero integers, empty strings/format, integer and f/F/e/E conversions, widths, sign/zero-padding, per-conversion state reset, GP register boundaries and interleaved GP/FP stack overflow. Argument consumption is checked separately. ## Validation Same test source against the entire actual production `guestPrintf.cpp`: - Upstream `6aa6e390ee2930d7eeba7351c38bd342b783d933`: 440 cases, **151 failures**, executable exit 1 / CTest exit 8. - Parser patch only: 440 cases, **0 failures**, executable and CTest exit 0. - Zero/positive precision controls pass on both. These were focused CMake/Ninja/CTest unit runs on Linux x86-64 using Zig 0.13 / Clang 18 and libc++. No formatter or argument-accessor copies were used. The verification driver supplies fail-fast link fixtures only for unused logging/fatal hooks and uses real fmt headers; the checked-in target links `common`. Objects were compiled in Release mode; a link-only `-O0` option reused the bootstrapped Zig runtime. The normal project configure was attempted but blocked by Zig rejecting `-Wl,-v` during compiler ABI detection. Consequently the root-project target itself has not been built locally. With the normal project configured, run: ```sh cmake --build build --target guest_printf_tests -j1 ctest --test-dir build -R '^guest_printf$' --output-on-failure ``` No full emulator/game compatibility, Windows/macOS validation, truncation/zero-size-buffer behavior or comprehensive floating-point conformance is claimed. Developed and verified with AI assistance.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:24:07 +00:00
github-bot closed this pull request 2026-11-26 09:24:07 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.