Skip to content

Commit 08f9a7f

Browse files
committed
Optimize duplicate rule ID check
Replace an exponential search function with a stl set search.
1 parent 077b182 commit 08f9a7f

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

headers/modsecurity/rules_properties.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class RulesProperties {
273273
std::ostringstream *err) {
274274
int amount_of_rules = 0;
275275

276-
amount_of_rules = appendRules(from->m_rules, to->m_rules, err);
276+
amount_of_rules = appendRules(from, to, err);
277277
if (amount_of_rules < 0) {
278278
return amount_of_rules;
279279
}
@@ -426,20 +426,22 @@ class RulesProperties {
426426

427427

428428
static int appendRules(
429-
std::vector<modsecurity::Rule *> *from,
430-
std::vector<modsecurity::Rule *> *to,
429+
RulesProperties *from,
430+
RulesProperties *to,
431431
std::ostringstream *err) {
432+
std::vector<modsecurity::Rule *> *from_rules = from->m_rules;
433+
std::vector<modsecurity::Rule *> *to_rules = to->m_rules;
432434
int amount_of_rules = 0;
433435
for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
434-
std::vector<modsecurity::Rule *> *rules_to = to+i;
435-
std::vector<modsecurity::Rule *> *rules_from = from+i;
436+
std::vector<modsecurity::Rule *> *rules_to = to_rules+i;
437+
std::vector<modsecurity::Rule *> *rules_from = from_rules+i;
436438
for (size_t j = 0; j < rules_from->size(); j++) {
437439
Rule *rule = rules_from->at(j);
438-
for (size_t z = 0; z < rules_to->size(); z++) {
439-
Rule *rule_ckc = rules_to->at(z);
440-
if (rule_ckc->m_ruleId == rule->m_ruleId &&
441-
rule_ckc->m_secMarker == false &&
442-
rule->m_secMarker == false) {
440+
bool do_check = rule->m_secMarker == false;
441+
442+
if (do_check) {
443+
std::set<int64_t>::iterator it = to->m_ruleIds.find(rule->m_ruleId);
444+
if (it != to->m_ruleIds.end()) {
443445
if (err != NULL) {
444446
*err << "Rule id: " \
445447
<< std::to_string(rule->m_ruleId) \
@@ -448,9 +450,13 @@ class RulesProperties {
448450
return -1;
449451
}
450452
}
453+
451454
amount_of_rules++;
452455
rules_to->push_back(rule);
453456
rule->refCountIncrease();
457+
458+
if (do_check)
459+
to->m_ruleIds.insert(rule->m_ruleId);
454460
}
455461
}
456462
return amount_of_rules;
@@ -492,6 +498,7 @@ class RulesProperties {
492498
ConfigString m_secWebAppId;
493499
std::vector<actions::Action *> m_defaultActions[8];
494500
std::vector<modsecurity::Rule *> m_rules[8];
501+
std::set<int64_t> m_ruleIds;
495502
ConfigUnicodeMap m_unicodeMapTable;
496503
};
497504

0 commit comments

Comments
 (0)