Skip to content

Commit 64375ab

Browse files
committed
Minor codebase improvements suggested by Sonarcloud
- src/modsecurity.cc - Replace the redundant type with "auto". - src/transaction.cc - Avoid this unnecessary copy by using a "const" reference. - test/common/custom_debug_log.cc - Use "=default" instead of the default implementation of this special member functions. - Removed the unnecessary destructor override instead. - Annotate this function with "override" or "final". - Removed the unnecessary destructor override instead. - Remove this "const" qualifier from the return type in all declarations. - test/common/modsecurity_test_context.h - Replace the redundant type with "auto". - test/regression/regression.cc - Use the "nullptr" literal. - Replace this declaration by a structured binding declaration. - Replace "reinterpret_cast" with a safer operation. - Change this raw for-loop to a range for-loop or an "std::for_each".
1 parent 147cee9 commit 64375ab

File tree

6 files changed

+25
-32
lines changed

6 files changed

+25
-32
lines changed

src/modsecurity.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ void ModSecurity::serverLog(void *data, const RuleMessage &rm) {
199199

200200
if (m_logProperties & TextLogProperty) {
201201
auto d = rm.log();
202-
const void *a = static_cast<const void *>(d.c_str());
202+
auto a = static_cast<const void *>(d.c_str());
203203
m_logCb(data, a);
204204
return;
205205
}
206206

207207
if (m_logProperties & RuleMessageLogProperty) {
208-
const void *a = static_cast<const void *>(&rm);
208+
auto a = static_cast<const void *>(&rm);
209209
m_logCb(data, a);
210210
return;
211211
}

src/transaction.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ std::string Transaction::toOldAuditLogFormat(int parts,
15291529
}
15301530
if (parts & audit_log::AuditLog::HAuditLogPart) {
15311531
audit_log << "--" << trailer << "-" << "H--" << std::endl;
1532-
for (auto a : m_rulesMessages) {
1532+
for (const auto &a : m_rulesMessages) {
15331533
audit_log << a.log(0, m_httpCodeReturned) << std::endl;
15341534
}
15351535
audit_log << std::endl;

test/common/custom_debug_log.cc

+2-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323

2424
namespace modsecurity_test
2525
{
26-
27-
CustomDebugLog::~CustomDebugLog() {}
28-
2926
void CustomDebugLog::write(int level, const std::string &message)
3027
{
3128
m_log << "[" << level << "] " << message << std::endl;
@@ -39,14 +36,14 @@ namespace modsecurity_test
3936
m_log << msgf << std::endl;
4037
}
4138

42-
bool const CustomDebugLog::contains(const std::string &pattern) const
39+
bool CustomDebugLog::contains(const std::string &pattern) const
4340
{
4441
modsecurity::Utils::Regex re(pattern);
4542
std::string s = m_log.str();
4643
return modsecurity::Utils::regex_search(s, re);
4744
}
4845

49-
std::string const CustomDebugLog::log_messages() const
46+
std::string CustomDebugLog::log_messages() const
5047
{
5148
return m_log.str();
5249
}

test/common/custom_debug_log.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ namespace modsecurity_test {
2626
class CustomDebugLog : public modsecurity::debug_log::DebugLog {
2727
public:
2828
CustomDebugLog *new_instance();
29-
~CustomDebugLog();
3029

3130
void write(int level, const std::string& message) override;
3231
void write(int level, const std::string &id,
3332
const std::string &uri, const std::string &msg) override;
34-
bool const contains(const std::string& pattern) const;
35-
std::string const log_messages() const;
33+
bool contains(const std::string& pattern) const;
34+
std::string log_messages() const;
3635
std::string error_log_messages();
3736
int getDebugLogLevel() override;
3837

test/common/modsecurity_test_context.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace modsecurity_test
3636
private:
3737
static void logCb(void *data, const void *msgv)
3838
{
39-
const char *msg = reinterpret_cast<const char *>(msgv);
40-
std::stringstream *ss = (std::stringstream *)data;
39+
auto msg = reinterpret_cast<const char *>(msgv);
40+
auto ss = reinterpret_cast<std::stringstream *>(data);
4141
*ss << msg << std::endl;
4242
}
4343
};

test/regression/regression.cc

+16-19
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ void actions(ModSecurityTestResults<RegressionTest> *r,
107107
{
108108
r->status = it.status;
109109
}
110-
if (it.url != NULL)
110+
if (it.url != nullptr)
111111
{
112112
r->location.append(it.url);
113113
free(it.url);
114-
it.url = NULL;
114+
it.url = nullptr;
115115
}
116-
if (it.log != NULL)
116+
if (it.log != nullptr)
117117
{
118118
*serverLog << it.log;
119119
free(it.log);
120-
it.log = NULL;
120+
it.log = nullptr;
121121
}
122122
}
123123
}
@@ -329,10 +329,10 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
329329

330330
actions(&r, &modsec_transaction, &context.m_server_log);
331331

332-
for (const auto &headers : t->request_headers)
332+
for (const auto &[name, value] : t->request_headers)
333333
{
334-
modsec_transaction.addRequestHeader(headers.first.c_str(),
335-
headers.second.c_str());
334+
modsec_transaction.addRequestHeader(name.c_str(),
335+
value.c_str());
336336
}
337337

338338
modsec_transaction.processRequestHeaders();
@@ -344,10 +344,10 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
344344
modsec_transaction.processRequestBody();
345345
actions(&r, &modsec_transaction, &context.m_server_log);
346346

347-
for (const auto &headers : t->response_headers)
347+
for (const auto &[name, value] : t->response_headers)
348348
{
349-
modsec_transaction.addResponseHeader(headers.first.c_str(),
350-
headers.second.c_str());
349+
modsec_transaction.addResponseHeader(name.c_str(),
350+
value.c_str());
351351
}
352352

353353
modsec_transaction.processResponseHeaders(r.status,
@@ -362,7 +362,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
362362

363363
modsec_transaction.processLogging();
364364

365-
const auto *d = reinterpret_cast<CustomDebugLog *>(context.m_modsec_rules.m_debugLog);
365+
const auto *d = static_cast<CustomDebugLog *>(context.m_modsec_rules.m_debugLog);
366366

367367
if (!d->contains(t->debug_log))
368368
{
@@ -530,9 +530,9 @@ int main(int argc, char **argv)
530530
int counter = 0;
531531

532532
std::list<std::string> keyList;
533-
for (const auto &a : test)
533+
for (const auto &[name, tests] : test)
534534
{
535-
keyList.push_back(a.first);
535+
keyList.push_back(name);
536536
}
537537
keyList.sort();
538538

@@ -611,13 +611,10 @@ int main(int argc, char **argv)
611611
std::cout << "disabled test(s)." << RESET << std::endl;
612612
}
613613

614-
for (auto a : test)
614+
for (auto &[name, vec] : test)
615615
{
616-
std::vector<RegressionTest *> *vec = a.second;
617-
for (int i = 0; i < vec->size(); i++)
618-
{
619-
delete vec->at(i);
620-
}
616+
for (auto x : vec)
617+
delete x;
621618
delete vec;
622619
}
623620

0 commit comments

Comments
 (0)