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
diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp
index 5733567..0deb200 100644
--- a/libc/bionic/strerror.cpp
+++ b/libc/bionic/strerror.cpp
@@ -196,8 +196,7 @@
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 596684b..67b3c1f 100644
--- a/tests/string_posix_strerror_r_test.cpp
+++ b/tests/string_posix_strerror_r_test.cpp
@@ -55,10 +55,10 @@
// 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