Perform CmdLine transformation in-place

This commit is contained in:
Eduardo Arias 2024-08-19 07:09:26 -07:00
parent 3ff72fbbc5
commit 13203ae5e7

View File

@ -20,10 +20,10 @@ namespace modsecurity::actions::transformations {
bool CmdLine::transform(std::string &value, const Transaction *trans) const {
std::string ret;
int space = 0;
char *d = value.data();
bool space = false;
for (auto& a : value) {
for (const auto& a : value) {
switch (a) {
/* remove some characters */
case '"':
@ -39,9 +39,9 @@ bool CmdLine::transform(std::string &value, const Transaction *trans) const {
case '\t':
case '\r':
case '\n':
if (space == 0) {
ret.append(" ");
space++;
if (space == false) {
*d++ = ' ';
space = true;
}
break;
@ -49,23 +49,24 @@ bool CmdLine::transform(std::string &value, const Transaction *trans) const {
case '/':
case '(':
if (space) {
ret.pop_back();
d--;
}
space = 0;
ret.append(&a, 1);
space = false;
*d++ = a;
break;
/* copy normal characters */
default :
char b = std::tolower(a);
ret.append(&b, 1);
space = 0;
*d++ = b;
space = false;
break;
}
}
const auto changed = ret != value;
value = ret;
const auto new_len = d - value.c_str();
const auto changed = new_len != value.length();
value.resize(new_len);
return changed;
}