Replace usage of range-checked 'at' method when vector/string has already been size checked

This commit is contained in:
Eduardo Arias
2024-06-02 20:07:19 +00:00
parent 99ce9779e6
commit 0613ceeb75
11 changed files with 26 additions and 30 deletions

View File

@@ -23,13 +23,14 @@
namespace modsecurity {
namespace operators {
bool ContainsWord::acceptableChar(const std::string& a, size_t pos) {
inline bool ContainsWord::acceptableChar(const std::string& a, size_t pos) {
if (a.size() - 1 < pos) {
return false;
}
if ((a.at(pos) >= 65 && a.at(pos) <= 90) ||
(a.at(pos) >= 97 && a.at(pos) <= 122)) {
const auto ch = a[pos];
if ((ch >= 65 && ch <= 90) ||
(ch >= 97 && ch <= 122)) {
return false;
}

View File

@@ -77,7 +77,7 @@ bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
pclose(in);
res.append(s.str());
if (res.size() > 1 && res.at(0) != '1') {
if (res.size() > 1 && res[0] != '1') {
return true; /* match */
}

View File

@@ -32,7 +32,7 @@ static inline std::string parse_pm_content(const std::string &op_parm) {
auto size = op_parm.size() - offset;
if (size >= 2 &&
op_parm.at(offset) == '\"' && op_parm.back() == '\"') {
op_parm[offset] == '\"' && op_parm.back() == '\"') {
offset++;
size -= 2;
}

View File

@@ -115,8 +115,8 @@ bool ValidateByteRange::evaluate(Transaction *transaction, RuleWithActions *rule
bool ret = true;
size_t count = 0;
for (int i = 0; i < input.length(); i++) {
int x = (unsigned char) input.at(i);
for (std::string::size_type i = 0; i < input.length(); i++) {
int x = (unsigned char) input[i];
if (!(table[x >> 3] & (1 << (x & 0x7)))) {
// debug(9, "Value " + std::to_string(x) + " in " +
// input + " ouside range: " + param);