Skip to content

Commit a3d357e

Browse files
committed
[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
1 parent 2498f8f commit a3d357e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,8 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
10341034
bool IsLegacyLineExpr = false;
10351035
StringRef DefName;
10361036
StringRef SubstStr;
1037-
std::string MatchRegexp;
1037+
StringRef MatchRegexp;
1038+
std::string WildcardRegexp;
10381039
size_t SubstInsertIdx = RegExStr.size();
10391040

10401041
// Parse string variable or legacy @LINE expression.
@@ -1078,7 +1079,7 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
10781079
return true;
10791080
}
10801081
DefName = Name;
1081-
MatchRegexp = MatchStr.str();
1082+
MatchRegexp = MatchStr;
10821083
} else {
10831084
if (IsPseudo) {
10841085
MatchStr = OrigMatchStr;
@@ -1117,7 +1118,8 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
11171118
SubstStr = MatchStr;
11181119
else {
11191120
ExpressionFormat Format = ExpressionPointer->getFormat();
1120-
MatchRegexp = cantFail(Format.getWildcardRegex());
1121+
WildcardRegexp = cantFail(Format.getWildcardRegex());
1122+
MatchRegexp = WildcardRegexp;
11211123
}
11221124
}
11231125

llvm/test/FileCheck/invalid-regex.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file contains invalid regular expressions in variable patterns. Make
2+
# sure a proper error message is presented
3+
//------------------------------------------------
4+
RUN: %ProtectFileCheckOutput \
5+
RUN: not FileCheck -check-prefix=CHECK-STAR %s < /dev/null 2>&1 | \
6+
RUN: FileCheck -check-prefix=ERR-STAR %s
7+
8+
CHECK-STAR: [[BOOM:*]]
9+
ERR-STAR: error: invalid regex: repetition-operator operand invalid
10+
11+
//------------------------------------------------
12+
RUN: %ProtectFileCheckOutput \
13+
RUN: not FileCheck -check-prefix=CHECK-PLUS %s < /dev/null 2>&1 | \
14+
RUN: FileCheck -check-prefix=ERR-PLUS %s
15+
16+
CHECK-PLUS: [[BOOM:+]]
17+
ERR-PLUS: error: invalid regex: repetition-operator operand invalid
18+
19+
//------------------------------------------------

0 commit comments

Comments
 (0)