Skip to content

Commit f9f4011

Browse files
authored
Merge pull request #3434 from airween/v3/cppcheckfix2
chore: fix cppcheck warning
2 parents 0ac551b + 62cb73f commit f9f4011

File tree

7 files changed

+50
-39
lines changed

7 files changed

+50
-39
lines changed

src/operators/fuzzy_hash.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bool FuzzyHash::init(const std::string &param2, std::string *error) {
2727
#ifdef WITH_SSDEEP
2828
std::string digit;
2929
std::string file;
30-
std::istream *iss;
30+
std::ifstream *iss;
3131
std::shared_ptr<fuzzy_hash_chunk> chunk, t;
3232
std::string err;
3333

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

51-
if (((std::ifstream *)iss)->is_open() == false) {
51+
if (iss->is_open() == false) {
5252
error->assign("Failed to open file: " + m_param + ". " + err);
5353
delete iss;
5454
return false;

src/operators/inspect_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ namespace modsecurity {
3131
namespace operators {
3232

3333
bool InspectFile::init(const std::string &param2, std::string *error) {
34-
std::istream *iss;
34+
std::ifstream *iss;
3535
std::string err;
3636
std::string err_lua;
3737

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

41-
if (((std::ifstream *)iss)->is_open() == false) {
41+
if (iss->is_open() == false) {
4242
error->assign("Failed to open file: " + m_param + ". " + err);
4343
delete iss;
4444
return false;

src/operators/pm_from_file.cc

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,34 @@ bool PmFromFile::init(const std::string &config, std::string *error) {
4949
std::vector<std::string> tokens = split(m_param, ' ');
5050

5151
for (const auto& token : tokens) {
52-
if (! token.empty()) {
53-
54-
std::istream *iss;
52+
if (token.empty()) {
53+
continue;
54+
}
5555

56-
if (token.compare(0, 8, "https://") == 0) {
57-
Utils::HttpsClient client;
58-
bool ret = client.download(token);
59-
if (ret == false) {
60-
error->assign(client.error);
61-
return false;
62-
}
63-
iss = new std::stringstream(client.content);
64-
} else {
65-
std::string err;
66-
std::string resource = utils::find_resource(token, config, &err);
67-
iss = new std::ifstream(resource, std::ios::in);
56+
std::unique_ptr<std::istream> iss;
6857

69-
if (((std::ifstream *)iss)->is_open() == false) {
70-
error->assign("Failed to open file: '" + token + "'. " + err);
71-
delete iss;
72-
return false;
73-
}
58+
if (token.compare(0, 8, "https://") == 0) {
59+
Utils::HttpsClient client;
60+
bool ret = client.download(token);
61+
if (ret == false) {
62+
error->assign(client.error);
63+
return false;
7464
}
75-
76-
for (std::string line; std::getline(*iss, line); ) {
77-
if (isComment(line) == false) {
78-
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
79-
}
65+
iss = std::make_unique<std::stringstream>(client.content);
66+
} else {
67+
std::string err;
68+
std::string resource = utils::find_resource(token, config, &err);
69+
auto file = std::make_unique<std::ifstream>(resource, std::ios::in);
70+
if (file->is_open() == false) {
71+
error->assign("Failed to open file: '" + token + "'. " + err);
72+
return false;
73+
}
74+
iss = std::move(file);
75+
}
76+
for (std::string line; std::getline(*iss, line); ) {
77+
if (isComment(line) == false) {
78+
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
8079
}
81-
82-
delete iss;
8380
}
8481
}
8582

src/operators/rbl.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,20 @@ bool Rbl::evaluate(Transaction *t, RuleWithActions *rule,
226226
return false;
227227
}
228228

229-
struct sockaddr *addr = info->ai_addr;
230-
struct sockaddr_in *sin = (struct sockaddr_in *) addr;
231-
furtherInfo(sin, ipStr, t, m_provider);
229+
// SonarCloud suggested to use the init-statement to declare "addr" inside the if statement.
230+
// I think that's not good here, because we need that in the else block
231+
const struct sockaddr *addr = info->ai_addr; // NOSONAR
232+
if (addr->sa_family == AF_INET) { // NOSONAR
233+
struct sockaddr_in sin{}; // initialize an empty struct; we don't need port info
234+
memcpy(&sin.sin_addr, addr->sa_data + 2, sizeof(sin.sin_addr));
235+
sin.sin_family = AF_INET;
236+
furtherInfo(&sin, ipStr, t, m_provider);
237+
}
238+
else {
239+
ms_dbg_a(t, 7, "Unsupported address family: " + std::to_string(addr->sa_family));
240+
freeaddrinfo(info);
241+
return false;
242+
}
232243

233244
freeaddrinfo(info);
234245
if (rule && t && rule->hasCaptureAction()) {

src/operators/validate_dtd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool ValidateDTD::init(const std::string &file, std::string *error) {
4545

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

48-
XmlDtdPtrManager dtd(xmlParseDTD(NULL, (const xmlChar *)m_resource.c_str()));
48+
XmlDtdPtrManager dtd(xmlParseDTD(NULL, reinterpret_cast<const xmlChar *>(m_resource.c_str())));
4949
if (dtd.get() == NULL) {
5050
std::string err = std::string("XML: Failed to load DTD: ") \
5151
+ m_resource;

src/variables/xml.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void XML::evaluate(Transaction *t,
7979
}
8080

8181
/* Process the XPath expression. */
82-
xpathExpr = (const xmlChar*)param.c_str();
82+
xpathExpr = reinterpret_cast<const xmlChar*>(param.c_str());
8383
xpathCtx = xmlXPathNewContext(t->m_xml->m_data.doc);
8484
if (xpathCtx == NULL) {
8585
ms_dbg_a(t, 1, "XML: Unable to create new XPath context. : ");
@@ -91,9 +91,9 @@ void XML::evaluate(Transaction *t,
9191
} else {
9292
std::vector<actions::Action *> acts = rule->getActionsByName("xmlns", t);
9393
for (auto &x : acts) {
94-
actions::XmlNS *z = (actions::XmlNS *)x;
95-
if (xmlXPathRegisterNs(xpathCtx, (const xmlChar*)z->m_scope.c_str(),
96-
(const xmlChar*)z->m_href.c_str()) != 0) {
94+
actions::XmlNS *z = static_cast<actions::XmlNS *>(x);
95+
if (xmlXPathRegisterNs(xpathCtx, reinterpret_cast<const xmlChar*>(z->m_scope.c_str()),
96+
reinterpret_cast<const xmlChar*>(z->m_href.c_str())) != 0) {
9797
ms_dbg_a(t, 1, "Failed to register XML namespace href \"" + \
9898
z->m_href + "\" prefix \"" + z->m_scope + "\".");
9999
return;

test/cppcheck_suppressions.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ accessMoved:seclang-parser.hh
3131
returnTempReference:seclang-parser.hh
3232
duplInheritedMember:seclang-parser.hh
3333
constVariableReference:seclang-parser.hh
34+
uninitMemberVar:seclang-parser.hh
35+
3436

3537
unreadVariable:src/operators/rx.cc
3638
unreadVariable:src/operators/rx_global.cc
@@ -59,3 +61,4 @@ uselessCallsSubstr
5961

6062
// Examples
6163
memleak:examples/using_bodies_in_chunks/simple_request.cc
64+

0 commit comments

Comments
 (0)