[llvm] r359604 - Fix stack-use-after free after r359580

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 12:43:36 PDT 2019


Author: nico
Date: Tue Apr 30 12:43:35 2019
New Revision: 359604

URL: http://llvm.org/viewvc/llvm-project?rev=359604&view=rev
Log:
Fix stack-use-after free after r359580

`Candidate` was a StringRef refering to a temporary string.
Instead, create a local variable for the string and use
a StringRef referring to that.

Modified:
    llvm/trunk/lib/Option/OptTable.cpp

Modified: llvm/trunk/lib/Option/OptTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Option/OptTable.cpp?rev=359604&r1=359603&r2=359604&view=diff
==============================================================================
--- llvm/trunk/lib/Option/OptTable.cpp (original)
+++ llvm/trunk/lib/Option/OptTable.cpp Tue Apr 30 12:43:35 2019
@@ -296,10 +296,11 @@ unsigned OptTable::findNearest(StringRef
     // "--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();




More information about the llvm-commits mailing list