Skip to content

Commit ec506da

Browse files
authored
Merge pull request #3280 from eduar-hte/range-checked-at
Replace usage of range-checked 'at' method when vector/string has already been size checked
2 parents 99ce977 + 0613cee commit ec506da

11 files changed

+26
-30
lines changed

headers/modsecurity/anchored_set_variable_translation_proxy.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,9 @@ class AnchoredSetVariableTranslationProxy {
100100
return nullptr;
101101
}
102102

103-
std::unique_ptr<std::string> ret(new std::string(""));
103+
auto ret = std::make_unique<std::string>(l[0]->getValue());
104104

105-
ret->assign(l.at(0)->getValue());
106-
107-
while (!l.empty()) {
108-
auto &a = l.back();
109-
l.pop_back();
105+
for(auto a : l) {
110106
delete a;
111107
}
112108

headers/modsecurity/rules.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ namespace modsecurity {
4141
class Rules {
4242
public:
4343
void dump() const {
44-
for (int j = 0; j < m_rules.size(); j++) {
45-
std::cout << " Rule ID: " << m_rules.at(j)->getReference();
46-
std::cout << "--" << m_rules.at(j) << std::endl;
44+
for (const auto &r : m_rules) {
45+
std::cout << " Rule ID: " << r->getReference();
46+
std::cout << "--" << r << std::endl;
4747
}
4848
}
4949

src/actions/xmlns.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool XmlNS::init(std::string *error) {
4343
return false;
4444
}
4545

46-
if (m_href.at(0) == '\'' && m_href.size() > 3) {
46+
if (m_href[0] == '\'' && m_href.size() > 3) {
4747
m_href.erase(0, 1);
4848
m_href.pop_back();
4949
}

src/operators/contains_word.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
namespace modsecurity {
2424
namespace operators {
2525

26-
bool ContainsWord::acceptableChar(const std::string& a, size_t pos) {
26+
inline bool ContainsWord::acceptableChar(const std::string& a, size_t pos) {
2727
if (a.size() - 1 < pos) {
2828
return false;
2929
}
3030

31-
if ((a.at(pos) >= 65 && a.at(pos) <= 90) ||
32-
(a.at(pos) >= 97 && a.at(pos) <= 122)) {
31+
const auto ch = a[pos];
32+
if ((ch >= 65 && ch <= 90) ||
33+
(ch >= 97 && ch <= 122)) {
3334
return false;
3435
}
3536

src/operators/inspect_file.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
7777
pclose(in);
7878

7979
res.append(s.str());
80-
if (res.size() > 1 && res.at(0) != '1') {
80+
if (res.size() > 1 && res[0] != '1') {
8181
return true; /* match */
8282
}
8383

src/operators/pm.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static inline std::string parse_pm_content(const std::string &op_parm) {
3232

3333
auto size = op_parm.size() - offset;
3434
if (size >= 2 &&
35-
op_parm.at(offset) == '\"' && op_parm.back() == '\"') {
35+
op_parm[offset] == '\"' && op_parm.back() == '\"') {
3636
offset++;
3737
size -= 2;
3838
}

src/operators/validate_byte_range.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ bool ValidateByteRange::evaluate(Transaction *transaction, RuleWithActions *rule
115115
bool ret = true;
116116

117117
size_t count = 0;
118-
for (int i = 0; i < input.length(); i++) {
119-
int x = (unsigned char) input.at(i);
118+
for (std::string::size_type i = 0; i < input.length(); i++) {
119+
int x = (unsigned char) input[i];
120120
if (!(table[x >> 3] & (1 << (x & 0x7)))) {
121121
// debug(9, "Value " + std::to_string(x) + " in " +
122122
// input + " ouside range: " + param);

src/rule_with_operator.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars,
194194
vars->push_back(variable);
195195
}
196196

197-
for (int i = 0; i < addition.size(); i++) {
198-
Variable *variable = addition.at(i);
197+
for (auto *variable : addition) {
199198
vars->push_back(variable);
200199
}
201200
}

src/rules_set_phases.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ int RulesSetPhases::append(RulesSetPhases *from, std::ostringstream *err) {
4545

4646
for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
4747
v.reserve(m_rulesAtPhase[i].size());
48-
for (size_t z = 0; z < m_rulesAtPhase[i].size(); z++) {
49-
RuleWithOperator *rule_ckc = dynamic_cast<RuleWithOperator *>(m_rulesAtPhase[i].at(z).get());
48+
for (const auto &r : m_rulesAtPhase[i].m_rules) {
49+
const auto *rule_ckc = dynamic_cast<const RuleWithOperator *>(r.get());
5050
if (!rule_ckc) {
5151
continue;
5252
}

src/transaction.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ int Transaction::processURI(const char *uri, const char *method,
405405

406406
std::string parsedURI = m_uri_decoded;
407407
// The more popular case is without domain
408-
if (!m_uri_decoded.empty() && m_uri_decoded.at(0) != '/') {
408+
if (!m_uri_decoded.empty() && m_uri_decoded[0] != '/') {
409409
bool fullDomain = true;
410410
size_t scheme = m_uri_decoded.find(":")+1;
411411
if (scheme == std::string::npos) {
@@ -540,7 +540,7 @@ int Transaction::addRequestHeader(const std::string& key,
540540
}
541541

542542
// ltrim the key - following the modsec v2 way
543-
while (ckey.empty() == false && isspace(ckey.at(0))) {
543+
while (ckey.empty() == false && isspace(ckey[0])) {
544544
ckey.erase(0, 1);
545545
localOffset++;
546546
}

src/utils/string.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ inline std::string toHexIfNeeded(const std::string &str, bool escape_spec = fals
107107
// spec chars: '"' (quotation mark, ascii 34), '\' (backslash, ascii 92)
108108
std::stringstream res;
109109

110-
for (int i = 0; i < str.size(); i++) {
111-
int c = (unsigned char)str.at(i);
110+
for (const auto ch : str) {
111+
int c = (unsigned char)ch;
112112
if (c < 32 || c > 126 || (escape_spec == true && (c == 34 || c == 92))) {
113113
res << "\\x" << std::setw(2) << std::setfill('0') << std::hex << c;
114114
} else {
115-
res << str.at(i);
115+
res << ch;
116116
}
117117
}
118118

@@ -177,22 +177,22 @@ inline void replaceAll(std::string &str, std::string_view from,
177177

178178

179179
inline std::string removeWhiteSpacesIfNeeded(std::string a) {
180-
while (a.size() > 1 && a.at(0) == ' ') {
180+
while (a.size() > 1 && a.front() == ' ') {
181181
a.erase(0, 1);
182182
}
183-
while (a.size() > 1 && a.at(a.length()-1) == ' ') {
183+
while (a.size() > 1 && a.back() == ' ') {
184184
a.pop_back();
185185
}
186186
return a;
187187
}
188188

189189

190190
inline std::string removeBracketsIfNeeded(std::string a) {
191-
if (a.length() > 1 && a.at(0) == '"' && a.at(a.length()-1) == '"') {
191+
if (a.length() > 1 && a.front() == '"' && a.back() == '"') {
192192
a.pop_back();
193193
a.erase(0, 1);
194194
}
195-
if (a.length() > 1 && a.at(0) == '\'' && a.at(a.length()-1) == '\'') {
195+
if (a.length() > 1 && a.front() == '\'' && a.back() == '\'') {
196196
a.pop_back();
197197
a.erase(0, 1);
198198
}

0 commit comments

Comments
 (0)