Skip to content

Commit 003a8e8

Browse files
author
Felipe Zimmerle
committed
Uses shared_ptr on variable names
1 parent 9d062f5 commit 003a8e8

File tree

10 files changed

+37
-45
lines changed

10 files changed

+37
-45
lines changed

headers/modsecurity/collection/variable.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,32 @@ namespace collection {
3737
class Variable {
3838
public:
3939
explicit Variable(const std::string *key) :
40-
m_key(key),
41-
m_value(),
42-
m_dynamic_value(false),
43-
m_dynamic_key(false),
44-
m_dynamic(true) { }
40+
m_dynamic(true) {
41+
m_key.reset(new std::string(*key));
42+
}
4543
Variable(const std::string *key, const std::string *value) :
46-
m_key(key),
47-
m_value(value),
48-
m_dynamic_value(false),
49-
m_dynamic_key(false),
50-
m_dynamic(true) { }
44+
m_dynamic(true) {
45+
m_key.reset(new std::string(*key));
46+
m_value.reset(new std::string(*value));
5147

52-
~Variable() {
53-
if (m_dynamic_value) {
54-
delete m_value;
5548
}
56-
if (m_dynamic_key) {
57-
delete m_key;
49+
explicit Variable(const std::shared_ptr<std::string> key) :
50+
m_dynamic(true) {
51+
m_key = key;
52+
}
53+
Variable(std::shared_ptr<std::string> key, std::shared_ptr<std::string> value) :
54+
m_dynamic(true) {
55+
m_key = key;
56+
m_value = value;
57+
5858
}
59+
~Variable() {
5960
}
6061

61-
const std::string *m_key;
62-
const std::string *m_value;
63-
bool m_dynamic_value;
64-
bool m_dynamic_key;
65-
bool m_dynamic;
62+
std::shared_ptr<std::string> m_key;
63+
std::shared_ptr<std::string> m_value;
6664
std::list<std::unique_ptr<VariableOrigin>> m_orign;
65+
bool m_dynamic;
6766
};
6867

6968
} // namespace collection

headers/modsecurity/rules_exceptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class RulesExceptions {
6060
std::unique_ptr<std::vector<std::unique_ptr<Variables::Variable> > > var,
6161
std::string *error);
6262

63-
std::unordered_multimap<std::string, std::unique_ptr<Variables::Variable>> m_variable_update_target_by_tag;
63+
std::unordered_multimap<std::shared_ptr<std::string>, std::unique_ptr<Variables::Variable>> m_variable_update_target_by_tag;
6464
std::unordered_multimap<double, std::unique_ptr<Variables::Variable>> m_variable_update_target_by_id;
6565
std::list<std::string> m_remove_rule_by_msg;
6666

src/anchored_set_variable.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ AnchoredSetVariable::~AnchoredSetVariable() {
4343
void AnchoredSetVariable::unset() {
4444
for (const auto& x : *this) {
4545
collection::Variable *var = x.second;
46-
delete var->m_key;
47-
var->m_key = NULL;
4846
delete var;
4947
}
5048
clear();
@@ -61,7 +59,6 @@ void AnchoredSetVariable::set(const std::string &key,
6159
origin->m_offset = offset;
6260
origin->m_length = len;
6361

64-
var->m_dynamic_value = true;
6562
var->m_dynamic = false;
6663
var->m_orign.push_back(std::move(origin));
6764
emplace(key, var);
@@ -78,7 +75,6 @@ void AnchoredSetVariable::set(const std::string &key,
7875
origin->m_offset = offset;
7976
origin->m_length = value.size();
8077

81-
var->m_dynamic_value = true;
8278
var->m_dynamic = false;
8379
var->m_orign.push_back(std::move(origin));
8480
emplace(key, var);

src/anchored_variable.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ AnchoredVariable::AnchoredVariable(Transaction *t,
3838
m_name.append(name);
3939
m_var = new collection::Variable(&m_name);
4040
m_var->m_dynamic = false;
41-
m_var->m_value = &m_value;
41+
m_var->m_value.reset(&m_value);
4242
}
4343

4444

4545
AnchoredVariable::~AnchoredVariable() {
46+
/*
4647
if (m_var) {
4748
delete (m_var);
4849
m_var = NULL;
4950
}
51+
*/
5052
}
5153

5254

src/rule.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,16 @@ std::list<std::pair<std::shared_ptr<std::string>,
431431

432432
std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
433433
Transaction *trans) {
434-
std::list<const std::string*> exclusions;
435-
std::list<const std::string*> exclusions_update_by_tag_remove;
436-
std::list<const std::string*> exclusions_update_by_id_remove;
434+
std::list<std::shared_ptr<std::string>> exclusions;
435+
std::list<std::shared_ptr<std::string>> exclusions_update_by_tag_remove;
436+
std::list<std::shared_ptr<std::string>> exclusions_update_by_id_remove;
437437
std::vector<Variables::Variable *> variables;
438438
std::vector<std::unique_ptr<collection::Variable>> finalVars;
439439

440440
std::copy (m_variables->begin(), m_variables->end(), std::back_inserter(variables));
441441

442442
for (auto &a : trans->m_rules->m_exceptions.m_variable_update_target_by_tag) {
443-
if (containsTag(a.first, trans) == false) {
443+
if (containsTag(*a.first.get(), trans) == false) {
444444
continue;
445445
}
446446
if (a.second->m_isExclusion) {
@@ -449,7 +449,7 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
449449
for (auto &y : z) {
450450
exclusions_update_by_tag_remove.push_back(y->m_key);
451451
}
452-
exclusions_update_by_tag_remove.push_back(&a.second->m_name);
452+
exclusions_update_by_tag_remove.push_back(std::make_shared<std::string>(a.second->m_name));
453453

454454
} else {
455455
Variable *b = a.second.get();
@@ -467,7 +467,7 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
467467
for (auto &y : z) {
468468
exclusions_update_by_id_remove.push_back(y->m_key);
469469
}
470-
exclusions_update_by_id_remove.push_back(&a.second->m_name);
470+
exclusions_update_by_id_remove.push_back(std::make_shared<std::string>(a.second->m_name));
471471
} else {
472472
Variable *b = a.second.get();
473473
variables.push_back(b);
@@ -482,7 +482,7 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
482482
for (auto &y : z) {
483483
exclusions.push_back(y->m_key);
484484
}
485-
exclusions.push_back(&variable->m_name);
485+
// exclusions.push_back(std::make_shared<std::string>(&variable->m_name));
486486
}
487487
}
488488

@@ -497,9 +497,9 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
497497

498498
variable->evaluateInternal(trans, this, &e);
499499
for (const collection::Variable *v : e) {
500-
const std::string *key = v->m_key;
500+
const std::shared_ptr<std::string> key = v->m_key;
501501
if (std::find_if(exclusions.begin(), exclusions.end(),
502-
[key](const std::string *m) -> bool { return *key == *m; })
502+
[key](std::shared_ptr<std::string> m) -> bool { return *key == *m.get(); })
503503
!= exclusions.end()) {
504504
#ifndef NO_LOGS
505505
trans->debug(9, "Variable: " + *key +
@@ -513,7 +513,7 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
513513
}
514514
if (std::find_if(exclusions_update_by_tag_remove.begin(),
515515
exclusions_update_by_tag_remove.end(),
516-
[key](const std::string *m) -> bool { return *key == *m; })
516+
[key](std::shared_ptr<std::string> m) -> bool { return *key == *m.get(); })
517517
!= exclusions_update_by_tag_remove.end()) {
518518
#ifndef NO_LOGS
519519
trans->debug(9, "Variable: " + *key +
@@ -529,7 +529,7 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
529529

530530
if (std::find_if(exclusions_update_by_id_remove.begin(),
531531
exclusions_update_by_id_remove.end(),
532-
[key](const std::string *m) -> bool { return *key == *m; })
532+
[key](std::shared_ptr<std::string> m) -> bool { return *key == *m.get(); })
533533
!= exclusions_update_by_id_remove.end()) {
534534
#ifndef NO_LOGS
535535
trans->debug(9, "Variable: " + *key +
@@ -621,8 +621,6 @@ std::vector<std::unique_ptr<collection::Variable>> Rule::getFinalVars(
621621
std::unique_ptr<collection::Variable> var(new collection::Variable(
622622
new std::string(*v->m_key),
623623
new std::string(*v->m_value)));
624-
var->m_dynamic_value = true;
625-
var->m_dynamic_key = true;
626624
for (auto &i : v->m_orign) {
627625
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
628626
origin->m_offset = i->m_offset;

src/rules_exceptions.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool RulesExceptions::loadUpdateTargetByTag(const std::string &tag,
4444
std::string *error) {
4545

4646
for (auto &i : *var) {
47-
m_variable_update_target_by_tag.emplace(std::pair<std::string, std::unique_ptr<Variables::Variable>>(tag, std::move(i)));
47+
m_variable_update_target_by_tag.emplace(std::pair<std::shared_ptr<std::string>, std::unique_ptr<Variables::Variable>>(std::make_shared<std::string>(tag), std::move(i)));
4848
}
4949

5050
return true;
@@ -164,7 +164,7 @@ bool RulesExceptions::merge(RulesExceptions& from) {
164164
}
165165

166166
for (auto &p : from.m_variable_update_target_by_tag) {
167-
m_variable_update_target_by_tag.emplace(std::pair<std::string, std::unique_ptr<Variables::Variable>>(p.first, std::move(p.second)));
167+
m_variable_update_target_by_tag.emplace(std::pair<std::shared_ptr<std::string>, std::unique_ptr<Variables::Variable>>(p.first, std::move(p.second)));
168168
}
169169

170170
for (auto &p : from.m_variable_update_target_by_id) {

src/variables/remote_user.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void RemoteUser::evaluate(Transaction *transaction,
6464
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
6565

6666
var = new collection::Variable(l->at(0)->m_key,
67-
&transaction->m_variableRemoteUser);
67+
std::make_shared<std::string>(transaction->m_variableRemoteUser));
6868

6969
for (auto &i : l->at(0)->m_orign) {
7070
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());

src/variables/variable.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ class VariableModificatorCount : public Variable {
143143
res = new std::string(std::to_string(count));
144144

145145
val = new collection::Variable(&m_name, res);
146-
val->m_dynamic_value = true;
147146
val->m_dynamic = true;
148147

149148
l->push_back(val);

src/variables/xml.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ void XML::evaluate(Transaction *t,
127127
if (content != NULL) {
128128
collection::Variable *var = new collection::Variable(&m_name,
129129
new std::string(content));
130-
var->m_dynamic_value = true;
131130
l->push_back(var);
132131
xmlFree(content);
133132
}

src/variables/xml.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class XML_NoDictElement : public Variable {
4141
m_plain("[XML document tree]"),
4242
m_var(&m_name, &m_plain) {
4343
m_var.m_dynamic = false;
44-
m_var.m_dynamic_value = false;
4544
}
4645

4746
void evaluate(Transaction *transaction,

0 commit comments

Comments
 (0)