diff options
author | Colin Cross <ccross@android.com> | 2021-07-30 09:36:57 -0700 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2021-07-30 09:39:21 -0700 |
commit | 695af0da30a37cb7e105c40811656a22a898536c (patch) | |
tree | c2a24253954884fbfd8112d83aea087a46397d35 | |
parent | e397e49aa73c4c21cb43c0b302d3df3a7b7fb944 (diff) | |
download | bionic-695af0da30a37cb7e105c40811656a22a898536c.tar.gz |
POSIX strerror_r returns an error number, not -1
The posix spec says strerror_r returns a positive error number, not
-1 and set errno.
Test: bionic-unit-tests-static
Change-Id: I6a12d50d046f9caac299bf3bff63e6c9496c1b6f
-rw-r--r-- | libc/bionic/strerror.cpp | 3 | ||||
-rw-r--r-- | tests/string_posix_strerror_r_test.cpp | 8 |
2 files changed, 5 insertions, 6 deletions
diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp index 57335674b..0deb2000f 100644 --- a/libc/bionic/strerror.cpp +++ b/libc/bionic/strerror.cpp @@ -196,8 +196,7 @@ int strerror_r(int error_number, char* buf, size_t buf_len) { length = async_safe_format_buffer(buf, buf_len, "Unknown error %d", error_number); } if (length >= buf_len) { - errno_restorer.override(ERANGE); - return -1; + return ERANGE; } return 0; diff --git a/tests/string_posix_strerror_r_test.cpp b/tests/string_posix_strerror_r_test.cpp index 596684b56..67b3c1f90 100644 --- a/tests/string_posix_strerror_r_test.cpp +++ b/tests/string_posix_strerror_r_test.cpp @@ -55,10 +55,10 @@ TEST(string, posix_strerror_r) { // Buffer too small. errno = 0; memset(buf, 0, sizeof(buf)); - ASSERT_EQ(-1, strerror_r(4567, buf, 2)); - ASSERT_STREQ("U", buf); - // The POSIX strerror_r sets errno to ERANGE (the GNU one doesn't). - ASSERT_EQ(ERANGE, errno); + ASSERT_EQ(ERANGE, strerror_r(EPERM, buf, 2)); + ASSERT_STREQ("O", buf); + // POSIX strerror_r returns an error without updating errno. + ASSERT_EQ(0, errno); } #endif |