Skip to content

Enable C99 length modifiers in newlib's printf - #196

Open
NotTimm wants to merge 1 commit into
vitasdk:masterfrom
NotTimm:newlib-io-c99-formats
Open

NotTimm wants to merge 1 commit into
vitasdk:masterfrom
NotTimm:newlib-io-c99-formats

Conversation

@NotTimm

@NotTimm NotTimm commented Nov 23, 2026

Copy link
Copy Markdown

What does this change?

Adds --enable-newlib-io-c99-formats to newlib's configure flags in cmake/recipes/BuildSdkComponents.cmake, so the printf/scanf family supports the C99 length modifiers z (size_t), t (ptrdiff_t) and j (intmax_t).

Why is this change needed?

newlib was configured without --enable-newlib-io-c99-formats, so its printf family does not understand the z, t and j length modifiers. A conversion such as %zu is skipped without consuming its argument: the modifier is printed as a literal character, every following argument is read from the wrong slot, and a later %s dereferences an integer.

mpv and FFmpeg use %zd/%zu in log messages, and this crashed Switchfin on the Vita (dragonflylee/switchfin#291). The crash came after a seek, while mpv formatted "%s/%d: %zd packets, %zd bytes%s%s\n".

Minimal repro:

char buf[64];
snprintf(buf, sizeof(buf), "%zu %s", (size_t)1, "ok");
// expected "1 ok"; today 'z' is printed literally, then %s reads 1 as a pointer and crashes

Testing

  • Diagnosis confirmed on real PlayStation Vita hardware (PS Vita 1000): a core dump showed the crash inside newlib's formatter with the shifted arguments. A workaround in Switchfin that rewrites z/t/j before calling newlib (fix(psv): support C99 printf length modifiers dragonflylee/switchfin#291) stopped the crash.
  • In newlib's libc/stdio/vfprintf.c, case 'j', case 'z' and case 't' sit behind #ifdef _WANT_IO_C99_FORMATS, which this flag defines.
  • I have not built the full toolchain with this change locally. This PR's CI is the first build with the flag.

Compatibility

  • No API or ABI change. Headers and function signatures stay the same.
  • Behavior change: %z…, %t… and %j… conversions now format and consume their argument as C99 requires, instead of printing the modifier and leaving the argument for the next conversion. Code that worked around the old behavior by avoiding these modifiers is unaffected.
  • scanf gains the same modifiers.
  • libc's formatted-IO code gets slightly larger.
  • long long support (--enable-newlib-io-long-long) is already enabled, so j maps onto existing 64-bit handling.

Third-party material

None

AI assistance

  • Claude (Anthropic) via Claude Code, used to help trace the cause and fix it.

Additional context

newlib was configured without --enable-newlib-io-c99-formats, so its
printf family does not understand the z, t and j length modifiers. A
conversion such as %zu is skipped without consuming its argument, so
every following argument is read from the wrong slot, and a later %s
dereferences an integer.

mpv and FFmpeg use %zd/%zu in log messages, and this crashed Switchfin
on the Vita (dragonflylee/switchfin#291).

@devnoname120 devnoname120 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking findings. I verified the exact head 9a826a1 against base 38d1e45 rather than relying only on the configure-option documentation.

Evidence:

  • Rebuilt the pinned newlib (2e428297) with the base and PR configure arguments. The PR build defines _WANT_IO_C99_FORMATS=1; the base does not. The option reaches the target configuration and preserves the existing long-long support.
  • Linked representative snprintf and sscanf programs through the normal Vita compiler driver against each rebuilt libc, then executed the ARM code under a Cortex-A9 emulator with the Vita TLS import stubbed. The base formatter emits the unsupported modifiers literally (zu td j) and then faults on the shifted %s argument; the PR build produces 123 -45 6789012345 ok. The base scanner performs zero assignments; the PR build parses all three z/t/j values correctly.
  • Checked the target type mapping used by newlib's dispatch: size_t is unsigned int, ptrdiff_t is int, and intmax_t is long long, matching the implementation paths selected by this flag.
  • Rebuilt with both hard-float and softfp Vita compilers. Both configurations complete, and the softfp objects retain the softfp calling convention.
  • Inspected the PR-produced stage-1 artifact: its provenance matches the PR lock, installed newlib.h defines the feature macro, <inttypes.h> exposes the corresponding SCN* macros, and a normal-driver link selects the artifact's C99-enabled libc.
  • The representative linked text increase was about 2.1 KiB for snprintf and 5.8 KiB for sscanf, with no data/BSS increase.

Two non-blocking follow-ups:

  1. Add a direct toolchain-contract assertion for _WANT_IO_C99_FORMATS (and preferably one 8-bit SCN* macro). The full build proves the option is accepted, but without a focused assertion a later recipe change could silently drop this behavior.
  2. The PR description's “No API change” wording is slightly too broad: enabling the option intentionally exposes fifteen 8-bit SCN* macros from <inttypes.h>. This is a compatible API addition, and there is still no ABI or function-signature change.

The remaining downstream CI jobs are still running, but the implementation and produced stage-1 SDK are sufficiently verified for approval.

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.

2 participants