Skip to content

Commit 3dda900

Browse files
authored
Merge pull request #3164 from eduar-hte/variable-origin
Improve performance of VariableOrigin instances
2 parents 7c174e9 + eb62cac commit 3dda900

13 files changed

+84
-223
lines changed

headers/modsecurity/anchored_set_variable_translation_proxy.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ class AnchoredSetVariableTranslationProxy {
4747
VariableValue *newVariableValue = new VariableValue(name, &l->at(i)->getKey(), &l->at(i)->getKey());
4848
const VariableValue *oldVariableValue = l->at(i);
4949
l->at(i) = newVariableValue;
50+
newVariableValue->reserveOrigin(oldVariableValue->getOrigin().size());
5051
for (const auto &oldOrigin : oldVariableValue->getOrigin()) {
51-
std::unique_ptr<VariableOrigin> newOrigin(new VariableOrigin);
52-
newOrigin->m_length = oldVariableValue->getKey().size();
53-
newOrigin->m_offset = oldOrigin->m_offset - oldVariableValue->getKey().size() - 1;
54-
newVariableValue->addOrigin(std::move(newOrigin));
52+
newVariableValue->addOrigin(
53+
oldVariableValue->getKey().size(),
54+
oldOrigin.m_offset - oldVariableValue->getKey().size() - 1
55+
);
5556
}
5657
delete oldVariableValue;
5758
}

headers/modsecurity/anchored_variable.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,11 @@ class AnchoredVariable {
4747
AnchoredVariable(const AnchoredVariable &a) = delete;
4848
AnchoredVariable &operator= (const AnchoredVariable &a) = delete;
4949

50-
/*
51-
: m_transaction(a.m_transaction),
52-
m_offset(a.m_offset),
53-
m_name(a.m_name),
54-
m_value(a.m_value),
55-
m_var(a.m_var) { }
56-
*/
57-
58-
~AnchoredVariable();
50+
~AnchoredVariable() = default;
5951

6052
void unset();
6153
void set(const std::string &a, size_t offset);
6254
void set(const std::string &a, size_t offset, size_t offsetLen);
63-
void append(const std::string &a, size_t offset,
64-
bool spaceSeparator = false);
65-
void append(const std::string &a, size_t offset,
66-
bool spaceSeparator, int size);
6755

6856
void evaluate(std::vector<const VariableValue *> *l);
6957
std::string * evaluate();
@@ -75,7 +63,7 @@ class AnchoredVariable {
7563
std::string m_value;
7664

7765
private:
78-
VariableValue *m_var;
66+
VariableValue m_var;
7967
};
8068

8169
} // namespace modsecurity

headers/modsecurity/variable_origin.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#ifdef __cplusplus
1717
#include <string>
18+
#include <memory>
1819
#endif
1920

2021
#ifndef HEADERS_MODSECURITY_VARIABLE_ORIGIN_H_
@@ -36,14 +37,17 @@ class VariableOrigin {
3637
VariableOrigin()
3738
: m_length(0),
3839
m_offset(0) { }
40+
VariableOrigin(size_t length, size_t offset)
41+
: m_length(length),
42+
m_offset(offset) { }
3943

40-
std::string toText() {
41-
std::string offset = std::to_string(m_offset);
42-
std::string len = std::to_string(m_length);
44+
std::string toText() const {
45+
const auto offset = std::to_string(m_offset);
46+
const auto len = std::to_string(m_length);
4347
return "v" + offset + "," + len;
4448
}
4549

46-
int m_length;
50+
size_t m_length;
4751
size_t m_offset;
4852
};
4953

headers/modsecurity/variable_value.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <string>
1919
#include <iostream>
2020
#include <memory>
21-
#include <list>
21+
#include <vector>
2222
#include <utility>
2323
#endif
2424

@@ -37,7 +37,7 @@ namespace modsecurity {
3737
class Collection;
3838
class VariableValue {
3939
public:
40-
using Origins = std::list<std::unique_ptr<VariableOrigin>>;
40+
using Origins = std::vector<VariableOrigin>;
4141

4242
explicit VariableValue(const std::string *key,
4343
const std::string *value = nullptr)
@@ -62,11 +62,9 @@ class VariableValue {
6262
m_keyWithCollection(o->m_keyWithCollection),
6363
m_value(o->m_value)
6464
{
65+
reserveOrigin(o->m_orign.size());
6566
for (const auto &i : o->m_orign) {
66-
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
67-
origin->m_offset = i->m_offset;
68-
origin->m_length = i->m_length;
69-
m_orign.push_back(std::move(origin));
67+
addOrigin(i);
7068
}
7169
}
7270

@@ -98,15 +96,27 @@ class VariableValue {
9896
}
9997

10098

101-
void addOrigin(std::unique_ptr<VariableOrigin> origin) {
102-
m_orign.push_back(std::move(origin));
99+
void addOrigin(const VariableOrigin &origin) {
100+
m_orign.emplace_back(origin);
101+
}
102+
103+
104+
template<typename... Args>
105+
void addOrigin(Args&&... args) {
106+
m_orign.emplace_back(args...);
103107
}
104108

105109

106110
const Origins& getOrigin() const {
107111
return m_orign;
108112
}
109113

114+
115+
void reserveOrigin(Origins::size_type additionalSize) {
116+
m_orign.reserve(m_orign.size() + additionalSize);
117+
}
118+
119+
110120
private:
111121
Origins m_orign;
112122
std::string m_collection;

src/anchored_set_variable.cc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,16 @@ void AnchoredSetVariable::unset() {
5252

5353
void AnchoredSetVariable::set(const std::string &key,
5454
const std::string &value, size_t offset, size_t len) {
55-
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
5655
VariableValue *var = new VariableValue(&m_name, &key, &value);
57-
58-
origin->m_offset = offset;
59-
origin->m_length = len;
60-
61-
var->addOrigin(std::move(origin));
56+
var->addOrigin(len, offset);
6257
emplace(key, var);
6358
}
6459

6560

6661
void AnchoredSetVariable::set(const std::string &key,
6762
const std::string &value, size_t offset) {
68-
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
6963
VariableValue *var = new VariableValue(&m_name, &key, &value);
70-
71-
origin->m_offset = offset;
72-
origin->m_length = value.size();
73-
74-
var->addOrigin(std::move(origin));
64+
var->addOrigin(value.size(), offset);
7565
emplace(key, var);
7666
}
7767

src/anchored_variable.cc

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,9 @@ AnchoredVariable::AnchoredVariable(Transaction *t,
3131
const std::string &name)
3232
: m_transaction(t),
3333
m_offset(0),
34-
m_name(""),
34+
m_name(name),
3535
m_value(""),
36-
m_var(NULL) {
37-
m_name.append(name);
38-
m_var = new VariableValue(&m_name);
39-
}
40-
41-
42-
AnchoredVariable::~AnchoredVariable() {
43-
if (m_var) {
44-
delete (m_var);
45-
m_var = NULL;
46-
}
36+
m_var(&name) {
4737
}
4838

4939

@@ -54,58 +44,16 @@ void AnchoredVariable::unset() {
5444

5545
void AnchoredVariable::set(const std::string &a, size_t offset,
5646
size_t offsetLen) {
57-
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
58-
5947
m_offset = offset;
6048
m_value.assign(a.c_str(), a.size());
61-
origin->m_offset = offset;
62-
origin->m_length = offsetLen;
63-
m_var->addOrigin(std::move(origin));
49+
m_var.addOrigin(offsetLen, offset);
6450
}
6551

6652

6753
void AnchoredVariable::set(const std::string &a, size_t offset) {
68-
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
69-
7054
m_offset = offset;
7155
m_value.assign(a.c_str(), a.size());
72-
origin->m_offset = offset;
73-
origin->m_length = m_value.size();
74-
m_var->addOrigin(std::move(origin));
75-
}
76-
77-
78-
void AnchoredVariable::append(const std::string &a, size_t offset,
79-
bool spaceSeparator) {
80-
std::unique_ptr<VariableOrigin> origin(
81-
new VariableOrigin());
82-
83-
if (spaceSeparator && !m_value.empty()) {
84-
m_value.append(" " + a);
85-
} else {
86-
m_value.append(a);
87-
}
88-
m_offset = offset;
89-
origin->m_offset = offset;
90-
origin->m_length = a.size();
91-
m_var->addOrigin(std::move(origin));
92-
}
93-
94-
95-
void AnchoredVariable::append(const std::string &a, size_t offset,
96-
bool spaceSeparator, int size) {
97-
std::unique_ptr<VariableOrigin> origin(
98-
new VariableOrigin());
99-
100-
if (spaceSeparator && !m_value.empty()) {
101-
m_value.append(" " + a);
102-
} else {
103-
m_value.append(a);
104-
}
105-
m_offset = offset;
106-
origin->m_offset = offset;
107-
origin->m_length = size;
108-
m_var->addOrigin(std::move(origin));
56+
m_var.addOrigin(m_value.size(), offset);
10957
}
11058

11159

@@ -114,9 +62,8 @@ void AnchoredVariable::evaluate(std::vector<const VariableValue *> *l) {
11462
return;
11563
}
11664

117-
m_var->setValue(m_value);
118-
VariableValue *m_var2 = new VariableValue(m_var);
119-
l->push_back(m_var2);
65+
m_var.setValue(m_value);
66+
l->push_back(new VariableValue(&m_var));
12067
}
12168

12269

@@ -129,9 +76,7 @@ std::unique_ptr<std::string> AnchoredVariable::resolveFirst() {
12976
if (m_value.empty()) {
13077
return nullptr;
13178
}
132-
std::unique_ptr<std::string> a(new std::string());
133-
a->append(m_value);
134-
return a;
79+
return std::make_unique<std::string>(m_value);
13580
}
13681

13782

src/rule_with_operator.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ bool RuleWithOperator::evaluate(Transaction *trans,
317317
if (ret == true) {
318318
ruleMessage->m_match = m_operator->resolveMatchMessage(trans,
319319
key, value);
320-
for (auto &i : v->getOrigin()) {
321-
ruleMessage->m_reference.append(i->toText());
320+
for (const auto &i : v->getOrigin()) {
321+
ruleMessage->m_reference.append(i.toText());
322322
}
323323

324324
ruleMessage->m_reference.append(*valueTemp.second);

src/variables/remote_user.cc

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,41 @@ namespace variables {
3939
void RemoteUser::evaluate(Transaction *transaction,
4040
RuleWithActions *rule,
4141
std::vector<const VariableValue *> *l) {
42-
size_t pos;
43-
std::string base64;
44-
VariableValue *var;
45-
std::string header;
42+
std::vector<const VariableValue *> l2;
4643

47-
std::vector<const VariableValue *> *l2 = \
48-
new std::vector<const VariableValue *>();
49-
transaction->m_variableRequestHeaders.resolve("authorization", l2);
44+
transaction->m_variableRequestHeaders.resolve("authorization", &l2);
5045

51-
if (l2->size() < 1) {
52-
goto clear;
53-
}
46+
if (!l2.empty()) {
47+
const auto *v = l2[0];
5448

55-
header = std::string(l2->at(0)->getValue());
49+
const auto &header = v->getValue();
5650

57-
if (header.compare(0, 6, "Basic ") == 0) {
58-
base64 = std::string(header, 6, header.length());
59-
}
51+
std::string base64;
6052

61-
base64 = Utils::Base64::decode(base64);
53+
if (header.compare(0, 6, "Basic ") == 0) {
54+
base64 = std::string(header, 6, header.length());
55+
}
6256

63-
pos = base64.find(":");
64-
if (pos == std::string::npos) {
65-
goto clear;
66-
}
67-
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
57+
base64 = Utils::Base64::decode(base64);
6858

69-
var = new VariableValue(&l2->at(0)->getKeyWithCollection(),
70-
&transaction->m_variableRemoteUser);
59+
const auto pos = base64.find(":");
60+
if (pos != std::string::npos) {
61+
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
7162

72-
for (const auto &i : l2->at(0)->getOrigin()) {
73-
std::unique_ptr<VariableOrigin> origin(new VariableOrigin());
74-
origin->m_offset = i->m_offset;
75-
origin->m_length = i->m_length;
76-
var->addOrigin(std::move(origin));
77-
}
78-
l->push_back(var);
63+
auto var = std::make_unique<VariableValue>(&v->getKeyWithCollection(),
64+
&transaction->m_variableRemoteUser);
65+
66+
var->reserveOrigin(v->getOrigin().size());
67+
for (const auto &i : v->getOrigin()) {
68+
var->addOrigin(i);
69+
}
70+
l->push_back(var.release());
71+
}
7972

80-
clear:
81-
for (auto &a : *l2) {
82-
delete a;
73+
for (auto &a : l2) {
74+
delete a;
75+
}
8376
}
84-
l2->clear();
85-
delete l2;
8677
}
8778

8879

0 commit comments

Comments
 (0)