mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-17 01:36:08 +03:00
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.
This commit is contained in:
parent
4df297b596
commit
b7b2d9a40d
@ -199,13 +199,13 @@ void ModSecurity::serverLog(void *data, const RuleMessage &rm) {
|
|||||||
|
|
||||||
if (m_logProperties & TextLogProperty) {
|
if (m_logProperties & TextLogProperty) {
|
||||||
auto d = rm.log();
|
auto d = rm.log();
|
||||||
const void *a = static_cast<const void *>(d.c_str());
|
auto a = static_cast<const void *>(d.c_str());
|
||||||
m_logCb(data, a);
|
m_logCb(data, a);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_logProperties & RuleMessageLogProperty) {
|
if (m_logProperties & RuleMessageLogProperty) {
|
||||||
const void *a = static_cast<const void *>(&rm);
|
auto a = static_cast<const void *>(&rm);
|
||||||
m_logCb(data, a);
|
m_logCb(data, a);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1529,7 +1529,7 @@ std::string Transaction::toOldAuditLogFormat(int parts,
|
|||||||
}
|
}
|
||||||
if (parts & audit_log::AuditLog::HAuditLogPart) {
|
if (parts & audit_log::AuditLog::HAuditLogPart) {
|
||||||
audit_log << "--" << trailer << "-" << "H--" << std::endl;
|
audit_log << "--" << trailer << "-" << "H--" << std::endl;
|
||||||
for (auto a : m_rulesMessages) {
|
for (const auto &a : m_rulesMessages) {
|
||||||
audit_log << a.log(0, m_httpCodeReturned) << std::endl;
|
audit_log << a.log(0, m_httpCodeReturned) << std::endl;
|
||||||
}
|
}
|
||||||
audit_log << std::endl;
|
audit_log << std::endl;
|
||||||
|
@ -23,8 +23,6 @@
|
|||||||
|
|
||||||
namespace modsecurity_test {
|
namespace modsecurity_test {
|
||||||
|
|
||||||
CustomDebugLog::~CustomDebugLog() {}
|
|
||||||
|
|
||||||
void CustomDebugLog::write(int level, const std::string &message) {
|
void CustomDebugLog::write(int level, const std::string &message) {
|
||||||
m_log << "[" << level << "] " << message << std::endl;
|
m_log << "[" << level << "] " << message << std::endl;
|
||||||
}
|
}
|
||||||
@ -36,13 +34,13 @@ namespace modsecurity_test {
|
|||||||
m_log << msgf << std::endl;
|
m_log << msgf << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool const CustomDebugLog::contains(const std::string &pattern) const {
|
bool CustomDebugLog::contains(const std::string &pattern) const {
|
||||||
modsecurity::Utils::Regex re(pattern);
|
modsecurity::Utils::Regex re(pattern);
|
||||||
std::string s = m_log.str();
|
std::string s = m_log.str();
|
||||||
return modsecurity::Utils::regex_search(s, re);
|
return modsecurity::Utils::regex_search(s, re);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string const CustomDebugLog::log_messages() const {
|
std::string CustomDebugLog::log_messages() const {
|
||||||
return m_log.str();
|
return m_log.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,13 +26,12 @@ namespace modsecurity_test {
|
|||||||
class CustomDebugLog : public modsecurity::debug_log::DebugLog {
|
class CustomDebugLog : public modsecurity::debug_log::DebugLog {
|
||||||
public:
|
public:
|
||||||
CustomDebugLog *new_instance();
|
CustomDebugLog *new_instance();
|
||||||
~CustomDebugLog();
|
|
||||||
|
|
||||||
void write(int level, const std::string& message) override;
|
void write(int level, const std::string& message) override;
|
||||||
void write(int level, const std::string &id,
|
void write(int level, const std::string &id,
|
||||||
const std::string &uri, const std::string &msg) override;
|
const std::string &uri, const std::string &msg) override;
|
||||||
bool const contains(const std::string& pattern) const;
|
bool contains(const std::string& pattern) const;
|
||||||
std::string const log_messages() const;
|
std::string log_messages() const;
|
||||||
std::string error_log_messages();
|
std::string error_log_messages();
|
||||||
int getDebugLogLevel() override;
|
int getDebugLogLevel() override;
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ namespace modsecurity_test {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static void logCb(void *data, const void *msgv) {
|
static void logCb(void *data, const void *msgv) {
|
||||||
const char *msg = reinterpret_cast<const char *>(msgv);
|
auto msg = reinterpret_cast<const char *>(msgv);
|
||||||
std::stringstream *ss = (std::stringstream *)data;
|
auto ss = reinterpret_cast<std::stringstream *>(data);
|
||||||
*ss << msg << std::endl;
|
*ss << msg << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -97,15 +97,15 @@ void actions(ModSecurityTestResults<RegressionTest> *r,
|
|||||||
if (it.status != 0) {
|
if (it.status != 0) {
|
||||||
r->status = it.status;
|
r->status = it.status;
|
||||||
}
|
}
|
||||||
if (it.url != NULL) {
|
if (it.url != nullptr) {
|
||||||
r->location.append(it.url);
|
r->location.append(it.url);
|
||||||
free(it.url);
|
free(it.url);
|
||||||
it.url = NULL;
|
it.url = nullptr;
|
||||||
}
|
}
|
||||||
if (it.log != NULL) {
|
if (it.log != nullptr) {
|
||||||
*serverLog << it.log;
|
*serverLog << it.log;
|
||||||
free(it.log);
|
free(it.log);
|
||||||
it.log = NULL;
|
it.log = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -283,9 +283,9 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
|
|||||||
|
|
||||||
actions(&r, &modsec_transaction, &context.m_server_log);
|
actions(&r, &modsec_transaction, &context.m_server_log);
|
||||||
|
|
||||||
for (const auto &headers : t->request_headers) {
|
for (const auto &[name, value] : t->request_headers) {
|
||||||
modsec_transaction.addRequestHeader(headers.first.c_str(),
|
modsec_transaction.addRequestHeader(name.c_str(),
|
||||||
headers.second.c_str());
|
value.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
modsec_transaction.processRequestHeaders();
|
modsec_transaction.processRequestHeaders();
|
||||||
@ -297,9 +297,9 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
|
|||||||
modsec_transaction.processRequestBody();
|
modsec_transaction.processRequestBody();
|
||||||
actions(&r, &modsec_transaction, &context.m_server_log);
|
actions(&r, &modsec_transaction, &context.m_server_log);
|
||||||
|
|
||||||
for (const auto &headers : t->response_headers) {
|
for (const auto &[name, value] : t->response_headers) {
|
||||||
modsec_transaction.addResponseHeader(headers.first.c_str(),
|
modsec_transaction.addResponseHeader(name.c_str(),
|
||||||
headers.second.c_str());
|
value.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
modsec_transaction.processResponseHeaders(r.status,
|
modsec_transaction.processResponseHeaders(r.status,
|
||||||
@ -314,7 +314,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
|
|||||||
|
|
||||||
modsec_transaction.processLogging();
|
modsec_transaction.processLogging();
|
||||||
|
|
||||||
const auto *d = reinterpret_cast<CustomDebugLog *>(context.m_modsec_rules.m_debugLog);
|
const auto *d = static_cast<CustomDebugLog *>(context.m_modsec_rules.m_debugLog);
|
||||||
|
|
||||||
if (!d->contains(t->debug_log)) {
|
if (!d->contains(t->debug_log)) {
|
||||||
if (test->m_automake_output) {
|
if (test->m_automake_output) {
|
||||||
@ -455,8 +455,8 @@ int main(int argc, char **argv)
|
|||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|
||||||
std::list<std::string> keyList;
|
std::list<std::string> keyList;
|
||||||
for (const auto &a : test) {
|
for (const auto &[name, tests] : test) {
|
||||||
keyList.push_back(a.first);
|
keyList.push_back(name);
|
||||||
}
|
}
|
||||||
keyList.sort();
|
keyList.sort();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user