From 13203ae5e7dba5ce8e80fd720573ca6e1e3155e6 Mon Sep 17 00:00:00 2001 From: Eduardo Arias Date: Mon, 19 Aug 2024 07:09:26 -0700 Subject: [PATCH] Perform CmdLine transformation in-place --- src/actions/transformations/cmd_line.cc | 27 +++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/actions/transformations/cmd_line.cc b/src/actions/transformations/cmd_line.cc index cac1d2df..8db1529d 100644 --- a/src/actions/transformations/cmd_line.cc +++ b/src/actions/transformations/cmd_line.cc @@ -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; }