libc: printf family prints %p as a zero-padded 16-digit value without 0x on Windows, where FreeBSD prints 0x1234 and 0x0 #3111
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:23:26Z.
What
On Windows the guest printf family sends
%pto the host CRT, which prints a zero-padded 16-digit value with no prefix:0000000000001234for 0x1234 and0000000000000000for NULL. FreeBSD'svfprintf.c(case 'p') setsox[1] = 'x'unconditionally (and the widevfwprintf.cdoes the same), so the output is always0xplus lowercase hex digits and NULL prints0x0;#,+and space are ignored,-,0, width and precision behave as for an integer conversion (%.0pof NULL prints0x). A title that logs or parses pointers with%psees a different text than on the console. An existing check incore/libs/tests/Formatting.cppcompared the guest output with the host's%pand therefore encoded the old behaviour.Reproduce
Windows 11, WinLibs GCC 15.2.0 r7,
mainat the time of writing:snprintf_nid_postfix(buffer, 64, "%p", (void*)0x1234)gives0000000000001234and for NULL0000000000000000(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 onmain; PR #2693, closed by the intake check):LibcDetail::FormatPointerbuilds that form and is used byFormatWindows(snprintf, sprintf, vsnprintf, vfprintf, printf, ...) and byFormatWide(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) andguest_wide_formattingfail 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 throughsnprintf,sprintf,vsnprintfandswprintf: 0 divergences, nova_listmutation, no guard-byte overrun; a second review confirmed the FreeBSD output for every flag combination fromvfprintf.c. Linux not run.AI-assisted: yes.