Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,23 @@
inline std::string toHexIfNeeded(const std::string &str, bool escape_spec = false) {
// escape_spec: escape special chars or not
// spec chars: '"' (quotation mark, ascii 34), '\' (backslash, ascii 92)
std::stringstream res;

for (const auto ch : str) {
int c = (unsigned char)ch;
if (c < 32 || c > 126 || (escape_spec == true && (c == 34 || c == 92))) {
res << "\\x" << std::setw(2) << std::setfill('0') << std::hex << c;
static constexpr char hex[] = "0123456789abcdef";

std::string res;
res.reserve(str.size());

for (const char ch : str) {
const unsigned char c = static_cast<unsigned char>(ch);

Check warning on line 115 in src/utils/string.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace the redundant type with "auto".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaGeO9679_L81x-M9oAJ&open=AaGeO9679_L81x-M9oAJ&pullRequest=3743
if (c < 32 || c > 126 || (escape_spec && (c == '"' || c == '\\'))) {
res += '\\';
res += 'x';
res += hex[c >> 4];

Check warning on line 119 in src/utils/string.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "std::byte" for byte-oriented data manipulation.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaGeO9679_L81x-M9oAK&open=AaGeO9679_L81x-M9oAK&pullRequest=3743
res += hex[c & 0x0F];

Check warning on line 120 in src/utils/string.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "std::byte" for byte-oriented data manipulation.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaGeO9679_L81x-M9oAL&open=AaGeO9679_L81x-M9oAL&pullRequest=3743
} else {
res << ch;
res += ch;
}
}

return res.str();
return res;
}


Expand Down
Loading