From 7814f7dafdb9f1afaf8078d37f62d01109ebbf0a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 10 Feb 2016 20:02:04 +0000 Subject: [PATCH] Don't assume that there is only one strchr overload in the global namespace; that's not true in general. Instead, use a preference order to pick the standard C++ signature 'char*(char*, int)' where possible and fall back to the C signature 'char*(const char*, int)' only when it's unavailable. llvm-svn: 260425 --- compiler-rt/lib/asan/tests/asan_str_test.cc | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/compiler-rt/lib/asan/tests/asan_str_test.cc b/compiler-rt/lib/asan/tests/asan_str_test.cc index 89b0d3d..5ed7aa0 100644 --- a/compiler-rt/lib/asan/tests/asan_str_test.cc +++ b/compiler-rt/lib/asan/tests/asan_str_test.cc @@ -186,7 +186,8 @@ TEST(AddressSanitizer, StrNCpyOOBTest) { typedef char*(*PointerToStrChr1)(const char*, int); typedef char*(*PointerToStrChr2)(char*, int); -UNUSED static void RunStrChrTest(PointerToStrChr1 StrChr) { +template +static void RunStrChrTestImpl(StrChrFn *StrChr) { size_t size = Ident(100); char *str = MallocAndMemsetString(size); str[10] = 'q'; @@ -202,28 +203,20 @@ UNUSED static void RunStrChrTest(PointerToStrChr1 StrChr) { EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBReadMessage(0)); free(str); } -UNUSED static void RunStrChrTest(PointerToStrChr2 StrChr) { - size_t size = Ident(100); - char *str = MallocAndMemsetString(size); - str[10] = 'q'; - str[11] = '\0'; - EXPECT_EQ(str, StrChr(str, 'z')); - EXPECT_EQ(str + 10, StrChr(str, 'q')); - EXPECT_EQ(NULL, StrChr(str, 'a')); - // StrChr argument points to not allocated memory. - EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBReadMessage(1)); - EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBReadMessage(0)); - // Overwrite the terminator and hit not allocated memory. - str[11] = 'z'; - EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBReadMessage(0)); - free(str); + +// Prefer to use the standard signature if both are available. +UNUSED static void RunStrChrTest(PointerToStrChr1 StrChr, ...) { + RunStrChrTestImpl(StrChr); +} +UNUSED static void RunStrChrTest(PointerToStrChr2 StrChr, int) { + RunStrChrTestImpl(StrChr); } TEST(AddressSanitizer, StrChrAndIndexOOBTest) { - RunStrChrTest(&strchr); + RunStrChrTest(&strchr, 0); // No index() on Windows and on Android L. #if !defined(_WIN32) && !defined(__ANDROID__) - RunStrChrTest(&index); + RunStrChrTest(&index, 0); #endif } -- 2.7.4