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