[llvm] r280473 - [Support] - Fix possible crash in match() of llvm::Regex.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 2 01:44:47 PDT 2016
Author: grimar
Date: Fri Sep 2 03:44:46 2016
New Revision: 280473
URL: http://llvm.org/viewvc/llvm-project?rev=280473&view=rev
Log:
[Support] - Fix possible crash in match() of llvm::Regex.
Crash was possible if match() method
was called on object that was moved or object
created with empty constructor.
Testcases updated.
DIfferential revision: https://reviews.llvm.org/D24123
Modified:
llvm/trunk/include/llvm/Support/Regex.h
llvm/trunk/lib/Support/Regex.cpp
llvm/trunk/unittests/Support/RegexTest.cpp
Modified: llvm/trunk/include/llvm/Support/Regex.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Regex.h?rev=280473&r1=280472&r2=280473&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Regex.h (original)
+++ llvm/trunk/include/llvm/Support/Regex.h Fri Sep 2 03:44:46 2016
@@ -52,11 +52,7 @@ namespace llvm {
std::swap(error, regex.error);
return *this;
}
- Regex(Regex &®ex) {
- preg = regex.preg;
- error = regex.error;
- regex.preg = nullptr;
- }
+ Regex(Regex &®ex);
~Regex();
/// isValid - returns the error encountered during regex compilation, or
Modified: llvm/trunk/lib/Support/Regex.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Regex.cpp?rev=280473&r1=280472&r2=280473&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Regex.cpp (original)
+++ llvm/trunk/lib/Support/Regex.cpp Fri Sep 2 03:44:46 2016
@@ -34,6 +34,13 @@ Regex::Regex(StringRef regex, unsigned F
error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
}
+Regex::Regex(Regex &®ex) {
+ preg = regex.preg;
+ error = regex.error;
+ regex.preg = nullptr;
+ regex.error = REG_BADPAT;
+}
+
Regex::~Regex() {
if (preg) {
llvm_regfree(preg);
@@ -59,6 +66,9 @@ unsigned Regex::getNumMatches() const {
}
bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches){
+ if (error)
+ return false;
+
unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
// pmatch needs to have at least one element.
Modified: llvm/trunk/unittests/Support/RegexTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/RegexTest.cpp?rev=280473&r1=280472&r2=280473&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/RegexTest.cpp (original)
+++ llvm/trunk/unittests/Support/RegexTest.cpp Fri Sep 2 03:44:46 2016
@@ -151,6 +151,8 @@ TEST_F(RegexTest, MoveAssign) {
Regex r2("abc");
r2 = std::move(r1);
EXPECT_TRUE(r2.match("916"));
+ std::string Error;
+ EXPECT_FALSE(r1.isValid(Error));
}
TEST_F(RegexTest, NoArgConstructor) {
@@ -162,4 +164,11 @@ TEST_F(RegexTest, NoArgConstructor) {
EXPECT_TRUE(r1.isValid(Error));
}
+TEST_F(RegexTest, MatchInvalid) {
+ Regex r1;
+ std::string Error;
+ EXPECT_FALSE(r1.isValid(Error));
+ EXPECT_FALSE(r1.match("X"));
+}
+
}
More information about the llvm-commits
mailing list