[PATCH] D109050: [FileCheck] Use StringRef for MatchRegexp to fix crash.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 1 01:55:02 PDT 2021
fhahn created this revision.
fhahn added reviewers: jdenny, jhenderson, probinson, thopre.
Herald added a subscriber: hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D109050
Files:
llvm/lib/FileCheck/FileCheck.cpp
llvm/test/FileCheck/invalid-regex.txt
Index: llvm/test/FileCheck/invalid-regex.txt
===================================================================
--- /dev/null
+++ llvm/test/FileCheck/invalid-regex.txt
@@ -0,0 +1,19 @@
+# This file contains invalid regular expressions in variable patterns. Make
+# sure a proper error message is presented
+//------------------------------------------------
+RUN: %ProtectFileCheckOutput \
+RUN: not FileCheck -check-prefix=CHECK-STAR %s < /dev/null 2>&1 | \
+RUN: FileCheck -check-prefix=ERR-STAR %s
+
+CHECK-STAR: [[BOOM:*]]
+ERR-STAR: error: invalid regex: repetition-operator operand invalid
+
+//------------------------------------------------
+RUN: %ProtectFileCheckOutput \
+RUN: not FileCheck -check-prefix=CHECK-PLUS %s < /dev/null 2>&1 | \
+RUN: FileCheck -check-prefix=ERR-PLUS %s
+
+CHECK-PLUS: [[BOOM:+]]
+ERR-PLUS: error: invalid regex: repetition-operator operand invalid
+
+//------------------------------------------------
Index: llvm/lib/FileCheck/FileCheck.cpp
===================================================================
--- llvm/lib/FileCheck/FileCheck.cpp
+++ llvm/lib/FileCheck/FileCheck.cpp
@@ -1034,7 +1034,8 @@
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 @@
return true;
}
DefName = Name;
- MatchRegexp = MatchStr.str();
+ MatchRegexp = MatchStr;
} else {
if (IsPseudo) {
MatchStr = OrigMatchStr;
@@ -1117,7 +1118,8 @@
SubstStr = MatchStr;
else {
ExpressionFormat Format = ExpressionPointer->getFormat();
- MatchRegexp = cantFail(Format.getWildcardRegex());
+ WildcardRegexp = cantFail(Format.getWildcardRegex());
+ MatchRegexp = WildcardRegexp;
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109050.369885.patch
Type: text/x-patch
Size: 2025 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210901/4effc878/attachment.bin>
More information about the llvm-commits
mailing list