From ecd8fa3437d5f1f0560cba296f55ceeddb58baff Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 23:04:40 +0800 Subject: [PATCH] Improve coverage of regex --- include/rapidjson/internal/regex.h | 12 +++++------- test/unittest/regextest.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index d317daa3a..c20629427 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -375,14 +375,14 @@ class GenericRegex { bool Eval(Stack& operandStack, Operator op) { switch (op) { case kConcatenation: - if (operandStack.GetSize() >= sizeof(Frag) * 2) { + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2); + { Frag e2 = *operandStack.template Pop(1); Frag e1 = *operandStack.template Pop(1); Patch(e1.out, e2.start); *operandStack.template Push() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex)); - return true; } - return false; + return true; case kAlternation: if (operandStack.GetSize() >= sizeof(Frag) * 2) { @@ -430,8 +430,7 @@ class GenericRegex { bool EvalQuantifier(Stack& operandStack, unsigned n, unsigned m) { RAPIDJSON_ASSERT(n <= m); - if (operandStack.GetSize() < sizeof(Frag)) - return false; + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag)); if (n == 0) { if (m == 0) // a{0} not support @@ -647,8 +646,7 @@ class GenericRegex { // Return whether the added states is a match state bool AddState(Stack& l, SizeType index) const { - if (index == kRegexInvalidState) - return true; + RAPIDJSON_ASSERT(index != kRegexInvalidState); const State& s = GetState(index); if (s.out1 != kRegexInvalidState) { // Split diff --git a/test/unittest/regextest.cpp b/test/unittest/regextest.cpp index e3371d168..b497df6ab 100644 --- a/test/unittest/regextest.cpp +++ b/test/unittest/regextest.cpp @@ -17,6 +17,14 @@ using namespace rapidjson::internal; +TEST(Regex, Single) { + Regex re("a"); + ASSERT_TRUE(re.IsValid()); + EXPECT_TRUE(re.Match("a")); + EXPECT_FALSE(re.Match("")); + EXPECT_FALSE(re.Match("b")); +} + TEST(Regex, Concatenation) { Regex re("abc"); ASSERT_TRUE(re.IsValid()); @@ -560,6 +568,9 @@ TEST(Regex, Invalid) { TEST_INVALID("a{1,0}"); TEST_INVALID("a{-1,0}"); TEST_INVALID("a{-1,1}"); + TEST_INVALID("a{4294967296}"); // overflow of unsigned + TEST_INVALID("a{1a}"); + TEST_INVALID("["); TEST_INVALID("[]"); TEST_INVALID("[^]"); TEST_INVALID("[\\a]");