Skip to content

chore: fix cppcheck warning #3434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: v3/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/operators/fuzzy_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
#ifdef WITH_SSDEEP
std::string digit;
std::string file;
std::istream *iss;
std::ifstream *iss;
std::shared_ptr<fuzzy_hash_chunk> chunk, t;
std::string err;

Expand All @@ -48,7 +48,7 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
std::string resource = utils::find_resource(file, param2, &err);
iss = new std::ifstream(resource, std::ios::in);

if (((std::ifstream *)iss)->is_open() == false) {
if (iss->is_open() == false) {
error->assign("Failed to open file: " + m_param + ". " + err);
delete iss;
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/operators/inspect_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ namespace modsecurity {
namespace operators {

bool InspectFile::init(const std::string &param2, std::string *error) {
std::istream *iss;
std::ifstream *iss;
std::string err;
std::string err_lua;

m_file = utils::find_resource(m_param, param2, &err);
iss = new std::ifstream(m_file, std::ios::in);

if (((std::ifstream *)iss)->is_open() == false) {
if (iss->is_open() == false) {
error->assign("Failed to open file: " + m_param + ". " + err);
delete iss;
return false;
Expand Down
51 changes: 24 additions & 27 deletions src/operators/pm_from_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,34 @@ bool PmFromFile::init(const std::string &config, std::string *error) {
std::vector<std::string> tokens = split(m_param, ' ');

for (const auto& token : tokens) {
if (! token.empty()) {

std::istream *iss;
if (token.empty()) {
continue;
}

if (token.compare(0, 8, "https://") == 0) {
Utils::HttpsClient client;
bool ret = client.download(token);
if (ret == false) {
error->assign(client.error);
return false;
}
iss = new std::stringstream(client.content);
} else {
std::string err;
std::string resource = utils::find_resource(token, config, &err);
iss = new std::ifstream(resource, std::ios::in);
std::unique_ptr<std::istream> iss;

if (((std::ifstream *)iss)->is_open() == false) {
error->assign("Failed to open file: '" + token + "'. " + err);
delete iss;
return false;
}
if (token.compare(0, 8, "https://") == 0) {
Utils::HttpsClient client;
bool ret = client.download(token);
if (ret == false) {
error->assign(client.error);
return false;
}

for (std::string line; std::getline(*iss, line); ) {
if (isComment(line) == false) {
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
}
iss = std::make_unique<std::stringstream>(client.content);
} else {
std::string err;
std::string resource = utils::find_resource(token, config, &err);
auto file = std::make_unique<std::ifstream>(resource, std::ios::in);
if (file->is_open() == false) {
error->assign("Failed to open file: '" + token + "'. " + err);
return false;
}
iss = std::move(file);
}
for (std::string line; std::getline(*iss, line); ) {
if (isComment(line) == false) {
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
}

delete iss;
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/operators/rbl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,20 @@ bool Rbl::evaluate(Transaction *t, RuleWithActions *rule,
return false;
}

// NOSONAR
// SonarCloud suggested to use the init-statement to declare "addr" inside the if statement.
// I think that's not good here, because we need that in the else block
struct sockaddr *addr = info->ai_addr;
struct sockaddr_in *sin = (struct sockaddr_in *) addr;
furtherInfo(sin, ipStr, t, m_provider);
// NOSONAR
if (addr->sa_family == AF_INET) { // only IPv4 address is allowed
auto sin = (struct sockaddr_in *) addr; // cppcheck-suppress[dangerousTypeCast]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you use a C++ cast here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I can't show the relevant SonarQube issue, because it shows only the current state (last analysis). As you mentioned there was a C++ type converter there: reinterpret_cast<struct sockaddr_in *>(addr); - but SonarQube reported this cast can lead to an undefined behavior. So I could choose from two options: suppress cppcheck warning or disable SonarQube check. I chose to suppress cppcheck warning 😃

Note, that here we can't use either dynamic_cast<> or static_cast<> because they make cast between derived classes, but these types here are C structs.

furtherInfo(sin, ipStr, t, m_provider);
}
else {
ms_dbg_a(t, 7, "Unsupported address family: " + std::to_string(addr->sa_family));
freeaddrinfo(info);
return false;
}

freeaddrinfo(info);
if (rule && t && rule->hasCaptureAction()) {
Expand Down
2 changes: 1 addition & 1 deletion src/operators/validate_dtd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool ValidateDTD::init(const std::string &file, std::string *error) {

bool ValidateDTD::evaluate(Transaction *transaction, const std::string &str) {

XmlDtdPtrManager dtd(xmlParseDTD(NULL, (const xmlChar *)m_resource.c_str()));
XmlDtdPtrManager dtd(xmlParseDTD(NULL, reinterpret_cast<const xmlChar *>(m_resource.c_str())));
if (dtd.get() == NULL) {
std::string err = std::string("XML: Failed to load DTD: ") \
+ m_resource;
Expand Down
8 changes: 4 additions & 4 deletions src/variables/xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void XML::evaluate(Transaction *t,
}

/* Process the XPath expression. */
xpathExpr = (const xmlChar*)param.c_str();
xpathExpr = reinterpret_cast<const xmlChar*>(param.c_str());
xpathCtx = xmlXPathNewContext(t->m_xml->m_data.doc);
if (xpathCtx == NULL) {
ms_dbg_a(t, 1, "XML: Unable to create new XPath context. : ");
Expand All @@ -91,9 +91,9 @@ void XML::evaluate(Transaction *t,
} else {
std::vector<actions::Action *> acts = rule->getActionsByName("xmlns", t);
for (auto &x : acts) {
actions::XmlNS *z = (actions::XmlNS *)x;
if (xmlXPathRegisterNs(xpathCtx, (const xmlChar*)z->m_scope.c_str(),
(const xmlChar*)z->m_href.c_str()) != 0) {
actions::XmlNS *z = static_cast<actions::XmlNS *>(x);
if (xmlXPathRegisterNs(xpathCtx, reinterpret_cast<const xmlChar*>(z->m_scope.c_str()),
reinterpret_cast<const xmlChar*>(z->m_href.c_str())) != 0) {
ms_dbg_a(t, 1, "Failed to register XML namespace href \"" + \
z->m_href + "\" prefix \"" + z->m_scope + "\".");
return;
Expand Down
3 changes: 3 additions & 0 deletions test/cppcheck_suppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ accessMoved:seclang-parser.hh
returnTempReference:seclang-parser.hh
duplInheritedMember:seclang-parser.hh
constVariableReference:seclang-parser.hh
uninitMemberVar:seclang-parser.hh


unreadVariable:src/operators/rx.cc
unreadVariable:src/operators/rx_global.cc
Expand Down Expand Up @@ -59,3 +61,4 @@ uselessCallsSubstr

// Examples
memleak:examples/using_bodies_in_chunks/simple_request.cc

Loading