Skip to content

Commit

Permalink
Fix mte build breakage.
Browse files Browse the repository at this point in the history
The strerror_buf is way too large, so instead of using a separate
buffer for just this string, reuse the already existing buffer.
Increase the buffer size to cover the maximum errno string.

Add a unit test to verify that none of the errno values are cut off
in the async_safe_format_buffer function when passing %m.

Bug: 274474681

Test: New unit test passes.
Test: Changing the buffer to a small value and verify that the test fails.
Change-Id: I4cb4652709582a8a6b958e12de5d923ec950e6b6
  • Loading branch information
cferris1000 authored and basamaryan committed Jul 24, 2023
1 parent 6573263 commit 0bdffb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions libc/async_safe/async_safe_log.cpp
Expand Up @@ -254,7 +254,7 @@ static void out_vformat(Out& o, const char* format, va_list args) {
bool alternate = false;
size_t bytelen = sizeof(int);
int slen;
char buffer[32]; /* temporary buffer used to format numbers */
char buffer[64]; // temporary buffer used to format numbers/format errno string

char c;

Expand Down Expand Up @@ -345,7 +345,6 @@ static void out_vformat(Out& o, const char* format, va_list args) {

/* conversion specifier */
const char* str = buffer;
char strerror_buf[256];
if (c == 's') {
/* string */
str = va_arg(args, const char*);
Expand All @@ -360,7 +359,7 @@ static void out_vformat(Out& o, const char* format, va_list args) {
buffer[1] = 'x';
format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
} else if (c == 'm') {
str = strerror_r(errno, strerror_buf, sizeof(strerror_buf));
strerror_r(errno, buffer, sizeof(buffer));
} else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') {
/* integers - first read value from stack */
uint64_t value;
Expand Down
18 changes: 18 additions & 0 deletions tests/async_safe_test.cpp
Expand Up @@ -16,6 +16,8 @@

#include <gtest/gtest.h>

#include <errno.h>

#if defined(__BIONIC__)
#include <async_safe/log.h>
#endif // __BIONIC__
Expand Down Expand Up @@ -227,3 +229,19 @@ TEST(async_safe_log, buffer_overrun) {
GTEST_SKIP() << "bionic-only test";
#endif // __BIONIC__
}

// Verify that using %m is never cut off.
TEST(async_safe_format_buffer, percent_m_fits_in_buffer) {
#if defined(__BIONIC__)
for (int i = 0; i < 256; i++) {
errno = i;
char async_buf[256];
async_safe_format_buffer(async_buf, sizeof(async_buf), "%m");
char strerror_buf[1024];
strerror_r(errno, strerror_buf, sizeof(strerror_buf));
ASSERT_STREQ(strerror_buf, async_buf);
}
#else // __BIONIC__
GTEST_SKIP() << "bionic-only test";
#endif // __BIONIC__
}

0 comments on commit 0bdffb1

Please sign in to comment.