diff --git a/src/grey.cpp b/src/grey.cpp index 05473abb..ea92fdb5 100644 --- a/src/grey.cpp +++ b/src/grey.cpp @@ -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") { diff --git a/src/grey.h b/src/grey.h index c2d5ac92..5fde7b4b 100644 --- a/src/grey.h +++ b/src/grey.h @@ -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 diff --git a/src/nfagraph/ng_fuzzy.cpp b/src/nfagraph/ng_fuzzy.cpp index fecb7065..fc468126 100644 --- a/src/nfagraph/ng_fuzzy.cpp +++ b/src/nfagraph/ng_fuzzy.cpp @@ -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