Skip to content

Commit 74d150c

Browse files
committed
Perform RemoveCommentsChar, RemoveComments & ReplaceComments transformations in-place
1 parent da775ec commit 74d150c

File tree

3 files changed

+75
-85
lines changed

3 files changed

+75
-85
lines changed

src/actions/transformations/remove_comments.cc

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,34 @@
1919
namespace modsecurity::actions::transformations {
2020

2121

22-
bool RemoveComments::transform(std::string &value, const Transaction *trans) const {
23-
std::string ret;
24-
unsigned char *input;
25-
26-
input = reinterpret_cast<unsigned char *>
27-
(malloc(sizeof(char) * value.length()+1));
28-
29-
if (input == NULL) {
30-
return "";
31-
}
32-
33-
memcpy(input, value.c_str(), value.length()+1);
34-
35-
uint64_t input_len = value.size();
36-
uint64_t i, j, incomment;
22+
static inline int inplace(std::string &value) {
23+
auto input = reinterpret_cast<unsigned char*>(value.data());
24+
const auto input_len = value.length();
25+
bool changed = false, incomment = false;
26+
std::string::size_type i = 0, j = 0;
3727

38-
i = j = incomment = 0;
3928
while (i < input_len) {
40-
if (incomment == 0) {
29+
if (!incomment) {
4130
if ((input[i] == '/') && (i + 1 < input_len)
4231
&& (input[i + 1] == '*')) {
43-
incomment = 1;
32+
incomment = true;
33+
changed = true;
4434
i += 2;
4535
} else if ((input[i] == '<') && (i + 1 < input_len)
4636
&& (input[i + 1] == '!') && (i + 2 < input_len)
4737
&& (input[i+2] == '-') && (i + 3 < input_len)
4838
&& (input[i + 3] == '-')) {
49-
incomment = 1;
39+
incomment = true;
40+
changed = true;
5041
i += 4;
5142
} else if ((input[i] == '-') && (i + 1 < input_len)
5243
&& (input[i + 1] == '-')) {
5344
input[i] = ' ';
45+
changed = true;
5446
break;
5547
} else if (input[i] == '#') {
5648
input[i] = ' ';
49+
changed = true;
5750
break;
5851
} else {
5952
input[j] = input[i];
@@ -63,15 +56,15 @@ bool RemoveComments::transform(std::string &value, const Transaction *trans) con
6356
} else {
6457
if ((input[i] == '*') && (i + 1 < input_len)
6558
&& (input[i + 1] == '/')) {
66-
incomment = 0;
59+
incomment = false;
6760
i += 2;
6861
input[j] = input[i];
6962
i++;
7063
j++;
7164
} else if ((input[i] == '-') && (i + 1 < input_len)
7265
&& (input[i + 1] == '-') && (i + 2 < input_len)
7366
&& (input[i+2] == '>')) {
74-
incomment = 0;
67+
incomment = false;
7568
i += 3;
7669
input[j] = input[i];
7770
i++;
@@ -86,13 +79,14 @@ bool RemoveComments::transform(std::string &value, const Transaction *trans) con
8679
input[j++] = ' ';
8780
}
8881

89-
ret.assign(reinterpret_cast<char *>(input), j);
90-
free(input);
91-
92-
const auto changed = ret != value;
93-
value = ret;
82+
value.resize(j);
9483
return changed;
9584
}
9685

9786

87+
bool RemoveComments::transform(std::string &value, const Transaction *trans) const {
88+
return inplace(value);
89+
}
90+
91+
9892
} // namespace modsecurity::actions::transformations

src/actions/transformations/remove_comments_char.cc

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,43 @@ RemoveCommentsChar::RemoveCommentsChar(const std::string &action)
2323
this->action_kind = 1;
2424
}
2525

26-
bool RemoveCommentsChar::transform(std::string &val, const Transaction *trans) const {
27-
size_t i = 0;
28-
std::string transformed_value;
29-
transformed_value.reserve(val.size());
26+
bool RemoveCommentsChar::transform(std::string &value, const Transaction *trans) const {
27+
char *d = value.data();
28+
const char *s = d;
29+
const char *e = s + value.size();
3030

31-
while (i < val.size()) {
32-
if (val.at(i) == '/'
33-
&& (i+1 < val.size()) && val.at(i+1) == '*') {
34-
i += 2;
35-
} else if (val.at(i) == '*'
36-
&& (i+1 < val.size()) && val.at(i+1) == '/') {
37-
i += 2;
38-
} else if (val.at(i) == '<'
39-
&& (i+1 < val.size())
40-
&& val.at(i+1) == '!'
41-
&& (i+2 < val.size())
42-
&& val.at(i+2) == '-'
43-
&& (i+3 < val.size())
44-
&& val.at(i+3) == '-') {
45-
i += 4;
46-
} else if (val.at(i) == '-'
47-
&& (i+1 < val.size()) && val.at(i+1) == '-'
48-
&& (i+2 < val.size()) && val.at(i+2) == '>') {
49-
i += 3;
50-
} else if (val.at(i) == '-'
51-
&& (i+1 < val.size()) && val.at(i+1) == '-') {
52-
i += 2;
53-
} else if (val.at(i) == '#') {
54-
i += 1;
31+
while (s < e) {
32+
if (*s == '/'
33+
&& (s+1 < e) && *(s+1) == '*') {
34+
s += 2;
35+
} else if (*s == '*'
36+
&& (s+1 < e) && *(s+1) == '/') {
37+
s += 2;
38+
} else if (*s == '<'
39+
&& (s+1 < e)
40+
&& *(s+1) == '!'
41+
&& (s+2 < e)
42+
&& *(s+2) == '-'
43+
&& (s+3 < e)
44+
&& *(s+3) == '-') {
45+
s += 4;
46+
} else if (*s == '-'
47+
&& (s+1 < e) && *(s+1) == '-'
48+
&& (s+2 < e) && *(s+2) == '>') {
49+
s += 3;
50+
} else if (*s == '-'
51+
&& (s+1 < e) && *(s+1) == '-') {
52+
s += 2;
53+
} else if (*s == '#') {
54+
s += 1;
5555
} else {
56-
transformed_value += val.at(i);
57-
i++;
56+
*d++ = *s++;
5857
}
5958
}
6059

61-
const auto changed = transformed_value != val;
62-
val = transformed_value;
60+
const auto changed = d != s;
61+
const auto new_len = d - value.c_str();
62+
value.resize(new_len);
6363
return changed;
6464
}
6565

src/actions/transformations/replace_comments.cc

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,28 @@
1919
namespace modsecurity::actions::transformations {
2020

2121

22-
ReplaceComments::ReplaceComments(const std::string &action)
23-
: Transformation(action) {
24-
this->action_kind = 1;
25-
}
26-
27-
28-
bool ReplaceComments::transform(std::string &value, const Transaction *trans) const {
29-
uint64_t i, j, incomment;
30-
31-
char *input = reinterpret_cast<char *>(
32-
malloc(sizeof(char) * value.size() + 1));
33-
memcpy(input, value.c_str(), value.size() + 1);
34-
input[value.size()] = '\0';
35-
36-
i = j = incomment = 0;
37-
while (i < value.size()) {
38-
if (incomment == 0) {
39-
if ((input[i] == '/') && (i + 1 < value.size())
22+
static inline bool inplace(std::string &value) {
23+
auto input = reinterpret_cast<unsigned char*>(value.data());
24+
const auto input_len = value.length();
25+
bool changed = false, incomment = false;
26+
std::string::size_type i = 0, j = 0;
27+
28+
while (i < input_len) {
29+
if (!incomment) {
30+
if ((input[i] == '/') && (i + 1 < input_len)
4031
&& (input[i + 1] == '*')) {
41-
incomment = 1;
32+
incomment = true;
4233
i += 2;
34+
changed = true;
4335
} else {
4436
input[j] = input[i];
4537
i++;
4638
j++;
4739
}
4840
} else {
49-
if ((input[i] == '*') && (i + 1 < value.size())
41+
if ((input[i] == '*') && (i + 1 < input_len)
5042
&& (input[i + 1] == '/')) {
51-
incomment = 0;
43+
incomment = false;
5244
i += 2;
5345
input[j] = ' ';
5446
j++;
@@ -62,15 +54,19 @@ bool ReplaceComments::transform(std::string &value, const Transaction *trans) co
6254
input[j++] = ' ';
6355
}
6456

57+
value.resize(j);
58+
return changed;
59+
}
6560

66-
std::string resp;
67-
resp.append(reinterpret_cast<char *>(input), j);
6861

69-
free(input);
62+
ReplaceComments::ReplaceComments(const std::string &action)
63+
: Transformation(action) {
64+
this->action_kind = 1;
65+
}
7066

71-
const auto changed = resp != value;
72-
value = resp;
73-
return changed;
67+
68+
bool ReplaceComments::transform(std::string &value, const Transaction *trans) const {
69+
return inplace(value);
7470
}
7571

7672

0 commit comments

Comments
 (0)