[clang-tools-extra] r322443 - [clangd] Fix uninitialized-read found by asan

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 13 08:46:26 PST 2018


Author: sammccall
Date: Sat Jan 13 08:46:26 2018
New Revision: 322443

URL: http://llvm.org/viewvc/llvm-project?rev=322443&view=rev
Log:
[clangd] Fix uninitialized-read found by asan

Modified:
    clang-tools-extra/trunk/clangd/FuzzyMatch.cpp
    clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp

Modified: clang-tools-extra/trunk/clangd/FuzzyMatch.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FuzzyMatch.cpp?rev=322443&r1=322442&r2=322443&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/FuzzyMatch.cpp (original)
+++ clang-tools-extra/trunk/clangd/FuzzyMatch.cpp Sat Jan 13 08:46:26 2018
@@ -87,14 +87,15 @@ FuzzyMatcher::FuzzyMatcher(StringRef Pat
     for (int W = 0; W < P; ++W)
       for (Action A : {Miss, Match})
         Scores[P][W][A] = {AwfulScore, Miss};
-  calculateRoles(Pat, PatRole, PatN);
+  if (PatN > 0)
+    calculateRoles(Pat, PatRole, PatN);
 }
 
 Optional<float> FuzzyMatcher::match(StringRef Word) {
-  if (!PatN)
-    return 1;
   if (!(WordContainsPattern = init(Word)))
     return None;
+  if (!PatN)
+    return 1;
   buildGraph();
   auto Best = std::max(Scores[PatN][WordN][Miss].Score,
                        Scores[PatN][WordN][Match].Score);
@@ -177,6 +178,7 @@ template <typename T> static T packedLoo
   return static_cast<T>((Data[I >> 2] >> ((I & 3) * 2)) & 3);
 }
 void FuzzyMatcher::calculateRoles(const char *Text, CharRole *Out, int N) {
+  assert(N > 0);
   // Types holds a sliding window of (Prev, Curr, Next) types.
   // Initial value is (Empty, Empty, type of Text[0]).
   int Types = packedLookup<CharType>(CharTypes, Text[0]);
@@ -199,6 +201,8 @@ bool FuzzyMatcher::init(StringRef NewWor
   if (PatN > WordN)
     return false;
   memcpy(Word, NewWord.data(), WordN);
+  if (PatN == 0)
+    return true;
   for (int I = 0; I < WordN; ++I)
     LowWord[I] = lower(Word[I]);
 
@@ -293,6 +297,14 @@ llvm::SmallString<256> FuzzyMatcher::dum
   llvm::SmallString<256> Result;
   OS << "=== Match \"" << StringRef(Word, WordN) << "\" against ["
      << StringRef(Pat, PatN) << "] ===\n";
+  if (PatN == 0) {
+    OS << "Pattern is empty: perfect match.\n";
+    return Result = StringRef(Word, WordN);
+  }
+  if (WordN == 0) {
+    OS << "Word is empty: no match.\n";
+    return Result;
+  }
   if (!WordContainsPattern) {
     OS << "Substring check failed.\n";
     return Result;

Modified: clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp?rev=322443&r1=322442&r2=322443&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp Sat Jan 13 08:46:26 2018
@@ -58,6 +58,7 @@ testing::Matcher<StringRef> matches(Stri
 }
 
 TEST(FuzzyMatch, Matches) {
+  EXPECT_THAT("", matches("unique_ptr"));
   EXPECT_THAT("u_p", matches("[u]nique[_p]tr"));
   EXPECT_THAT("up", matches("[u]nique_[p]tr"));
   EXPECT_THAT("uq", matches("[u]ni[q]ue_ptr"));




More information about the cfe-commits mailing list