]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
ng_fuzzy: apply a resource limit to vertex count
authorJustin Viiret <justin.viiret@intel.com>
Wed, 8 Mar 2017 00:36:24 +0000 (11:36 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 26 Apr 2017 05:16:27 +0000 (15:16 +1000)
src/grey.cpp
src/grey.h
src/nfagraph/ng_fuzzy.cpp

index 05473abb568ba8af8bc790ae42765be755f566b0..ea92fdb596abf4b38b2ccf2d9f9b1e43e8709405 100644 (file)
@@ -156,7 +156,8 @@ Grey::Grey(void) :
                    limitEngineSize(1073741824), // 1 GB
                    limitDFASize(1073741824), // 1 GB
                    limitNFASize(1048576), // 1 MB
-                   limitLBRSize(1048576) // 1 MB
+                   limitLBRSize(1048576), // 1 MB
+                   limitApproxMatchingVertices(5000)
 {
     assert(maxAnchoredRegion < 64); /* a[lm]_log_sum have limited capacity */
 }
@@ -317,6 +318,7 @@ void applyGreyOverrides(Grey *g, const string &s) {
         G_UPDATE(limitDFASize);
         G_UPDATE(limitNFASize);
         G_UPDATE(limitLBRSize);
+        G_UPDATE(limitApproxMatchingVertices);
 
 #undef G_UPDATE
         if (key == "simple_som") {
index c2d5ac9274a3e37c8aacd2df3c5ebd54602e105b..5fde7b4b7c4da9bb37fafed42e94e799157c3ff1 100644 (file)
@@ -201,6 +201,9 @@ struct Grey {
     u32 limitDFASize;    //!< max size of a DFA (in bytes)
     u32 limitNFASize;    //!< max size of an NFA (in bytes)
     u32 limitLBRSize;    //!< max size of an LBR engine (in bytes)
+
+    // Approximate matching limits.
+    u32 limitApproxMatchingVertices; //!< max number of vertices per graph
 };
 
 #ifndef RELEASE_BUILD
index fecb7065e01833f4b6131033d2529155e03769bc..fc4681261bfdafb5d63d40b3bba3c26146e3958f 100644 (file)
@@ -665,13 +665,23 @@ void validate_fuzzy_compile(const NGHolder &g, u32 edit_distance, bool utf8,
     }
 }
 
-void make_fuzzy(NGHolder &g, u32 edit_distance, UNUSED const Grey &grey) {
+void make_fuzzy(NGHolder &g, u32 edit_distance, const Grey &grey) {
     if (edit_distance == 0) {
         return;
     }
+
     assert(grey.allowApproximateMatching);
     assert(grey.maxEditDistance >= edit_distance);
+
     ShadowGraph sg(g, edit_distance);
     sg.fuzz_graph();
+
+    // For safety, enforce limit on actual vertex count.
+    if (num_vertices(g) > grey.limitApproxMatchingVertices) {
+        DEBUG_PRINTF("built %zu vertices > limit of %u\n", num_vertices(g),
+                     grey.limitApproxMatchingVertices);
+        throw ResourceLimitError();
+    }
 }
-}
+
+} // namespace ue2