diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -575,10 +575,12 @@ #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR static inline void StrstrCheck(void *ctx, char *r, const char *s1, const char *s2) { - uptr len1 = internal_strlen(s1); - uptr len2 = internal_strlen(s2); - COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1); - COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1); + uptr len2 = internal_strlen(s2); + COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1); + if (len2 == 0 && !common_flags()->strict_string_checks) + return; + uptr len1 = internal_strlen(s1); + COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1); } #endif diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_libc.cpp --- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.cpp @@ -217,8 +217,10 @@ char *internal_strstr(const char *haystack, const char *needle) { // This is O(N^2), but we are not using it in hot places. - uptr len1 = internal_strlen(haystack); uptr len2 = internal_strlen(needle); + if (len2 == 0) + return const_cast(haystack); + uptr len1 = internal_strlen(haystack); if (len1 < len2) return nullptr; for (uptr pos = 0; pos <= len1 - len2; pos++) { if (internal_memcmp(haystack + pos, needle, len2) == 0) diff --git a/compiler-rt/test/sanitizer_common/TestCases/strstr.c b/compiler-rt/test/sanitizer_common/TestCases/strstr.c --- a/compiler-rt/test/sanitizer_common/TestCases/strstr.c +++ b/compiler-rt/test/sanitizer_common/TestCases/strstr.c @@ -8,5 +8,9 @@ char s2[] = "b"; r = strstr(s1, s2); assert(r == s1 + 1); + char *s3 = NULL; + char *s4 = ""; + char *p = strstr(s3, s4); + assert(p == NULL); return 0; }