[PATCH] D24123: [Support] - Fix possible crash in match() of llvm::Regex.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 2 01:53:14 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL280473: [Support] - Fix possible crash in match() of llvm::Regex. (authored by grimar).

Changed prior to commit:
  https://reviews.llvm.org/D24123?vs=69977&id=70137#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D24123

Files:
  llvm/trunk/include/llvm/Support/Regex.h
  llvm/trunk/lib/Support/Regex.cpp
  llvm/trunk/unittests/Support/RegexTest.cpp

Index: llvm/trunk/include/llvm/Support/Regex.h
===================================================================
--- llvm/trunk/include/llvm/Support/Regex.h
+++ llvm/trunk/include/llvm/Support/Regex.h
@@ -52,11 +52,7 @@
       std::swap(error, regex.error);
       return *this;
     }
-    Regex(Regex &&regex) {
-      preg = regex.preg;
-      error = regex.error;
-      regex.preg = nullptr;
-    }
+    Regex(Regex &&regex);
     ~Regex();
 
     /// isValid - returns the error encountered during regex compilation, or
Index: llvm/trunk/lib/Support/Regex.cpp
===================================================================
--- llvm/trunk/lib/Support/Regex.cpp
+++ llvm/trunk/lib/Support/Regex.cpp
@@ -34,6 +34,13 @@
   error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
 }
 
+Regex::Regex(Regex &&regex) {
+  preg = regex.preg;
+  error = regex.error;
+  regex.preg = nullptr;
+  regex.error = REG_BADPAT;
+}
+
 Regex::~Regex() {
   if (preg) {
     llvm_regfree(preg);
@@ -59,6 +66,9 @@
 }
 
 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.
Index: llvm/trunk/unittests/Support/RegexTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/RegexTest.cpp
+++ llvm/trunk/unittests/Support/RegexTest.cpp
@@ -151,6 +151,8 @@
   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 @@
   EXPECT_TRUE(r1.isValid(Error));
 }
 
+TEST_F(RegexTest, MatchInvalid) {
+  Regex r1;
+  std::string Error;
+  EXPECT_FALSE(r1.isValid(Error));
+  EXPECT_FALSE(r1.match("X"));
+}
+
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24123.70137.patch
Type: text/x-patch
Size: 1843 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160902/e647abe8/attachment.bin>


More information about the llvm-commits mailing list