Fix off-by-one error in Regex::isValid
authorAlexey Samsonov <samsonov@google.com>
Thu, 8 Aug 2013 17:32:45 +0000 (17:32 +0000)
committerAlexey Samsonov <samsonov@google.com>
Thu, 8 Aug 2013 17:32:45 +0000 (17:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187992 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Regex.cpp
unittests/Support/RegexTest.cpp

index dec967e33f5b886abc484c00f3da3ce1d653bc02..541364184073006b42a047b1d4b6cb75e5760faa 100644 (file)
@@ -43,7 +43,7 @@ bool Regex::isValid(std::string &Error) {
   
   size_t len = llvm_regerror(error, preg, NULL, 0);
   
-  Error.resize(len);
+  Error.resize(len - 1);
   llvm_regerror(error, preg, &Error[0], len);
   return false;
 }
index 02869b3ed4671fe1da1850ee65418ed31dc450d9..7b977f7446681d122d1c8cf0912429086bde2913 100644 (file)
@@ -127,4 +127,12 @@ TEST_F(RegexTest, IsLiteralERE) {
   EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}"));
 }
 
+TEST_F(RegexTest, IsValid) {
+  std::string Error;
+  EXPECT_FALSE(Regex("(foo").isValid(Error));
+  EXPECT_EQ("parentheses not balanced", Error);
+  EXPECT_FALSE(Regex("a[b-").isValid(Error));
+  EXPECT_EQ("invalid character range", Error);
+}
+
 }