소스 검색

fix: caching null regex

Sam Jaffe 2 주 전
부모
커밋
3d8025bf6c
2개의 변경된 파일10개의 추가작업 그리고 2개의 파일을 삭제
  1. 5 0
      include/jvalidate/regex.h
  2. 5 2
      tests/regex_test.cxx

+ 5 - 0
include/jvalidate/regex.h

@@ -102,10 +102,15 @@ public:
       }
     }
 
+    if (it->second == nullptr) {
+      return false; // Regex was invalid - and we cached that
+    }
+
     UErrorCode status = U_ZERO_ERROR;
     icu::UnicodeString const ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(text));
     std::unique_ptr<icu::RegexMatcher> matcher(it->second->matcher(ucs, status));
     if (U_FAILURE(status)) {
+      // Realistically, never called
       return false;
     }
     return matcher->find(status);

+ 5 - 2
tests/regex_test.cxx

@@ -43,8 +43,11 @@ TYPED_TEST(RegexEngineTest, SearchCanSetBoundaries) {
 }
 
 TYPED_TEST(RegexEngineTest, SearchIsNoexceptOnBadRegex) {
-  EXPECT_NO_THROW(TypeParam().search("(ABC){1,2", "ABC"));
-  EXPECT_FALSE(TypeParam().search("(ABC){1,2", "ABC"));
+  TypeParam engine;
+  EXPECT_NO_THROW(engine.search("(ABC){1,2", "ABC"));
+  EXPECT_FALSE(engine.search("(ABC){1,2", "ABC"));
+  // Repeated calls *can* use a cached result, even if compilation failed
+  EXPECT_FALSE(engine.search("(ABC){1,2", "ABC"));
 }
 
 #if !defined(JVALIDATE_MONOTEST)