Skip to content

Commit ba98278

Browse files
committed
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.
1 parent a1ba1bd commit ba98278

File tree

4 files changed

+13
-34
lines changed

4 files changed

+13
-34
lines changed

test/common/modsecurity_test.cc

+3-10
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,11 @@ bool ModSecurityTest<T>::load_test_json(const std::string &file) {
7272
for ( int i = 0; i < num_tests; i++ ) {
7373
yajl_val obj = node->u.array.values[i];
7474

75-
T *u = T::from_yajl_node(obj);
75+
auto u = std::unique_ptr<T>(T::from_yajl_node(obj));
7676
u->filename = file;
7777

78-
if (this->count(u->filename + ":" + u->name) == 0) {
79-
auto vec = new std::vector<T *>;
80-
vec->push_back(u);
81-
std::string filename(u->filename + ":" + u->name);
82-
this->insert({filename, vec});
83-
} else {
84-
auto vec = this->at(u->filename + ":" + u->name);
85-
vec->push_back(u);
86-
}
78+
const auto key = u->filename + ":" + u->name;
79+
(*this)[key].push_back(std::move(u));
8780
}
8881

8982
yajl_tree_free(node);

test/common/modsecurity_test.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern std::string default_test_path;
2929
namespace modsecurity_test {
3030

3131
template <class T> class ModSecurityTest :
32-
public std::unordered_map<std::string, std::vector<T *> *> {
32+
public std::unordered_map<std::string, std::vector<std::unique_ptr<T>>> {
3333
public:
3434
ModSecurityTest()
3535
: m_test_number(0),

test/regression/regression.cc

+7-14
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,14 @@ void actions(ModSecurityTestResults<RegressionTest> *r,
111111
}
112112

113113
void perform_unit_test(ModSecurityTest<RegressionTest> *test,
114-
std::vector<RegressionTest *> *tests,
115-
ModSecurityTestResults<RegressionTestResult> *res, int *count) {
116-
for (RegressionTest *t : *tests) {
114+
std::vector<std::unique_ptr<RegressionTest>> &tests,
115+
ModSecurityTestResults<RegressionTestResult> *res, int *count)
116+
{
117+
for (auto &t : tests) {
117118
ModSecurityTestResults<RegressionTest> r;
118119
RegressionTestResult *testRes = new RegressionTestResult();
119120

120-
testRes->test = t;
121+
testRes->test = t.get();
121122
r.status = 200;
122123
(*count)++;
123124

@@ -468,9 +469,9 @@ int main(int argc, char **argv)
468469
ModSecurityTestResults<RegressionTestResult> res;
469470
for (const std::string &a : keyList) {
470471
test_number++;
471-
if ((test.m_test_number == 0)
472+
if ((test.m_test_number == 0)
472473
|| (test_number == test.m_test_number)) {
473-
std::vector<RegressionTest *> *tests = test[a];
474+
auto &tests = test[a];
474475
perform_unit_test(&test, tests, &res, &counter);
475476
}
476477
}
@@ -523,14 +524,6 @@ int main(int argc, char **argv)
523524
std::cout << "disabled test(s)." << RESET << std::endl;
524525
}
525526

526-
for (auto a : test) {
527-
std::vector<RegressionTest *> *vec = a.second;
528-
for (int i = 0; i < vec->size(); i++) {
529-
delete vec->at(i);
530-
}
531-
delete vec;
532-
}
533-
534527
return failed;
535528
#endif
536529
}

test/unit/unit.cc

+2-9
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ int main(int argc, char **argv) {
250250
}
251251

252252
for (auto& [filename, tests] : test) {
253-
total += tests->size();
254-
for (auto t : *tests) {
253+
total += tests.size();
254+
for (auto &t : tests) {
255255
ModSecurityTestResults<UnitTest> r;
256256

257257
if (!test.m_automake_output) {
@@ -311,12 +311,5 @@ int main(int argc, char **argv) {
311311
}
312312
}
313313

314-
for (auto a : test) {
315-
auto vec = a.second;
316-
for(auto t : *vec)
317-
delete t;
318-
delete vec;
319-
}
320-
321314
return failed;
322315
}

0 commit comments

Comments
 (0)