libc: printf family prints %p as a zero-padded 16-digit value without 0x on Windows, where FreeBSD prints 0x1234 and 0x0 #3111

Closed
opened 2026-10-10 22:23:26 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-10 22:23:26 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:23:26Z.


What

On Windows the guest printf family sends %p to the host CRT, which prints a zero-padded 16-digit value with no prefix: 0000000000001234 for 0x1234 and 0000000000000000 for NULL. FreeBSD's vfprintf.c (case 'p') sets ox[1] = 'x' unconditionally (and the wide vfwprintf.c does the same), so the output is always 0x plus lowercase hex digits and NULL prints 0x0; #, + and space are ignored, -, 0, width and precision behave as for an integer conversion (%.0p of NULL prints 0x). A title that logs or parses pointers with %p sees a different text than on the console. An existing check in core/libs/tests/Formatting.cpp compared the guest output with the host's %p and therefore encoded the old behaviour.

Reproduce

Windows 11, WinLibs GCC 15.2.0 r7, main at the time of writing: snprintf_nid_postfix(buffer, 64, "%p", (void*)0x1234) gives 0000000000001234 and for NULL 0000000000000000 (FreeBSD: 0x1234, 0x0). The Linux narrow path goes through glibc, which prints a NULL pointer as (nil); that is recorded in TechnicalDebt.

Fix

A tested fix is on my fork: husseinkad:fix/libc-printf-pointer-form (three commits on main; PR #2693, closed by the intake check): LibcDetail::FormatPointer builds that form and is used by FormatWindows (snprintf, sprintf, vsnprintf, vfprintf, printf, ...) and by FormatWide (swprintf, wprintf, ...); behaviour for every other conversion is unchanged. guest_formatting (Windows-only narrow cases: NULL, a normal value, width, -, 0, precision, %.0p, truncation return values) and guest_wide_formatting fail without the change.

Checked against a reference of FreeBSD's algorithm, cross-checked with the host's %#llx: 114,336 distinct format specs (all flag subsets and orders, widths, precisions, * forms) x 8 values x buffer sizes 0 to len+7, about 2.8 million calls through snprintf, sprintf, vsnprintf and swprintf: 0 divergences, no va_list mutation, no guard-byte overrun; a second review confirmed the FreeBSD output for every flag combination from vfprintf.c. Linux not run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795812752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3111). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T22:23:26Z. --- ### What On Windows the guest printf family sends `%p` to the host CRT, which prints a zero-padded 16-digit value with no prefix: `0000000000001234` for 0x1234 and `0000000000000000` for NULL. FreeBSD's [`vfprintf.c`](https://github.com/freebsd/freebsd-src/blob/main/lib/libc/stdio/vfprintf.c) (`case 'p'`) sets `ox[1] = 'x'` unconditionally (and the wide `vfwprintf.c` does the same), so the output is always `0x` plus lowercase hex digits and NULL prints `0x0`; `#`, `+` and space are ignored, `-`, `0`, width and precision behave as for an integer conversion (`%.0p` of NULL prints `0x`). A title that logs or parses pointers with `%p` sees a different text than on the console. An existing check in `core/libs/tests/Formatting.cpp` compared the guest output with the host's `%p` and therefore encoded the old behaviour. ### Reproduce Windows 11, WinLibs GCC 15.2.0 r7, `main` at the time of writing: `snprintf_nid_postfix(buffer, 64, "%p", (void*)0x1234)` gives `0000000000001234` and for NULL `0000000000000000` (FreeBSD: `0x1234`, `0x0`). The Linux narrow path goes through glibc, which prints a NULL pointer as `(nil)`; that is recorded in TechnicalDebt. ### Fix A tested fix is on my fork: [`husseinkad:fix/libc-printf-pointer-form`](https://github.com/husseinkad/AnyPS5/tree/fix/libc-printf-pointer-form) (three commits on `main`; PR #2693, closed by the intake check): `LibcDetail::FormatPointer` builds that form and is used by `FormatWindows` (snprintf, sprintf, vsnprintf, vfprintf, printf, ...) and by `FormatWide` (swprintf, wprintf, ...); behaviour for every other conversion is unchanged. `guest_formatting` (Windows-only narrow cases: NULL, a normal value, width, `-`, `0`, precision, `%.0p`, truncation return values) and `guest_wide_formatting` fail without the change. Checked against a reference of FreeBSD's algorithm, cross-checked with the host's `%#llx`: 114,336 distinct format specs (all flag subsets and orders, widths, precisions, `*` forms) x 8 values x buffer sizes 0 to len+7, about 2.8 million calls through `snprintf`, `sprintf`, `vsnprintf` and `swprintf`: 0 divergences, no `va_list` mutation, no guard-byte overrun; a second review confirmed the FreeBSD output for every flag combination from `vfprintf.c`. Linux not run. AI-assisted: yes.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#3111
No description provided.