]> granicus.if.org Git - llvm/commitdiff
Fix stack-use-after free after r359580
authorNico Weber <nicolasweber@gmx.de>
Tue, 30 Apr 2019 19:43:35 +0000 (19:43 +0000)
committerNico Weber <nicolasweber@gmx.de>
Tue, 30 Apr 2019 19:43:35 +0000 (19:43 +0000)
`Candidate` was a StringRef refering to a temporary string.
Instead, create a local variable for the string and use
a StringRef referring to that.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359604 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Option/OptTable.cpp

index 8956c6830dbb7685d24f443bc5c65dcdd2c2d2a2..1585059c2fbaaeec781a64e8e0a0834e995e0539 100644 (file)
@@ -296,10 +296,11 @@ unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
     // "--help" over "-help".
     for (int P = 0; const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
       std::string NormalizedName = (LHS + Delimiter).str();
-      StringRef Candidate = (CandidatePrefix + CandidateName).str();
+      std::string Candidate = (CandidatePrefix + CandidateName).str();
+      StringRef CandidateRef = Candidate;
       unsigned Distance =
-          Candidate.edit_distance(NormalizedName, /*AllowReplacements=*/true,
-                                  /*MaxEditDistance=*/BestDistance);
+          CandidateRef.edit_distance(NormalizedName, /*AllowReplacements=*/true,
+                                     /*MaxEditDistance=*/BestDistance);
       if (Distance < BestDistance) {
         BestDistance = Distance;
         NearestString = (Candidate + RHS).str();