diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp --- a/compiler-rt/lib/msan/msan_interceptors.cpp +++ b/compiler-rt/lib/msan/msan_interceptors.cpp @@ -827,7 +827,7 @@ INTERCEPTOR(int, gethostname, char *name, SIZE_T len) { ENSURE_MSAN_INITED(); int res = REAL(gethostname)(name, len); - if (!res) { + if (!res || (res == -1 && errno == errno_ENAMETOOLONG)) { SIZE_T real_len = REAL(strnlen)(name, len); if (real_len < len) ++real_len; diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -587,6 +587,20 @@ return res; } +TEST(MemorySanitizer, strcmp) { + char s1[10]; + char s2[10]; + strncpy(s1, "foo", 10); + s2[0] = 'f'; + s2[1] = 'n'; + EXPECT_GT(strcmp(s1, s2), 0); + s2[1] = 'o'; + int res; + EXPECT_UMR(res = strcmp(s1, s2)); + EXPECT_NOT_POISONED(res); + EXPECT_EQ(strncmp(s1, s2, 1), 0); +} + TEST(MemorySanitizer, LargeRet) { LargeStruct a = LargeRetTest(); EXPECT_POISONED(a.x[0]); @@ -1100,7 +1114,6 @@ } while (0) TEST(MemorySanitizer, gethostent) { - sethostent(0); struct hostent *he = gethostent(); ASSERT_NE((void *)NULL, he); EXPECT_HOSTENT_NOT_POISONED(he); @@ -1164,7 +1177,6 @@ #if !defined(__NetBSD__) TEST(MemorySanitizer, gethostent_r) { - sethostent(0); char buf[2000]; struct hostent he; struct hostent *result; @@ -3535,9 +3547,14 @@ } TEST(MemorySanitizer, gethostname) { - char buf[100]; - int res = gethostname(buf, 100); - ASSERT_EQ(0, res); + char buf[1000]; + EXPECT_EQ(-1, gethostname(buf, 1)); + EXPECT_EQ(ENAMETOOLONG, errno); + EXPECT_NOT_POISONED(buf[0]); + EXPECT_POISONED(buf[1]); + + __msan_poison(buf, sizeof(buf)); + EXPECT_EQ(0, gethostname(buf, sizeof(buf))); EXPECT_NOT_POISONED(strlen(buf)); } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_errno_codes.h b/compiler-rt/lib/sanitizer_common/sanitizer_errno_codes.h --- a/compiler-rt/lib/sanitizer_common/sanitizer_errno_codes.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_errno_codes.h @@ -24,6 +24,7 @@ #define errno_ENOMEM 12 #define errno_EBUSY 16 #define errno_EINVAL 22 +#define errno_ENAMETOOLONG 36 // Those might not present or their value differ on different platforms. extern const int errno_EOWNERDEAD;