Skip to content

Commit

Permalink
leverage auto to simplify declarations
Browse files Browse the repository at this point in the history
- declare objects as references if possible to avoid copies
- declare variables as const if possible to signal that they're not
  modified after initialization
  • Loading branch information
eduar-hte committed May 5, 2024
1 parent cfab750 commit 445e2eb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
13 changes: 5 additions & 8 deletions src/transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,9 @@ bool Transaction::extractArguments(const std::string &orig,
int invalid = 0;
int changed = 0;

std::string key;
std::string value;
std::pair<std::string, std::string> key_value_pair = utils::string::ssplit_pair(t, sep2);
key = key_value_pair.first;
value = key_value_pair.second;
const auto key_value_pair = utils::string::ssplit_pair(t, sep2);
const auto &key = key_value_pair.first;
const auto &value = key_value_pair.second;

key_s = (key.length() + 1);
value_s = (value.length() + 1);
Expand Down Expand Up @@ -1430,12 +1428,11 @@ int Transaction::processLogging() {

if (!this->m_auditLogModifier.empty()) {
ms_dbg(4, "There was an audit log modifier for this transaction.");
std::list<std::pair<int, std::string>>::iterator it;
ms_dbg(7, "AuditLog parts before modification(s): " +
std::to_string(parts) + ".");
for (it = m_auditLogModifier.begin();
for (auto it = m_auditLogModifier.begin();
it != m_auditLogModifier.end(); ++it) {
std::pair <int, std::string> p = *it;
const auto &p = *it;
if (p.first == 0) { // Add
parts = this->m_rules->m_auditLog->addParts(parts,
p.second);
Expand Down
8 changes: 4 additions & 4 deletions test/regression/regression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
}
#endif

for (std::pair<std::string, std::string> headers :
for (const auto &headers :
t->request_headers) {
modsec_transaction->addRequestHeader(headers.first.c_str(),
headers.second.c_str());
Expand All @@ -349,7 +349,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
}
#endif

for (std::pair<std::string, std::string> headers :
for (const auto &headers :
t->response_headers) {
modsec_transaction->addResponseHeader(headers.first.c_str(),
headers.second.c_str());
Expand Down Expand Up @@ -529,7 +529,7 @@ int main(int argc, char **argv) {
int counter = 0;

std::list<std::string> keyList;
for (std::pair<std::string, std::vector<RegressionTest *> *> a : test) {
for (const auto &a : test) {
keyList.push_back(a.first);
}
keyList.sort();
Expand Down Expand Up @@ -597,7 +597,7 @@ int main(int argc, char **argv) {
std::cout << "disabled test(s)." << RESET << std::endl;
}

for (std::pair<std::string, std::vector<RegressionTest *> *> a : test) {
for (auto a : test) {
std::vector<RegressionTest *> *vec = a.second;
for (int i = 0; i < vec->size(); i++) {
delete vec->at(i);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ int main(int argc, char **argv) {
test.load_tests("test-cases/secrules-language-tests/transformations");
}

for (std::pair<std::string, std::vector<UnitTest *> *> a : test) {
std::vector<UnitTest *> *tests = a.second;
for (auto a : test) {
auto *tests = a.second;

total += tests->size();
for (UnitTest *t : *tests) {
Expand Down Expand Up @@ -217,8 +217,8 @@ int main(int argc, char **argv) {
}
}

for (std::pair<std::string, std::vector<UnitTest *> *> a : test) {
std::vector<UnitTest *> *vec = a.second;
for (auto a : test) {
auto *vec = a.second;
for (int i = 0; i < vec->size(); i++) {
delete vec->at(i);
}
Expand Down

0 comments on commit 445e2eb

Please sign in to comment.