Moving the rules deletion to the RuleProperties class

The deletion was happning on the Rule class due to historical reasons.
The consequence of that was a parser memory leak.
This commit is contained in:
Felipe Zimmerle 2017-01-06 01:09:41 -03:00 committed by Felipe Zimmerle
parent 068a3eb517
commit a8e5cce744
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
5 changed files with 36 additions and 29 deletions

View File

@ -41,7 +41,7 @@ class Action {
: action_kind(2), : action_kind(2),
m_isNone(false), m_isNone(false),
m_name(""), m_name(""),
m_referenceCount(0), m_referenceCount(1),
m_parser_payload(""), m_parser_payload(""),
temporaryAction(false) { temporaryAction(false) {
set_name_and_payload(_action); set_name_and_payload(_action);
@ -50,7 +50,7 @@ class Action {
: action_kind(kind), : action_kind(kind),
m_isNone(false), m_isNone(false),
m_name(""), m_name(""),
m_referenceCount(0), m_referenceCount(1),
m_parser_payload(""), m_parser_payload(""),
temporaryAction(false) { temporaryAction(false) {
set_name_and_payload(_action); set_name_and_payload(_action);
@ -91,11 +91,13 @@ class Action {
} }
} }
void refCountDecreaseAndCheck() { int refCountDecreaseAndCheck() {
this->m_referenceCount--; this->m_referenceCount--;
if (this->m_referenceCount == 0) { if (this->m_referenceCount == 0) {
delete this; delete this;
return 1;
} }
return 0;
} }
void refCountIncrease() { void refCountIncrease() {

View File

@ -84,15 +84,17 @@ class Rule {
Rule *chainedRule; Rule *chainedRule;
bool chained; bool chained;
void refCountDecreaseAndCheck() { int refCountDecreaseAndCheck() {
this->m_referenceCount--; m_referenceCount--;
if (this->m_referenceCount == 0) { if (m_referenceCount == 0) {
delete this; delete this;
return 1;
} }
return 0;
} }
void refCountIncrease() { void refCountIncrease() {
this->m_referenceCount++; m_referenceCount++;
} }
std::string m_rev; std::string m_rev;

View File

@ -102,6 +102,29 @@ class RulesProperties {
~RulesProperties() { ~RulesProperties() {
int i = 0;
/** Cleanup the rules */
for (i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
std::vector<Rule *> rules = m_rules[i];
while (rules.empty() == false) {
Rule *rule = rules.back();
rules.pop_back();
if (rule->refCountDecreaseAndCheck()) {
rule = NULL;
}
}
}
for (i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
std::vector<actions::Action *> *tmp = &m_defaultActions[i];
while (tmp->empty() == false) {
actions::Action *a = tmp->back();
tmp->pop_back();
if (a->refCountDecreaseAndCheck()) {
a = NULL;
}
}
}
delete m_debugLog; delete m_debugLog;
delete m_auditLog; delete m_auditLog;
} }

View File

@ -94,7 +94,7 @@ Rule::Rule(std::string marker)
m_secmarker(true), m_secmarker(true),
m_marker(marker), m_marker(marker),
m_maturity(0), m_maturity(0),
m_referenceCount(0), m_referenceCount(1),
m_fileName(""), m_fileName(""),
m_lineNumber(0) { } m_lineNumber(0) { }
@ -113,7 +113,7 @@ Rule::Rule(Operator *_op,
m_secmarker(false), m_secmarker(false),
m_marker(""), m_marker(""),
m_maturity(0), m_maturity(0),
m_referenceCount(0), m_referenceCount(1),
m_fileName(fileName), m_fileName(fileName),
m_lineNumber(lineNumber) { m_lineNumber(lineNumber) {
if (actions != NULL) { if (actions != NULL) {

View File

@ -78,28 +78,8 @@ void Rules::decrementReferenceCount(void) {
Rules::~Rules() { Rules::~Rules() {
int i = 0;
free(unicode_map_table); free(unicode_map_table);
unicode_map_table = NULL; unicode_map_table = NULL;
/** Cleanup the rules */
for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
std::vector<Rule *> rules = m_rules[i];
while (rules.empty() == false) {
Rule *rule = rules.back();
rule->refCountDecreaseAndCheck();
rules.pop_back();
}
}
for (i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
std::vector<actions::Action *> *tmp = &m_defaultActions[i];
while (tmp->empty() == false) {
actions::Action *a = tmp->back();
a->refCountDecreaseAndCheck();
tmp->pop_back();
}
}
} }