From ba982789c21845fcafcb2ba6e9033c2100a5d034 Mon Sep 17 00:00:00 2001 From: Eduardo Arias Date: Tue, 10 Sep 2024 14:32:38 -0300 Subject: [PATCH] Simplified lifetime management of tests - Addresses Sonarcloud issues: - Rewrite the code so that you no longer need this "delete". - Make the type of this variable a reference-to-const. --- test/common/modsecurity_test.cc | 13 +++---------- test/common/modsecurity_test.h | 2 +- test/regression/regression.cc | 21 +++++++-------------- test/unit/unit.cc | 11 ++--------- 4 files changed, 13 insertions(+), 34 deletions(-) diff --git a/test/common/modsecurity_test.cc b/test/common/modsecurity_test.cc index 0f769054e8..1e0585d475 100644 --- a/test/common/modsecurity_test.cc +++ b/test/common/modsecurity_test.cc @@ -72,18 +72,11 @@ bool ModSecurityTest::load_test_json(const std::string &file) { for ( int i = 0; i < num_tests; i++ ) { yajl_val obj = node->u.array.values[i]; - T *u = T::from_yajl_node(obj); + auto u = std::unique_ptr(T::from_yajl_node(obj)); u->filename = file; - if (this->count(u->filename + ":" + u->name) == 0) { - auto vec = new std::vector; - vec->push_back(u); - std::string filename(u->filename + ":" + u->name); - this->insert({filename, vec}); - } else { - auto vec = this->at(u->filename + ":" + u->name); - vec->push_back(u); - } + const auto key = u->filename + ":" + u->name; + (*this)[key].push_back(std::move(u)); } yajl_tree_free(node); diff --git a/test/common/modsecurity_test.h b/test/common/modsecurity_test.h index 8b55a16c62..e7a8b1b3e5 100644 --- a/test/common/modsecurity_test.h +++ b/test/common/modsecurity_test.h @@ -29,7 +29,7 @@ extern std::string default_test_path; namespace modsecurity_test { template class ModSecurityTest : - public std::unordered_map *> { + public std::unordered_map>> { public: ModSecurityTest() : m_test_number(0), diff --git a/test/regression/regression.cc b/test/regression/regression.cc index 62ab1e8919..6d7b9dc3a1 100644 --- a/test/regression/regression.cc +++ b/test/regression/regression.cc @@ -111,13 +111,14 @@ void actions(ModSecurityTestResults *r, } void perform_unit_test(ModSecurityTest *test, - std::vector *tests, - ModSecurityTestResults *res, int *count) { - for (RegressionTest *t : *tests) { + std::vector> &tests, + ModSecurityTestResults *res, int *count) +{ + for (auto &t : tests) { ModSecurityTestResults r; RegressionTestResult *testRes = new RegressionTestResult(); - testRes->test = t; + testRes->test = t.get(); r.status = 200; (*count)++; @@ -468,9 +469,9 @@ int main(int argc, char **argv) ModSecurityTestResults res; for (const std::string &a : keyList) { test_number++; - if ((test.m_test_number == 0) + if ((test.m_test_number == 0) || (test_number == test.m_test_number)) { - std::vector *tests = test[a]; + auto &tests = test[a]; perform_unit_test(&test, tests, &res, &counter); } } @@ -523,14 +524,6 @@ int main(int argc, char **argv) std::cout << "disabled test(s)." << RESET << std::endl; } - for (auto a : test) { - std::vector *vec = a.second; - for (int i = 0; i < vec->size(); i++) { - delete vec->at(i); - } - delete vec; - } - return failed; #endif } diff --git a/test/unit/unit.cc b/test/unit/unit.cc index d1b871c6cd..a1c6b0b3c6 100644 --- a/test/unit/unit.cc +++ b/test/unit/unit.cc @@ -250,8 +250,8 @@ int main(int argc, char **argv) { } for (auto& [filename, tests] : test) { - total += tests->size(); - for (auto t : *tests) { + total += tests.size(); + for (auto &t : tests) { ModSecurityTestResults r; if (!test.m_automake_output) { @@ -311,12 +311,5 @@ int main(int argc, char **argv) { } } - for (auto a : test) { - auto vec = a.second; - for(auto t : *vec) - delete t; - delete vec; - } - return failed; }