Skip to content

Commit

Permalink
leverage std::make_unique & std::make_shared
Browse files Browse the repository at this point in the history
- Simpler code & more efficient because control block can be allocated
  with object.
  • Loading branch information
eduar-hte committed Aug 28, 2024
1 parent 366e881 commit c8403e6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions headers/modsecurity/actions/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ class Action {
}

if (pos == std::string::npos) {
m_name = std::shared_ptr<std::string>(new std::string(data));
m_name = std::make_shared<std::string>(data);
return;
}

m_name = std::shared_ptr<std::string>(new std::string(data, 0, pos));
m_name = std::make_shared<std::string>(data, 0, pos);
m_parser_payload = std::string(data, pos + 1, data.length());

if (m_parser_payload.at(0) == '\'' && m_parser_payload.size() > 2) {
Expand Down
12 changes: 6 additions & 6 deletions src/actions/expire_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> runTimeString(new RunTimeString());
auto runTimeString = std::make_unique<RunTimeString>();
runTimeString->appendText(fully_qualified_var);

if (collection == "ip") {
std::unique_ptr<modsecurity::variables::Ip_DynamicElement> ip_dynamicElement(new modsecurity::variables::Ip_DynamicElement(std::move(runTimeString)));
auto ip_dynamicElement = std::make_unique<modsecurity::variables::Ip_DynamicElement>(std::move(runTimeString));
ip_dynamicElement->setExpiry(t, variable_name, expirySeconds);
} else if (collection == "global") {
std::unique_ptr<modsecurity::variables::Global_DynamicElement> global_dynamicElement(new modsecurity::variables::Global_DynamicElement(std::move(runTimeString)));
auto global_dynamicElement = std::make_unique<modsecurity::variables::Global_DynamicElement>(std::move(runTimeString));
global_dynamicElement->setExpiry(t, variable_name, expirySeconds);
} else if (collection == "resource") {
std::unique_ptr<modsecurity::variables::Resource_DynamicElement> resource_dynamicElement(new modsecurity::variables::Resource_DynamicElement(std::move(runTimeString)));
auto resource_dynamicElement = std::make_unique<modsecurity::variables::Resource_DynamicElement>(std::move(runTimeString));
resource_dynamicElement->setExpiry(t, variable_name, expirySeconds);
} else if (collection == "session") {
std::unique_ptr<modsecurity::variables::Session_DynamicElement> session_dynamicElement(new modsecurity::variables::Session_DynamicElement(std::move(runTimeString)));
auto session_dynamicElement = std::make_unique<modsecurity::variables::Session_DynamicElement>(std::move(runTimeString));
session_dynamicElement->setExpiry(t, variable_name, expirySeconds);
} else if (collection == "user") {
std::unique_ptr<modsecurity::variables::User_DynamicElement> user_dynamicElement(new modsecurity::variables::User_DynamicElement(std::move(runTimeString)));
auto user_dynamicElement = std::make_unique<modsecurity::variables::User_DynamicElement>(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'");
Expand Down
2 changes: 1 addition & 1 deletion src/collection/backend/lmdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ std::unique_ptr<std::string> LMDB::resolveFirst(const std::string& var) {

collectionData.setFromSerialized(reinterpret_cast<char *>(mdb_value_ret.mv_data), mdb_value_ret.mv_size);
if ((!collectionData.isExpired()) && (collectionData.hasValue())) {
ret = std::unique_ptr<std::string>(new std::string(collectionData.getValue()));
ret = std::make_unique<std::string>(collectionData.getValue());
}

end_get:
Expand Down
2 changes: 1 addition & 1 deletion src/operators/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<RunTimeString> param(new RunTimeString());
auto param = std::make_unique<RunTimeString>();
param->appendText(param_str);

IF_MATCH(beginswith) { return new BeginsWith(std::move(param)); }
Expand Down
4 changes: 2 additions & 2 deletions src/run_time_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ namespace modsecurity {


void RunTimeString::appendText(const std::string &text) {
std::unique_ptr<RunTimeElementHolder> r(new RunTimeElementHolder);
auto r = std::make_unique<RunTimeElementHolder>();
r->m_string = text;
m_elements.push_back(std::move(r));
}


void RunTimeString::appendVar(
std::unique_ptr<modsecurity::variables::Variable> var) {
std::unique_ptr<RunTimeElementHolder> r(new RunTimeElementHolder);
auto r = std::make_unique<RunTimeElementHolder>();
r->m_var = std::move(var);
m_elements.push_back(std::move(r));
m_containsMacro = true;
Expand Down

0 comments on commit c8403e6

Please sign in to comment.