Skip to content

Commit a1ba1bd

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.
1 parent b6a29fa commit a1ba1bd

File tree

6 files changed

+22
-25
lines changed

6 files changed

+22
-25
lines changed

src/modsecurity.cc

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace modsecurity_test {
2525

26-
CustomDebugLog::~CustomDebugLog() {}
27-
2826
void CustomDebugLog::write(int level, const std::string &message) {
2927
m_log << "[" << level << "] " << message << std::endl;
3028
}
@@ -36,13 +34,13 @@ namespace modsecurity_test {
3634
m_log << msgf << std::endl;
3735
}
3836

39-
bool const CustomDebugLog::contains(const std::string &pattern) const {
37+
bool CustomDebugLog::contains(const std::string &pattern) const {
4038
modsecurity::Utils::Regex re(pattern);
4139
std::string s = m_log.str();
4240
return modsecurity::Utils::regex_search(s, re);
4341
}
4442

45-
std::string const CustomDebugLog::log_messages() const {
43+
std::string CustomDebugLog::log_messages() const {
4644
return m_log.str();
4745
}
4846

test/common/custom_debug_log.h

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ void actions(ModSecurityTestResults<RegressionTest> *r,
9797
if (it.status != 0) {
9898
r->status = it.status;
9999
}
100-
if (it.url != NULL) {
100+
if (it.url != nullptr) {
101101
r->location.append(it.url);
102102
free(it.url);
103-
it.url = NULL;
103+
it.url = nullptr;
104104
}
105-
if (it.log != NULL) {
105+
if (it.log != nullptr) {
106106
*serverLog << it.log;
107107
free(it.log);
108-
it.log = NULL;
108+
it.log = nullptr;
109109
}
110110
}
111111
}
@@ -283,9 +283,9 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
283283

284284
actions(&r, &modsec_transaction, &context.m_server_log);
285285

286-
for (const auto &headers : t->request_headers) {
287-
modsec_transaction.addRequestHeader(headers.first.c_str(),
288-
headers.second.c_str());
286+
for (const auto &[name, value] : t->request_headers) {
287+
modsec_transaction.addRequestHeader(name.c_str(),
288+
value.c_str());
289289
}
290290

291291
modsec_transaction.processRequestHeaders();
@@ -297,9 +297,9 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
297297
modsec_transaction.processRequestBody();
298298
actions(&r, &modsec_transaction, &context.m_server_log);
299299

300-
for (const auto &headers : t->response_headers) {
301-
modsec_transaction.addResponseHeader(headers.first.c_str(),
302-
headers.second.c_str());
300+
for (const auto &[name, value] : t->response_headers) {
301+
modsec_transaction.addResponseHeader(name.c_str(),
302+
value.c_str());
303303
}
304304

305305
modsec_transaction.processResponseHeaders(r.status,
@@ -314,7 +314,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
314314

315315
modsec_transaction.processLogging();
316316

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

319319
if (!d->contains(t->debug_log)) {
320320
if (test->m_automake_output) {
@@ -455,8 +455,8 @@ int main(int argc, char **argv)
455455
int counter = 0;
456456

457457
std::list<std::string> keyList;
458-
for (const auto &a : test) {
459-
keyList.push_back(a.first);
458+
for (const auto &[name, tests] : test) {
459+
keyList.push_back(name);
460460
}
461461
keyList.sort();
462462

0 commit comments

Comments
 (0)