Skip to content

Commit 13203ae

Browse files
committed
Perform CmdLine transformation in-place
1 parent 3ff72fb commit 13203ae

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/actions/transformations/cmd_line.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ namespace modsecurity::actions::transformations {
2020

2121

2222
bool CmdLine::transform(std::string &value, const Transaction *trans) const {
23-
std::string ret;
24-
int space = 0;
23+
char *d = value.data();
24+
bool space = false;
2525

26-
for (auto& a : value) {
26+
for (const auto& a : value) {
2727
switch (a) {
2828
/* remove some characters */
2929
case '"':
@@ -39,33 +39,34 @@ bool CmdLine::transform(std::string &value, const Transaction *trans) const {
3939
case '\t':
4040
case '\r':
4141
case '\n':
42-
if (space == 0) {
43-
ret.append(" ");
44-
space++;
42+
if (space == false) {
43+
*d++ = ' ';
44+
space = true;
4545
}
4646
break;
4747

4848
/* remove space before / or ( */
4949
case '/':
5050
case '(':
5151
if (space) {
52-
ret.pop_back();
52+
d--;
5353
}
54-
space = 0;
55-
ret.append(&a, 1);
54+
space = false;
55+
*d++ = a;
5656
break;
5757

5858
/* copy normal characters */
5959
default :
6060
char b = std::tolower(a);
61-
ret.append(&b, 1);
62-
space = 0;
61+
*d++ = b;
62+
space = false;
6363
break;
6464
}
6565
}
6666

67-
const auto changed = ret != value;
68-
value = ret;
67+
const auto new_len = d - value.c_str();
68+
const auto changed = new_len != value.length();
69+
value.resize(new_len);
6970
return changed;
7071
}
7172

0 commit comments

Comments
 (0)