From 003a8e8e5f33f4bbbc0e8c349b25faec74bb0440 Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Thu, 24 Aug 2017 18:18:55 -0300 Subject: [PATCH] Uses shared_ptr on variable names --- headers/modsecurity/collection/variable.h | 41 +++++++++++------------ headers/modsecurity/rules_exceptions.h | 2 +- src/anchored_set_variable.cc | 4 --- src/anchored_variable.cc | 4 ++- src/rule.cc | 24 ++++++------- src/rules_exceptions.cc | 4 +-- src/variables/remote_user.cc | 2 +- src/variables/variable.h | 1 - src/variables/xml.cc | 1 - src/variables/xml.h | 1 - 10 files changed, 38 insertions(+), 46 deletions(-) diff --git a/headers/modsecurity/collection/variable.h b/headers/modsecurity/collection/variable.h index d976c8d6..5f499fdc 100644 --- a/headers/modsecurity/collection/variable.h +++ b/headers/modsecurity/collection/variable.h @@ -37,33 +37,32 @@ namespace collection { class Variable { public: explicit Variable(const std::string *key) : - m_key(key), - m_value(), - m_dynamic_value(false), - m_dynamic_key(false), - m_dynamic(true) { } + m_dynamic(true) { + m_key.reset(new std::string(*key)); + } Variable(const std::string *key, const std::string *value) : - m_key(key), - m_value(value), - m_dynamic_value(false), - m_dynamic_key(false), - m_dynamic(true) { } + m_dynamic(true) { + m_key.reset(new std::string(*key)); + m_value.reset(new std::string(*value)); + } + explicit Variable(const std::shared_ptr key) : + m_dynamic(true) { + m_key = key; + } + Variable(std::shared_ptr key, std::shared_ptr value) : + m_dynamic(true) { + m_key = key; + m_value = value; + + } ~Variable() { - if (m_dynamic_value) { - delete m_value; - } - if (m_dynamic_key) { - delete m_key; - } } - const std::string *m_key; - const std::string *m_value; - bool m_dynamic_value; - bool m_dynamic_key; - bool m_dynamic; + std::shared_ptr m_key; + std::shared_ptr m_value; std::list> m_orign; + bool m_dynamic; }; } // namespace collection diff --git a/headers/modsecurity/rules_exceptions.h b/headers/modsecurity/rules_exceptions.h index cbbffb50..1c84c5cf 100644 --- a/headers/modsecurity/rules_exceptions.h +++ b/headers/modsecurity/rules_exceptions.h @@ -60,7 +60,7 @@ class RulesExceptions { std::unique_ptr > > var, std::string *error); - std::unordered_multimap> m_variable_update_target_by_tag; + std::unordered_multimap, std::unique_ptr> m_variable_update_target_by_tag; std::unordered_multimap> m_variable_update_target_by_id; std::list m_remove_rule_by_msg; diff --git a/src/anchored_set_variable.cc b/src/anchored_set_variable.cc index 0ea36874..daf73e5d 100644 --- a/src/anchored_set_variable.cc +++ b/src/anchored_set_variable.cc @@ -43,8 +43,6 @@ AnchoredSetVariable::~AnchoredSetVariable() { void AnchoredSetVariable::unset() { for (const auto& x : *this) { collection::Variable *var = x.second; - delete var->m_key; - var->m_key = NULL; delete var; } clear(); @@ -61,7 +59,6 @@ void AnchoredSetVariable::set(const std::string &key, origin->m_offset = offset; origin->m_length = len; - var->m_dynamic_value = true; var->m_dynamic = false; var->m_orign.push_back(std::move(origin)); emplace(key, var); @@ -78,7 +75,6 @@ void AnchoredSetVariable::set(const std::string &key, origin->m_offset = offset; origin->m_length = value.size(); - var->m_dynamic_value = true; var->m_dynamic = false; var->m_orign.push_back(std::move(origin)); emplace(key, var); diff --git a/src/anchored_variable.cc b/src/anchored_variable.cc index 9525610c..d80b415c 100644 --- a/src/anchored_variable.cc +++ b/src/anchored_variable.cc @@ -38,15 +38,17 @@ AnchoredVariable::AnchoredVariable(Transaction *t, m_name.append(name); m_var = new collection::Variable(&m_name); m_var->m_dynamic = false; - m_var->m_value = &m_value; + m_var->m_value.reset(&m_value); } AnchoredVariable::~AnchoredVariable() { + /* if (m_var) { delete (m_var); m_var = NULL; } + */ } diff --git a/src/rule.cc b/src/rule.cc index 2842624d..7fcb794e 100644 --- a/src/rule.cc +++ b/src/rule.cc @@ -431,16 +431,16 @@ std::list, std::vector> Rule::getFinalVars( Transaction *trans) { - std::list exclusions; - std::list exclusions_update_by_tag_remove; - std::list exclusions_update_by_id_remove; + std::list> exclusions; + std::list> exclusions_update_by_tag_remove; + std::list> exclusions_update_by_id_remove; std::vector variables; std::vector> finalVars; std::copy (m_variables->begin(), m_variables->end(), std::back_inserter(variables)); for (auto &a : trans->m_rules->m_exceptions.m_variable_update_target_by_tag) { - if (containsTag(a.first, trans) == false) { + if (containsTag(*a.first.get(), trans) == false) { continue; } if (a.second->m_isExclusion) { @@ -449,7 +449,7 @@ std::vector> Rule::getFinalVars( for (auto &y : z) { exclusions_update_by_tag_remove.push_back(y->m_key); } - exclusions_update_by_tag_remove.push_back(&a.second->m_name); + exclusions_update_by_tag_remove.push_back(std::make_shared(a.second->m_name)); } else { Variable *b = a.second.get(); @@ -467,7 +467,7 @@ std::vector> Rule::getFinalVars( for (auto &y : z) { exclusions_update_by_id_remove.push_back(y->m_key); } - exclusions_update_by_id_remove.push_back(&a.second->m_name); + exclusions_update_by_id_remove.push_back(std::make_shared(a.second->m_name)); } else { Variable *b = a.second.get(); variables.push_back(b); @@ -482,7 +482,7 @@ std::vector> Rule::getFinalVars( for (auto &y : z) { exclusions.push_back(y->m_key); } - exclusions.push_back(&variable->m_name); +// exclusions.push_back(std::make_shared(&variable->m_name)); } } @@ -497,9 +497,9 @@ std::vector> Rule::getFinalVars( variable->evaluateInternal(trans, this, &e); for (const collection::Variable *v : e) { - const std::string *key = v->m_key; + const std::shared_ptr key = v->m_key; if (std::find_if(exclusions.begin(), exclusions.end(), - [key](const std::string *m) -> bool { return *key == *m; }) + [key](std::shared_ptr m) -> bool { return *key == *m.get(); }) != exclusions.end()) { #ifndef NO_LOGS trans->debug(9, "Variable: " + *key + @@ -513,7 +513,7 @@ std::vector> Rule::getFinalVars( } if (std::find_if(exclusions_update_by_tag_remove.begin(), exclusions_update_by_tag_remove.end(), - [key](const std::string *m) -> bool { return *key == *m; }) + [key](std::shared_ptr m) -> bool { return *key == *m.get(); }) != exclusions_update_by_tag_remove.end()) { #ifndef NO_LOGS trans->debug(9, "Variable: " + *key + @@ -529,7 +529,7 @@ std::vector> Rule::getFinalVars( if (std::find_if(exclusions_update_by_id_remove.begin(), exclusions_update_by_id_remove.end(), - [key](const std::string *m) -> bool { return *key == *m; }) + [key](std::shared_ptr m) -> bool { return *key == *m.get(); }) != exclusions_update_by_id_remove.end()) { #ifndef NO_LOGS trans->debug(9, "Variable: " + *key + @@ -621,8 +621,6 @@ std::vector> Rule::getFinalVars( std::unique_ptr var(new collection::Variable( new std::string(*v->m_key), new std::string(*v->m_value))); - var->m_dynamic_value = true; - var->m_dynamic_key = true; for (auto &i : v->m_orign) { std::unique_ptr origin(new VariableOrigin()); origin->m_offset = i->m_offset; diff --git a/src/rules_exceptions.cc b/src/rules_exceptions.cc index 55269de7..5a08bdae 100644 --- a/src/rules_exceptions.cc +++ b/src/rules_exceptions.cc @@ -44,7 +44,7 @@ bool RulesExceptions::loadUpdateTargetByTag(const std::string &tag, std::string *error) { for (auto &i : *var) { - m_variable_update_target_by_tag.emplace(std::pair>(tag, std::move(i))); + m_variable_update_target_by_tag.emplace(std::pair, std::unique_ptr>(std::make_shared(tag), std::move(i))); } return true; @@ -164,7 +164,7 @@ bool RulesExceptions::merge(RulesExceptions& from) { } for (auto &p : from.m_variable_update_target_by_tag) { - m_variable_update_target_by_tag.emplace(std::pair>(p.first, std::move(p.second))); + m_variable_update_target_by_tag.emplace(std::pair, std::unique_ptr>(p.first, std::move(p.second))); } for (auto &p : from.m_variable_update_target_by_id) { diff --git a/src/variables/remote_user.cc b/src/variables/remote_user.cc index 9dd83cf9..db65be1d 100644 --- a/src/variables/remote_user.cc +++ b/src/variables/remote_user.cc @@ -64,7 +64,7 @@ void RemoteUser::evaluate(Transaction *transaction, transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos)); var = new collection::Variable(l->at(0)->m_key, - &transaction->m_variableRemoteUser); + std::make_shared(transaction->m_variableRemoteUser)); for (auto &i : l->at(0)->m_orign) { std::unique_ptr origin(new VariableOrigin()); diff --git a/src/variables/variable.h b/src/variables/variable.h index d244b3ff..0c5c135d 100644 --- a/src/variables/variable.h +++ b/src/variables/variable.h @@ -143,7 +143,6 @@ class VariableModificatorCount : public Variable { res = new std::string(std::to_string(count)); val = new collection::Variable(&m_name, res); - val->m_dynamic_value = true; val->m_dynamic = true; l->push_back(val); diff --git a/src/variables/xml.cc b/src/variables/xml.cc index ec086d52..fbf269e1 100644 --- a/src/variables/xml.cc +++ b/src/variables/xml.cc @@ -127,7 +127,6 @@ void XML::evaluate(Transaction *t, if (content != NULL) { collection::Variable *var = new collection::Variable(&m_name, new std::string(content)); - var->m_dynamic_value = true; l->push_back(var); xmlFree(content); } diff --git a/src/variables/xml.h b/src/variables/xml.h index 94f6fd5a..b2c67faf 100644 --- a/src/variables/xml.h +++ b/src/variables/xml.h @@ -41,7 +41,6 @@ class XML_NoDictElement : public Variable { m_plain("[XML document tree]"), m_var(&m_name, &m_plain) { m_var.m_dynamic = false; - m_var.m_dynamic_value = false; } void evaluate(Transaction *transaction,