Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change %p formatting to match glibc #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,20 +820,27 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
}

case 'p' : {
width = sizeof(void*) * 2U;
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
width = sizeof(void*) * 2U + 2;
flags |= FLAGS_ZEROPAD | FLAGS_HASH;
uintptr_t value = (uintptr_t)va_arg(va, void*);

if (value == 0) {
idx = _out_rev(out, buffer, idx, maxlen, ")lin(", 5, width, flags);
} else {
#if defined(PRINTF_SUPPORT_LONG_LONG)
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) {
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
}
else {
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) {
idx = _ntoa_long_long(out, buffer, idx, maxlen, value, false, 16U, precision, width, flags);
}
else {
#endif
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)value, false, 16U, precision, width, flags);
#if defined(PRINTF_SUPPORT_LONG_LONG)
}
}
#endif
format++;
}

format++;
break;
}

Expand Down
19 changes: 11 additions & 8 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,36 +1361,39 @@ TEST_CASE("pointer", "[]" ) {

test::sprintf(buffer, "%p", (void*)0x1234U);
if (sizeof(void*) == 4U) {
REQUIRE(!strcmp(buffer, "00001234"));
REQUIRE(!strcmp(buffer, "0x00001234"));
}
else {
REQUIRE(!strcmp(buffer, "0000000000001234"));
REQUIRE(!strcmp(buffer, "0x0000000000001234"));
}

test::sprintf(buffer, "%p", (void*)0x12345678U);
if (sizeof(void*) == 4U) {
REQUIRE(!strcmp(buffer, "12345678"));
REQUIRE(!strcmp(buffer, "0x12345678"));
}
else {
REQUIRE(!strcmp(buffer, "0000000012345678"));
REQUIRE(!strcmp(buffer, "0x0000000012345678"));
}

test::sprintf(buffer, "%p-%p", (void*)0x12345678U, (void*)0x7EDCBA98U);
if (sizeof(void*) == 4U) {
REQUIRE(!strcmp(buffer, "12345678-7EDCBA98"));
REQUIRE(!strcmp(buffer, "0x12345678-0x7edcba98"));
}
else {
REQUIRE(!strcmp(buffer, "0000000012345678-000000007EDCBA98"));
REQUIRE(!strcmp(buffer, "0x0000000012345678-0x000000007edcba98"));
}

if (sizeof(uintptr_t) == sizeof(uint64_t)) {
test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
REQUIRE(!strcmp(buffer, "00000000FFFFFFFF"));
REQUIRE(!strcmp(buffer, "0x00000000ffffffff"));
}
else {
test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
REQUIRE(!strcmp(buffer, "FFFFFFFF"));
REQUIRE(!strcmp(buffer, "0xffffffff"));
}

test::sprintf(buffer, "%p", nullptr);
REQUIRE(!strcmp(buffer, "(nil)"));
}


Expand Down