diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index d7fa99dbde27..393504cf01b0 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -372,7 +372,7 @@ void StringRef::split(SmallVectorImpl &A, char Separator, size_t StringRef::count(StringRef Str) const { size_t Count = 0; size_t N = Str.size(); - if (N > Length) + if (!N || N > Length) return 0; for (size_t i = 0, e = Length - N + 1; i < e;) { if (substr(i, N).equals(Str)) { diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index cbb2a30ff17a..9cb24bfdb4db 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -509,6 +509,7 @@ TEST(StringRefTest, Count) { EXPECT_EQ(1U, Str.count("hello")); EXPECT_EQ(1U, Str.count("ello")); EXPECT_EQ(0U, Str.count("zz")); + EXPECT_EQ(0U, Str.count("")); StringRef OverlappingAbba("abbabba"); EXPECT_EQ(1U, OverlappingAbba.count("abba")); diff --git a/polly/lib/Analysis/ScopDetection.cpp b/polly/lib/Analysis/ScopDetection.cpp index cf6ba75648df..59176d2a0ae9 100644 --- a/polly/lib/Analysis/ScopDetection.cpp +++ b/polly/lib/Analysis/ScopDetection.cpp @@ -1653,7 +1653,8 @@ bool ScopDetection::isValidRegion(DetectionContext &Context) const { CurRegion.getExit(), DbgLoc); } - if (!CurRegion.getEntry()->getName().count(OnlyRegion)) { + if (!OnlyRegion.empty() && + !CurRegion.getEntry()->getName().count(OnlyRegion)) { LLVM_DEBUG({ dbgs() << "Region entry does not match -polly-region-only"; dbgs() << "\n";