Skip to content

Commit 9673e36

Browse files
committed
pkg/mpaland-printf: format points like glibc
newlib and picolibc already implicitly add the `0x` prefix for `%p` just as glibc does, and some of our tests scripts depend on this. (And subjectively, this looks better.) This adds mpaland/printf#90 on top of our patches.
1 parent c0cc617 commit 9673e36

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
From 101d684f1759aa8aef4d49bbb1e2eebdbaa41afb Mon Sep 17 00:00:00 2001
2+
From: Marian Buschsieweke <[email protected]>
3+
Date: Fri, 22 Aug 2025 10:32:04 +0200
4+
Subject: [PATCH] Change %p formatting to match glibc
5+
6+
Taken from https://github.com/mpaland/printf/pull/90
7+
---
8+
printf.c | 24 +++++++++++++++---------
9+
test/test_suite.cpp | 19 +++++++++++--------
10+
2 files changed, 26 insertions(+), 17 deletions(-)
11+
12+
diff --git a/printf.c b/printf.c
13+
index efdee81..50a4ddf 100644
14+
--- a/printf.c
15+
+++ b/printf.c
16+
@@ -824,19 +824,25 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
17+
}
18+
19+
case 'p' : {
20+
- width = sizeof(void*) * 2U;
21+
- flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
22+
+ width = sizeof(void*) * 2U + 2;
23+
+ flags |= FLAGS_ZEROPAD | FLAGS_HASH;
24+
+ uintptr_t value = (uintptr_t)va_arg(va, void*);
25+
+
26+
+ if (value == 0) {
27+
+ idx = _out_rev(out, buffer, idx, maxlen, ")lin(", 5, width, flags);
28+
+ } else {
29+
#if defined(PRINTF_SUPPORT_LONG_LONG)
30+
- const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
31+
- if (is_ll) {
32+
- idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
33+
- }
34+
- else {
35+
+ const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
36+
+ if (is_ll) {
37+
+ idx = _ntoa_long_long(out, buffer, idx, maxlen, value, false, 16U, precision, width, flags);
38+
+ }
39+
+ else {
40+
#endif
41+
- idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
42+
+ idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)value, false, 16U, precision, width, flags);
43+
#if defined(PRINTF_SUPPORT_LONG_LONG)
44+
- }
45+
+ }
46+
#endif
47+
+ }
48+
format++;
49+
break;
50+
}
51+
diff --git a/test/test_suite.cpp b/test/test_suite.cpp
52+
index 5507c3b..740771a 100644
53+
--- a/test/test_suite.cpp
54+
+++ b/test/test_suite.cpp
55+
@@ -1361,36 +1361,39 @@ TEST_CASE("pointer", "[]" ) {
56+
57+
test::sprintf(buffer, "%p", (void*)0x1234U);
58+
if (sizeof(void*) == 4U) {
59+
- REQUIRE(!strcmp(buffer, "00001234"));
60+
+ REQUIRE(!strcmp(buffer, "0x00001234"));
61+
}
62+
else {
63+
- REQUIRE(!strcmp(buffer, "0000000000001234"));
64+
+ REQUIRE(!strcmp(buffer, "0x0000000000001234"));
65+
}
66+
67+
test::sprintf(buffer, "%p", (void*)0x12345678U);
68+
if (sizeof(void*) == 4U) {
69+
- REQUIRE(!strcmp(buffer, "12345678"));
70+
+ REQUIRE(!strcmp(buffer, "0x12345678"));
71+
}
72+
else {
73+
- REQUIRE(!strcmp(buffer, "0000000012345678"));
74+
+ REQUIRE(!strcmp(buffer, "0x0000000012345678"));
75+
}
76+
77+
test::sprintf(buffer, "%p-%p", (void*)0x12345678U, (void*)0x7EDCBA98U);
78+
if (sizeof(void*) == 4U) {
79+
- REQUIRE(!strcmp(buffer, "12345678-7EDCBA98"));
80+
+ REQUIRE(!strcmp(buffer, "0x12345678-0x7edcba98"));
81+
}
82+
else {
83+
- REQUIRE(!strcmp(buffer, "0000000012345678-000000007EDCBA98"));
84+
+ REQUIRE(!strcmp(buffer, "0x0000000012345678-0x000000007edcba98"));
85+
}
86+
87+
if (sizeof(uintptr_t) == sizeof(uint64_t)) {
88+
test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
89+
- REQUIRE(!strcmp(buffer, "00000000FFFFFFFF"));
90+
+ REQUIRE(!strcmp(buffer, "0x00000000ffffffff"));
91+
}
92+
else {
93+
test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
94+
- REQUIRE(!strcmp(buffer, "FFFFFFFF"));
95+
+ REQUIRE(!strcmp(buffer, "0xffffffff"));
96+
}
97+
+
98+
+ test::sprintf(buffer, "%p", nullptr);
99+
+ REQUIRE(!strcmp(buffer, "(nil)"));
100+
}
101+
102+
103+
--
104+
2.43.0
105+

0 commit comments

Comments
 (0)