llvm.org GIT mirror llvm / e1bd92b
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280473 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 4 years ago
3 changed file(s) with 20 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
5151 std::swap(error, regex.error);
5252 return *this;
5353 }
54 Regex(Regex &®ex) {
55 preg = regex.preg;
56 error = regex.error;
57 regex.preg = nullptr;
58 }
54 Regex(Regex &®ex);
5955 ~Regex();
6056
6157 /// isValid - returns the error encountered during regex compilation, or
3333 error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
3434 }
3535
36 Regex::Regex(Regex &®ex) {
37 preg = regex.preg;
38 error = regex.error;
39 regex.preg = nullptr;
40 regex.error = REG_BADPAT;
41 }
42
3643 Regex::~Regex() {
3744 if (preg) {
3845 llvm_regfree(preg);
5865 }
5966
6067 bool Regex::match(StringRef String, SmallVectorImpl *Matches){
68 if (error)
69 return false;
70
6171 unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
6272
6373 // pmatch needs to have at least one element.
150150 Regex r2("abc");
151151 r2 = std::move(r1);
152152 EXPECT_TRUE(r2.match("916"));
153 std::string Error;
154 EXPECT_FALSE(r1.isValid(Error));
153155 }
154156
155157 TEST_F(RegexTest, NoArgConstructor) {
161163 EXPECT_TRUE(r1.isValid(Error));
162164 }
163165
166 TEST_F(RegexTest, MatchInvalid) {
167 Regex r1;
168 std::string Error;
169 EXPECT_FALSE(r1.isValid(Error));
170 EXPECT_FALSE(r1.match("X"));
164171 }
172
173 }