diff --git a/libc/src/string/strstr.cpp b/libc/src/string/strstr.cpp --- a/libc/src/string/strstr.cpp +++ b/libc/src/string/strstr.cpp @@ -16,6 +16,11 @@ // TODO: This is a simple brute force implementation. This can be // improved upon using well known string matching algorithms. char *LLVM_LIBC_ENTRYPOINT(strstr)(const char *haystack, const char *needle) { + // The standard requires that haystack is returned if the needle is an empty + // string. + if (*needle == '\0') + return const_cast(haystack); + for (size_t i = 0; haystack[i]; ++i) { size_t j; for (j = 0; haystack[i + j] && haystack[i + j] == needle[j]; ++j) diff --git a/libc/utils/UnitTest/Test.cpp b/libc/utils/UnitTest/Test.cpp --- a/libc/utils/UnitTest/Test.cpp +++ b/libc/utils/UnitTest/Test.cpp @@ -242,6 +242,10 @@ bool Test::testStrEq(RunContext &Ctx, const char *LHS, const char *RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { + if (LHS == nullptr) + LHS = ""; + if (RHS == nullptr) + RHS = ""; return internal::test(Ctx, Cond_EQ, llvm::StringRef(LHS), llvm::StringRef(RHS), LHSStr, RHSStr, File, Line); } @@ -249,6 +253,10 @@ bool Test::testStrNe(RunContext &Ctx, const char *LHS, const char *RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { + if (LHS == nullptr) + LHS = ""; + if (RHS == nullptr) + RHS = ""; return internal::test(Ctx, Cond_NE, llvm::StringRef(LHS), llvm::StringRef(RHS), LHSStr, RHSStr, File, Line); }