aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/FileCheck/FileCheck.cpp
diff options
context:
space:
mode:
authorFlorian Hahn <flo@fhahn.com>2021-09-01 13:41:20 +0200
committerFlorian Hahn <flo@fhahn.com>2021-09-01 14:27:14 +0200
commita3d357e504875f4f6c2b3c2674eb84a7b4101cb9 (patch)
tree070c5caa97a032703fcd4b395426e018c51d22ce /llvm/lib/FileCheck/FileCheck.cpp
parent2498f8fd76c2376b77f5c493da8873f049394916 (diff)
downloadllvm-a3d357e504875f4f6c2b3c2674eb84a7b4101cb9.zip
llvm-a3d357e504875f4f6c2b3c2674eb84a7b4101cb9.tar.gz
llvm-a3d357e504875f4f6c2b3c2674eb84a7b4101cb9.tar.bz2
[FileCheck] Use StringRef for MatchRegexp to fix crash.
If MatchRegexp is an invalid regex, an error message will be printed using SourceManager::PrintMessage via AddRegExToRegEx. PrintMessage relies on the input being a StringRef into a string managed by SourceManager. At the moment, a StringRef to a std::string allocated in the caller of AddRegExToRegEx is passed. If the regex is invalid, this StringRef is passed to PrintMessage, where it will crash, because it does not point to a string managed via SourceMgr. This patch fixes the crash by turning MatchRegexp into a StringRef If we use MatchStr, we directly use that StringRef, which points into a string from SourceMgr. Otherwise, MatchRegexp gets assigned Format.getWildcardRegex(), which returns a std::string. To extend the lifetime, assign it to a std::string variable WildcardRegexp and assign MatchRegexp to a stringref to WildcardRegexp. WildcardRegexp should always be valid, so we should never have to print an error message via the SoureMgr I think. Fixes PR49319. Reviewed By: thopre Differential Revision: https://reviews.llvm.org/D109050
Diffstat (limited to 'llvm/lib/FileCheck/FileCheck.cpp')
-rw-r--r--llvm/lib/FileCheck/FileCheck.cpp8
1 files changed, 5 insertions, 3 deletions
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 04476d9..d4326ef 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1034,7 +1034,8 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
bool IsLegacyLineExpr = false;
StringRef DefName;
StringRef SubstStr;
- std::string MatchRegexp;
+ StringRef MatchRegexp;
+ std::string WildcardRegexp;
size_t SubstInsertIdx = RegExStr.size();
// Parse string variable or legacy @LINE expression.
@@ -1078,7 +1079,7 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
return true;
}
DefName = Name;
- MatchRegexp = MatchStr.str();
+ MatchRegexp = MatchStr;
} else {
if (IsPseudo) {
MatchStr = OrigMatchStr;
@@ -1117,7 +1118,8 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
SubstStr = MatchStr;
else {
ExpressionFormat Format = ExpressionPointer->getFormat();
- MatchRegexp = cantFail(Format.getWildcardRegex());
+ WildcardRegexp = cantFail(Format.getWildcardRegex());
+ MatchRegexp = WildcardRegexp;
}
}