diff options
author | Elliott Hughes <enh@google.com> | 2014-08-18 17:28:32 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2014-08-28 16:37:09 -0700 |
commit | 416d7ddaff0946d480b6aa945a741b3eeaca5569 (patch) | |
tree | d3fa6382f7d6d26ab7e3ecb95a715328e5f50b5e /tests/string_test.cpp | |
parent | f4e721dd519db89c504c8944763811a3df956b32 (diff) | |
download | bionic-416d7ddaff0946d480b6aa945a741b3eeaca5569.zip bionic-416d7ddaff0946d480b6aa945a741b3eeaca5569.tar.gz bionic-416d7ddaff0946d480b6aa945a741b3eeaca5569.tar.bz2 |
Add GNU-compatible strerror_r.
We already had the POSIX strerror_r, but some third-party code defines
_GNU_SOURCE and expects to get the GNU strerror_r instead.
This exposed a bug in the libc internal logging functions where unlike
their standard brethren they wouldn't return the number of bytes they'd
have liked to have written.
Bug: 16243479
Change-Id: I1745752ccbdc569646d34f5071f6df2be066d5f4
Diffstat (limited to 'tests/string_test.cpp')
-rw-r--r-- | tests/string_test.cpp | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/tests/string_test.cpp b/tests/string_test.cpp index a3c6abb..7db8e21 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#define _GNU_SOURCE 1 + #include <string.h> #include <errno.h> @@ -72,28 +74,34 @@ TEST(string, strerror_concurrent) { #endif // __BIONIC__ } -TEST(string, strerror_r) { -#if defined(__BIONIC__) // glibc's strerror_r doesn't even have the same signature as the POSIX one. +TEST(string, gnu_strerror_r) { char buf[256]; + // Note that glibc doesn't necessarily write into the buffer. + // Valid. - ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf))); + ASSERT_STREQ("Success", strerror_r(0, buf, sizeof(buf))); +#if defined(__BIONIC__) ASSERT_STREQ("Success", buf); - ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf))); +#endif + ASSERT_STREQ("Operation not permitted", strerror_r(1, buf, sizeof(buf))); +#if defined(__BIONIC__) ASSERT_STREQ("Operation not permitted", buf); +#endif // Invalid. - ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf))); + ASSERT_STREQ("Unknown error -1", strerror_r(-1, buf, sizeof(buf))); ASSERT_STREQ("Unknown error -1", buf); - ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf))); + ASSERT_STREQ("Unknown error 1234", strerror_r(1234, buf, sizeof(buf))); ASSERT_STREQ("Unknown error 1234", buf); // Buffer too small. - ASSERT_EQ(-1, strerror_r(0, buf, 2)); - ASSERT_EQ(ERANGE, errno); -#else // __BIONIC__ - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif // __BIONIC__ + errno = 0; + memset(buf, 0, sizeof(buf)); + ASSERT_EQ(buf, strerror_r(4567, buf, 2)); + ASSERT_STREQ("U", buf); + // The GNU strerror_r doesn't set errno (the POSIX one sets it to ERANGE). + ASSERT_EQ(0, errno); } TEST(string, strsignal) { |