diff --git a/compiler-rt/lib/dfsan/dfsan_custom.cpp b/compiler-rt/lib/dfsan/dfsan_custom.cpp --- a/compiler-rt/lib/dfsan/dfsan_custom.cpp +++ b/compiler-rt/lib/dfsan/dfsan_custom.cpp @@ -84,7 +84,13 @@ *ret_label = dfsan_union(dfsan_read_label(s, i + 1), dfsan_union(s_label, c_label)); } - return s[i] == 0 ? nullptr : const_cast(s+i); + + // If s[i] is the \0 at the end of the string, and \0 is not the + // character we are searching for, then return null. + if (s[i] == 0 && c != 0) { + return nullptr; + } + return const_cast(s + i); } } } diff --git a/compiler-rt/test/dfsan/custom.cpp b/compiler-rt/test/dfsan/custom.cpp --- a/compiler-rt/test/dfsan/custom.cpp +++ b/compiler-rt/test/dfsan/custom.cpp @@ -250,6 +250,17 @@ #else ASSERT_LABEL(crv, i_label); #endif + + // `man strchr` says: + // The terminating null byte is considered part of the string, so that if c + // is specified as '\0', these functions return a pointer to the terminator. + crv = strchr(str1, '\0'); + assert(crv == &str1[4]); +#ifdef STRICT_DATA_DEPENDENCIES + ASSERT_ZERO_LABEL(crv); +#else + ASSERT_LABEL(crv, i_label); +#endif } void test_calloc() {