Merge "Add %b/%B to async-safe logging." into main am: bf4d3a95c1 am: 5abfd88813 am: 925b5f482f am: a5da931b29 am: d30174967c

Original change: https://android-review.googlesource.com/c/platform/bionic/+/2721534

Change-Id: Ib294ee85f065bb4b6d747d50d9bce1b31fb5f58f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Elliott Hughes 2023-08-24 18:03:56 +00:00 committed by Automerger Merge Worker
commit faa747b38c
2 changed files with 23 additions and 5 deletions

View file

@ -207,10 +207,12 @@ static void format_integer(char* buf, size_t buf_size, uint64_t value, char conv
// Decode the conversion specifier.
int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o');
int base = 10;
if (conversion == 'x' || conversion == 'X') {
if (tolower(conversion) == 'x') {
base = 16;
} else if (conversion == 'o') {
base = 8;
} else if (tolower(conversion) == 'b') {
base = 2;
}
bool caps = (conversion == 'X');
@ -360,7 +362,8 @@ static void out_vformat(Out& o, const char* format, va_list args) {
format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
} else if (c == 'm') {
strerror_r(errno, buffer, sizeof(buffer));
} else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') {
} else if (tolower(c) == 'b' || c == 'd' || c == 'i' || c == 'o' || c == 'u' ||
tolower(c) == 'x') {
/* integers - first read value from stack */
uint64_t value;
int is_signed = (c == 'd' || c == 'i' || c == 'o');
@ -391,10 +394,10 @@ static void out_vformat(Out& o, const char* format, va_list args) {
value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift);
}
if (alternate && value != 0 && (c == 'x' || c == 'o')) {
if (c == 'x') {
if (alternate && value != 0 && (tolower(c) == 'x' || c == 'o' || tolower(c) == 'b')) {
if (tolower(c) == 'x' || tolower(c) == 'b') {
buffer[0] = '0';
buffer[1] = 'x';
buffer[1] = c;
format_integer(buffer + 2, sizeof(buffer) - 2, value, c);
} else {
buffer[0] = '0';

View file

@ -45,6 +45,15 @@ TEST(async_safe_log, smoke) {
async_safe_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
EXPECT_STREQ("aabbcc", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%bb", 1234);
EXPECT_STREQ("a10011010010b", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%#bb", 1234);
EXPECT_STREQ("a0b10011010010b", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%#Bb", 1234);
EXPECT_STREQ("a0B10011010010b", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%cc", 'b');
EXPECT_STREQ("abc", buf);
@ -76,9 +85,15 @@ TEST(async_safe_log, smoke) {
async_safe_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
EXPECT_STREQ("a12abz", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%#xz", 0x12ab);
EXPECT_STREQ("a0x12abz", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
EXPECT_STREQ("a12ABz", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%#Xz", 0x12ab);
EXPECT_STREQ("a0X12ABz", buf);
async_safe_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
EXPECT_STREQ("a00123456z", buf);