From c8403e62513e574a6d4a33705b172e82e4c9bc2d Mon Sep 17 00:00:00 2001 From: Eduardo Arias Date: Sun, 5 May 2024 13:06:50 -0300 Subject: [PATCH] leverage std::make_unique & std::make_shared - Simpler code & more efficient because control block can be allocated with object. --- headers/modsecurity/actions/action.h | 4 ++-- src/actions/expire_var.cc | 12 ++++++------ src/collection/backend/lmdb.cc | 2 +- src/operators/operator.cc | 2 +- src/run_time_string.cc | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/headers/modsecurity/actions/action.h b/headers/modsecurity/actions/action.h index f0c2165cf5..e62835b2e2 100644 --- a/headers/modsecurity/actions/action.h +++ b/headers/modsecurity/actions/action.h @@ -105,11 +105,11 @@ class Action { } if (pos == std::string::npos) { - m_name = std::shared_ptr(new std::string(data)); + m_name = std::make_shared(data); return; } - m_name = std::shared_ptr(new std::string(data, 0, pos)); + m_name = std::make_shared(data, 0, pos); m_parser_payload = std::string(data, pos + 1, data.length()); if (m_parser_payload.at(0) == '\'' && m_parser_payload.size() > 2) { diff --git a/src/actions/expire_var.cc b/src/actions/expire_var.cc index 5ed50db70d..988bc6c68c 100644 --- a/src/actions/expire_var.cc +++ b/src/actions/expire_var.cc @@ -65,23 +65,23 @@ bool ExpireVar::evaluate(RuleWithActions *rule, Transaction *t) { std::string collection = fully_qualified_var.substr(0, posDot); std::string variable_name = fully_qualified_var.substr(posDot+1); - std::unique_ptr runTimeString(new RunTimeString()); + auto runTimeString = std::make_unique(); runTimeString->appendText(fully_qualified_var); if (collection == "ip") { - std::unique_ptr ip_dynamicElement(new modsecurity::variables::Ip_DynamicElement(std::move(runTimeString))); + auto ip_dynamicElement = std::make_unique(std::move(runTimeString)); ip_dynamicElement->setExpiry(t, variable_name, expirySeconds); } else if (collection == "global") { - std::unique_ptr global_dynamicElement(new modsecurity::variables::Global_DynamicElement(std::move(runTimeString))); + auto global_dynamicElement = std::make_unique(std::move(runTimeString)); global_dynamicElement->setExpiry(t, variable_name, expirySeconds); } else if (collection == "resource") { - std::unique_ptr resource_dynamicElement(new modsecurity::variables::Resource_DynamicElement(std::move(runTimeString))); + auto resource_dynamicElement = std::make_unique(std::move(runTimeString)); resource_dynamicElement->setExpiry(t, variable_name, expirySeconds); } else if (collection == "session") { - std::unique_ptr session_dynamicElement(new modsecurity::variables::Session_DynamicElement(std::move(runTimeString))); + auto session_dynamicElement = std::make_unique(std::move(runTimeString)); session_dynamicElement->setExpiry(t, variable_name, expirySeconds); } else if (collection == "user") { - std::unique_ptr user_dynamicElement(new modsecurity::variables::User_DynamicElement(std::move(runTimeString))); + auto user_dynamicElement = std::make_unique(std::move(runTimeString)); user_dynamicElement->setExpiry(t, variable_name, expirySeconds); } else { ms_dbg_a(t, 5, "Invalid collection found in expirevar expression: collection must be `ip', `global', `resource', `user' or `session'"); diff --git a/src/collection/backend/lmdb.cc b/src/collection/backend/lmdb.cc index 4a77f5fb81..3fe68061e5 100644 --- a/src/collection/backend/lmdb.cc +++ b/src/collection/backend/lmdb.cc @@ -178,7 +178,7 @@ std::unique_ptr LMDB::resolveFirst(const std::string& var) { collectionData.setFromSerialized(reinterpret_cast(mdb_value_ret.mv_data), mdb_value_ret.mv_size); if ((!collectionData.isExpired()) && (collectionData.hasValue())) { - ret = std::unique_ptr(new std::string(collectionData.getValue())); + ret = std::make_unique(collectionData.getValue()); } end_get: diff --git a/src/operators/operator.cc b/src/operators/operator.cc index 238c885e6d..674e82d3dc 100644 --- a/src/operators/operator.cc +++ b/src/operators/operator.cc @@ -140,7 +140,7 @@ bool Operator::evaluate(Transaction *transaction, const std::string& a) { Operator *Operator::instantiate(const std::string& op, const std::string& param_str) { std::string op_ = utils::string::tolower(op); - std::unique_ptr param(new RunTimeString()); + auto param = std::make_unique(); param->appendText(param_str); IF_MATCH(beginswith) { return new BeginsWith(std::move(param)); } diff --git a/src/run_time_string.cc b/src/run_time_string.cc index f2dfa686f2..45f298bc3d 100644 --- a/src/run_time_string.cc +++ b/src/run_time_string.cc @@ -31,7 +31,7 @@ namespace modsecurity { void RunTimeString::appendText(const std::string &text) { - std::unique_ptr r(new RunTimeElementHolder); + auto r = std::make_unique(); r->m_string = text; m_elements.push_back(std::move(r)); } @@ -39,7 +39,7 @@ void RunTimeString::appendText(const std::string &text) { void RunTimeString::appendVar( std::unique_ptr var) { - std::unique_ptr r(new RunTimeElementHolder); + auto r = std::make_unique(); r->m_var = std::move(var); m_elements.push_back(std::move(r)); m_containsMacro = true;