Skip to content

fix(psv): support C99 printf length modifiers - #291

Open
NotTimm wants to merge 1 commit into
dragonflylee:devfrom
NotTimm:fix-psv-printf
Open

NotTimm wants to merge 1 commit into
dragonflylee:devfrom
NotTimm:fix-psv-printf

Conversation

@NotTimm

@NotTimm NotTimm commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Split out of #289.

Seeking crashed in mpv's demuxer thread while formatting " %s/%d: %zd packets, %zd bytes%s%s\n" (part of the "Too many packets in the demuxer packet queues" warning). VitaSDK's newlib is built without _WANT_IO_C99_FORMATS, so z, t and j aren't supported. The conversion is skipped without consuming its argument, every following argument shifts, and %s dereferenced the packet count (0x613f). Any mpv or FFmpeg message with %z… before a %s can crash the same way.

Fix: wrap newlib's string and stream formatters at link time (-Wl,--wrap=_svfprintf_r,_vfprintf_r,vfprintf) and rewrite the format first. z and t are dropped (size_t/ptrdiff_t are 32-bit like int on the Vita) and j becomes ll. Verified in the final binary that vsnprintf, snprintf, sprintf and fprintf go through the wrapper.

Found from a psp2dmp core dump on a PS Vita 1000. Tested on the Vita: the original crash case no longer crashes.

@NotTimm

NotTimm commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Core dump from a PS Vita 1000, built from 56d3db7 (my #288 branch). mpv demuxer thread, data abort during a seek:

PC  81c8ccee  strlen + 0xae     ldrd r2, r3, [r1]    R0=0000613f R1=00006138
LR  81c90547  _svfprintf_r
              vsnprintf
              bstr_xappend_vasprintf
              mp_msg_va          "  %s/%d: %zd packets, %zd bytes%s%s\n"
              (parse_packet / read_frame_internal further up the stack)

strlen is called on 0x613f, which is the packet count. newlib skipped %zd without consuming its argument, so %s got the number.

Files: redacted core dump · matching ELF. Parse with vita-parse-core against the ELF. Server address, access token, and user/device/session IDs were overwritten with x at the same length, so the dump still parses.

@NotTimm

NotTimm commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

I sent the root-cause fix to VitaSDK: vitasdk/buildscripts#196. It's one newlib configure flag (--enable-newlib-io-c99-formats).

Because printf comes from newlib's libc at the app's final link, a VitaSDK image with that fix would fix this on the next app build. The mpv/FFmpeg packages wouldn't need rebuilding. Until then this PR works as a stopgap. Happy to close it once a fixed image is out, whichever you prefer.

@dragonflylee
dragonflylee force-pushed the dev branch 5 times, most recently from e948600 to 91a8dc8 Compare November 24, 2026 14:09
VitaSDK's newlib is built without _WANT_IO_C99_FORMATS, so %zd, %zu, %td
and %jd are skipped without consuming their argument. Every following
argument shifts, and a later %s dereferences an integer. mpv's "Too many
packets in the demuxer packet queues" warning ("%s/%d: %zd packets...")
crashed the app this way after a seek.

Wrap newlib's string and stream formatters at link time and rewrite the
format first: z and t are dropped (size_t and ptrdiff_t are 32-bit like
int) and j becomes ll.

This branch has not been deployed

No deployments
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.

1 participant