Added const reported by cppcheck 2.14

This commit is contained in:
Eduardo Arias 2024-04-28 21:23:43 -03:00 committed by Eduardo Arias
parent d053ec6de6
commit bbef22b3b5
46 changed files with 110 additions and 112 deletions

View File

@ -40,7 +40,7 @@ int main (int argc, char *argv[]) {
modsec->setConnectorInformation("ModSecurity-test v0.0.1-alpha (Simple " \ modsec->setConnectorInformation("ModSecurity-test v0.0.1-alpha (Simple " \
"example on how to use ModSecurity API"); "example on how to use ModSecurity API");
char main_rule_uri[] = "basic_rules.conf"; const char main_rule_uri[] = "basic_rules.conf";
auto rules = std::make_unique<modsecurity::RulesSet>(); auto rules = std::make_unique<modsecurity::RulesSet>();
if (rules->loadFromUri(main_rule_uri) < 0) { if (rules->loadFromUri(main_rule_uri) < 0) {
std::cerr << "Problems loading the rules..." << std::endl; std::cerr << "Problems loading the rules..." << std::endl;

View File

@ -42,7 +42,7 @@ class AnchoredSetVariableTranslationProxy {
: m_name(name), : m_name(name),
m_fount(fount) m_fount(fount)
{ {
m_translate = [](std::string *name, std::vector<const VariableValue *> *l) { m_translate = [](const std::string *name, std::vector<const VariableValue *> *l) {
for (int i = 0; i < l->size(); ++i) { for (int i = 0; i < l->size(); ++i) {
VariableValue *newVariableValue = new VariableValue(name, &l->at(i)->getKey(), &l->at(i)->getKey()); VariableValue *newVariableValue = new VariableValue(name, &l->at(i)->getKey(), &l->at(i)->getKey());
const VariableValue *oldVariableValue = l->at(i); const VariableValue *oldVariableValue = l->at(i);

View File

@ -79,7 +79,7 @@ class RuleWithActions : public Rule {
bool chainedParentNull = false) const; bool chainedParentNull = false) const;
std::vector<actions::Action *> getActionsByName(const std::string& name, std::vector<actions::Action *> getActionsByName(const std::string& name,
Transaction *t); const Transaction *t);
bool containsTag(const std::string& name, Transaction *t); bool containsTag(const std::string& name, Transaction *t);
bool containsMsg(const std::string& name, Transaction *t); bool containsMsg(const std::string& name, Transaction *t);

View File

@ -62,7 +62,7 @@ class RuleWithOperator : public RuleWithActions {
static void cleanMatchedVars(Transaction *trasn); static void cleanMatchedVars(Transaction *trasn);
std::string getOperatorName() const; const std::string& getOperatorName() const;
virtual std::string getReference() override { virtual std::string getReference() override {
return std::to_string(m_ruleId); return std::to_string(m_ruleId);

View File

@ -50,7 +50,7 @@ class Rules {
int append(Rules *from, const std::vector<int64_t> &ids, std::ostringstream *err) { int append(Rules *from, const std::vector<int64_t> &ids, std::ostringstream *err) {
size_t j = 0; size_t j = 0;
for (; j < from->size(); j++) { for (; j < from->size(); j++) {
RuleWithOperator *rule = dynamic_cast<RuleWithOperator *>(from->at(j).get()); const RuleWithOperator *rule = dynamic_cast<RuleWithOperator *>(from->at(j).get());
if (rule && std::binary_search(ids.begin(), ids.end(), rule->m_ruleId)) { if (rule && std::binary_search(ids.begin(), ids.end(), rule->m_ruleId)) {
if (err != NULL) { if (err != NULL) {
*err << "Rule id: " << std::to_string(rule->m_ruleId) \ *err << "Rule id: " << std::to_string(rule->m_ruleId) \
@ -68,7 +68,7 @@ class Rules {
} }
bool insert(std::shared_ptr<Rule> rule, const std::vector<int64_t> *ids, std::ostringstream *err) { bool insert(std::shared_ptr<Rule> rule, const std::vector<int64_t> *ids, std::ostringstream *err) {
RuleWithOperator *r = dynamic_cast<RuleWithOperator *>(rule.get()); const RuleWithOperator *r = dynamic_cast<RuleWithOperator *>(rule.get());
if (r && ids != nullptr && std::binary_search(ids->begin(), ids->end(), r->m_ruleId)) { if (r && ids != nullptr && std::binary_search(ids->begin(), ids->end(), r->m_ruleId)) {
if (err != nullptr) { if (err != nullptr) {
*err << "Rule id: " << std::to_string(r->m_ruleId) \ *err << "Rule id: " << std::to_string(r->m_ruleId) \

View File

@ -93,7 +93,7 @@ extern "C" {
#endif #endif
RulesSet *msc_create_rules_set(void); RulesSet *msc_create_rules_set(void);
void msc_rules_dump(RulesSet *rules); void msc_rules_dump(const RulesSet *rules);
int msc_rules_merge(RulesSet *rules_dst, RulesSet *rules_from, const char **error); int msc_rules_merge(RulesSet *rules_dst, RulesSet *rules_from, const char **error);
int msc_rules_add_remote(RulesSet *rules, const char *key, const char *uri, int msc_rules_add_remote(RulesSet *rules, const char *key, const char *uri,
const char **error); const char **error);

View File

@ -70,7 +70,7 @@ class ConfigInt {
bool m_set; bool m_set;
int m_value; int m_value;
void merge(ConfigInt *from) { void merge(const ConfigInt *from) {
if (m_set == true || from->m_set == false) { if (m_set == true || from->m_set == false) {
return; return;
} }
@ -87,7 +87,7 @@ class ConfigDouble {
bool m_set; bool m_set;
double m_value; double m_value;
void merge(ConfigDouble *from) { void merge(const ConfigDouble *from) {
if (m_set == true || from->m_set == false) { if (m_set == true || from->m_set == false) {
return; return;
} }
@ -104,7 +104,7 @@ class ConfigString {
bool m_set; bool m_set;
std::string m_value; std::string m_value;
void merge(ConfigString *from) { void merge(const ConfigString *from) {
if (m_set == true || from->m_set == false) { if (m_set == true || from->m_set == false) {
return; return;
} }
@ -150,7 +150,7 @@ class ConfigUnicodeMap {
static void loadConfig(std::string f, double codePage, static void loadConfig(std::string f, double codePage,
RulesSetProperties *driver, std::string *errg); RulesSetProperties *driver, std::string *errg);
void merge(ConfigUnicodeMap *from) { void merge(const ConfigUnicodeMap *from) {
if (from->m_set == false) { if (from->m_set == false) {
return; return;
} }

View File

@ -713,7 +713,7 @@ int msc_process_uri(Transaction *transaction, const char *uri,
const char *protocol, const char *http_version); const char *protocol, const char *http_version);
/** @ingroup ModSecurity_C_API */ /** @ingroup ModSecurity_C_API */
const char *msc_get_response_body(Transaction *transaction); const char *msc_get_response_body(const Transaction *transaction);
/** @ingroup ModSecurity_C_API */ /** @ingroup ModSecurity_C_API */
size_t msc_get_response_body_length(Transaction *transaction); size_t msc_get_response_body_length(Transaction *transaction);

View File

@ -84,10 +84,10 @@ bool RuleRemoveById::init(std::string *error) {
} }
bool RuleRemoveById::evaluate(RuleWithActions *rule, Transaction *transaction) { bool RuleRemoveById::evaluate(RuleWithActions *rule, Transaction *transaction) {
for (auto &i : m_ids) { for (const auto &i : m_ids) {
transaction->m_ruleRemoveById.push_back(i); transaction->m_ruleRemoveById.push_back(i);
} }
for (auto &i : m_ranges) { for (const auto &i : m_ranges) {
transaction->m_ruleRemoveByIdRange.push_back(i); transaction->m_ruleRemoveByIdRange.push_back(i);
} }

View File

@ -26,7 +26,7 @@ static inline int inplace(std::string &value) {
const auto len = value.length(); const auto len = value.length();
auto d = reinterpret_cast<unsigned char *>(value.data()); auto d = reinterpret_cast<unsigned char *>(value.data());
const auto data = d; const auto *data = d;
for (int i = 0; i <= len - 2; i += 2) { for (int i = 0; i <= len - 2; i += 2) {
*d++ = utils::string::x2c(&data[i]); *d++ = utils::string::x2c(&data[i]);

View File

@ -118,7 +118,7 @@ static inline bool inplace(std::string &value) {
j++; j++;
} }
if (j > k) { /* Do we have at least one digit? */ if (j > k) { /* Do we have at least one digit? */
const auto x = reinterpret_cast<const char*>(&input[k]); const auto *x = reinterpret_cast<const char*>(&input[k]);
/* Decode the entity. */ /* Decode the entity. */
/* ENH What about others? */ /* ENH What about others? */

View File

@ -29,9 +29,6 @@ bool NormalisePath::transform(std::string &value, const Transaction *trans) cons
* IMP1 Assumes NUL-terminated * IMP1 Assumes NUL-terminated
*/ */
bool NormalisePath::normalize_path_inplace(std::string &val, const bool win) { bool NormalisePath::normalize_path_inplace(std::string &val, const bool win) {
unsigned char *src;
unsigned char *dst;
unsigned char *end;
int hitroot = 0; int hitroot = 0;
int done = 0; int done = 0;
int relative; int relative;
@ -49,13 +46,13 @@ bool NormalisePath::normalize_path_inplace(std::string &val, const bool win) {
* ENH: Deal with UNC and drive letters? * ENH: Deal with UNC and drive letters?
*/ */
src = dst = input; auto src = input;
end = input + (input_len - 1); auto dst = input;
const auto *end = input + (input_len - 1);
relative = ((*input == '/') || (win && (*input == '\\'))) ? 0 : 1; relative = ((*input == '/') || (win && (*input == '\\'))) ? 0 : 1;
trailing = ((*end == '/') || (win && (*end == '\\'))) ? 1 : 0; trailing = ((*end == '/') || (win && (*end == '\\'))) ? 1 : 0;
while (!done && (src <= end) && (dst <= end)) { while (!done && (src <= end) && (dst <= end)) {
/* Convert backslash to forward slash on Windows only. */ /* Convert backslash to forward slash on Windows only. */
if (win) { if (win) {
@ -152,7 +149,7 @@ copy:
/* Skip to the last forward slash when multiple are used. */ /* Skip to the last forward slash when multiple are used. */
if (*src == '/') { if (*src == '/') {
unsigned char *oldsrc = src; const unsigned char *oldsrc = src;
while ((src < end) while ((src < end)
&& ((*(src + 1) == '/') || (win && (*(src + 1) == '\\'))) ) { && ((*(src + 1) == '/') || (win && (*(src + 1) == '\\'))) ) {

View File

@ -38,7 +38,7 @@ static inline bool inplace(std::string &value) {
auto d = reinterpret_cast<unsigned char*>(value.data()); auto d = reinterpret_cast<unsigned char*>(value.data());
const unsigned char *data = d; const unsigned char *data = d;
const auto end = data + value.size(); const auto *end = data + value.size();
bool changed = false; bool changed = false;

View File

@ -559,7 +559,7 @@ void LMDB::resolveMultiMatches(const std::string& var,
continue; continue;
} }
char *a = reinterpret_cast<char *>(key.mv_data); const char *a = reinterpret_cast<char *>(key.mv_data);
if (strncmp(var.c_str(), a, keySize) == 0) { if (strncmp(var.c_str(), a, keySize) == 0) {
std::string key_to_insert(reinterpret_cast<char *>(key.mv_data), key.mv_size); std::string key_to_insert(reinterpret_cast<char *>(key.mv_data), key.mv_size);
l->insert(l->begin(), new VariableValue(&m_name, &key_to_insert, &collectionData.getValue())); l->insert(l->begin(), new VariableValue(&m_name, &key_to_insert, &collectionData.getValue()));

View File

@ -114,14 +114,14 @@ int Lua::blob_keeper(lua_State *L, const void *p, size_t sz, void *ud) {
const char *Lua::blob_reader(lua_State *L, void *ud, size_t *size) { const char *Lua::blob_reader(lua_State *L, void *ud, size_t *size) {
LuaScriptBlob *lsb = static_cast<LuaScriptBlob *>(ud); const LuaScriptBlob *lsb = static_cast<LuaScriptBlob *>(ud);
const char *data = lsb->read(size); const char *data = lsb->read(size);
return data; return data;
} }
#endif #endif
int Lua::run(Transaction *t, const std::string &str) { int Lua::run(Transaction *t, const std::string &str) { // cppcheck-suppress constParameterPointer
#ifdef WITH_LUA #ifdef WITH_LUA
std::string luaRet; std::string luaRet;
const char *a = NULL; const char *a = NULL;

View File

@ -33,7 +33,7 @@ class GeoLookup : public Operator {
protected: protected:
// cppcheck-suppress functionStatic // cppcheck-suppress functionStatic
bool debug(Transaction *transaction, int x, const std::string &a) { bool debug(const Transaction *transaction, int x, const std::string &a) {
ms_dbg_a(transaction, x, a); ms_dbg_a(transaction, x, a);
return true; return true;
} }

View File

@ -136,7 +136,7 @@ class Operator {
std::string m_match_message; std::string m_match_message;
bool m_negation; bool m_negation;
std::string m_op; const std::string m_op;
std::string m_param; std::string m_param;
std::unique_ptr<RunTimeString> m_string; std::unique_ptr<RunTimeString> m_string;
bool m_couldContainsMacro; bool m_couldContainsMacro;

View File

@ -73,8 +73,8 @@ std::string Rbl::mapIpToAddress(const std::string &ipStr, Transaction *trans) co
void Rbl::futherInfo_httpbl(struct sockaddr_in *sin, const std::string &ipStr, void Rbl::futherInfo_httpbl(struct sockaddr_in *sin, const std::string &ipStr,
Transaction *trans) { const Transaction *trans) {
char *respBl; const char *respBl;
int first, days, score, type; int first, days, score, type;
#ifndef NO_LOGS #ifndef NO_LOGS
std::string ptype; std::string ptype;
@ -131,7 +131,7 @@ void Rbl::futherInfo_httpbl(struct sockaddr_in *sin, const std::string &ipStr,
void Rbl::futherInfo_spamhaus(unsigned int high8bits, const std::string &ipStr, void Rbl::futherInfo_spamhaus(unsigned int high8bits, const std::string &ipStr,
Transaction *trans) { const Transaction *trans) {
switch (high8bits) { switch (high8bits) {
case 2: case 2:
case 3: case 3:
@ -158,7 +158,7 @@ void Rbl::futherInfo_spamhaus(unsigned int high8bits, const std::string &ipStr,
void Rbl::futherInfo_uribl(unsigned int high8bits, const std::string &ipStr, void Rbl::futherInfo_uribl(unsigned int high8bits, const std::string &ipStr,
Transaction *trans) { const Transaction *trans) {
switch (high8bits) { switch (high8bits) {
case 2: case 2:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded (BLACK)."); ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded (BLACK).");
@ -185,7 +185,7 @@ void Rbl::futherInfo_uribl(unsigned int high8bits, const std::string &ipStr,
void Rbl::furtherInfo(struct sockaddr_in *sin, const std::string &ipStr, void Rbl::furtherInfo(struct sockaddr_in *sin, const std::string &ipStr,
Transaction *trans, RblProvider provider) { const Transaction *trans, RblProvider provider) {
unsigned int high8bits = sin->sin_addr.s_addr >> 24; unsigned int high8bits = sin->sin_addr.s_addr >> 24;
switch (provider) { switch (provider) {

View File

@ -88,13 +88,13 @@ class Rbl : public Operator {
std::string mapIpToAddress(const std::string &ipStr, Transaction *trans) const; std::string mapIpToAddress(const std::string &ipStr, Transaction *trans) const;
static void futherInfo_httpbl(struct sockaddr_in *sin, const std::string &ipStr, static void futherInfo_httpbl(struct sockaddr_in *sin, const std::string &ipStr,
Transaction *trans); const Transaction *trans);
static void futherInfo_spamhaus(unsigned int high8bits, const std::string &ipStr, static void futherInfo_spamhaus(unsigned int high8bits, const std::string &ipStr,
Transaction *trans); const Transaction *trans);
static void futherInfo_uribl(unsigned int high8bits, const std::string &ipStr, static void futherInfo_uribl(unsigned int high8bits, const std::string &ipStr,
Transaction *trans); const Transaction *trans);
static void furtherInfo(struct sockaddr_in *sin, const std::string &ipStr, static void furtherInfo(struct sockaddr_in *sin, const std::string &ipStr,
Transaction *trans, RblProvider provider); const Transaction *trans, RblProvider provider);
private: private:
std::string m_service; std::string m_service;

View File

@ -62,7 +62,7 @@ class ValidateDTD : public Operator {
static void error_runtime(void *ctx, const char *msg, ...) { static void error_runtime(void *ctx, const char *msg, ...) {
Transaction *t = reinterpret_cast<Transaction *>(ctx); const Transaction *t = reinterpret_cast<Transaction *>(ctx);
char buf[1024]; char buf[1024];
std::string s; std::string s;
va_list args; va_list args;
@ -79,7 +79,7 @@ class ValidateDTD : public Operator {
static void warn_runtime(void *ctx, const char *msg, ...) { static void warn_runtime(void *ctx, const char *msg, ...) {
Transaction *t = reinterpret_cast<Transaction *>(ctx); const Transaction *t = reinterpret_cast<Transaction *>(ctx);
char buf[1024]; char buf[1024];
std::string s; std::string s;
va_list args; va_list args;
@ -95,7 +95,7 @@ class ValidateDTD : public Operator {
} }
static void null_error(void *ctx, const char *msg, ...) { static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
} }
private: private:

View File

@ -75,7 +75,7 @@ class ValidateSchema : public Operator {
static void error_runtime(void *ctx, const char *msg, ...) { static void error_runtime(void *ctx, const char *msg, ...) {
Transaction *t = reinterpret_cast<Transaction *>(ctx); const Transaction *t = reinterpret_cast<Transaction *>(ctx);
char buf[1024]; char buf[1024];
std::string s; std::string s;
va_list args; va_list args;
@ -92,7 +92,7 @@ class ValidateSchema : public Operator {
static void warn_runtime(void *ctx, const char *msg, ...) { static void warn_runtime(void *ctx, const char *msg, ...) {
Transaction *t = reinterpret_cast<Transaction *>(ctx); const Transaction *t = reinterpret_cast<Transaction *>(ctx);
char buf[1024]; char buf[1024];
std::string s; std::string s;
va_list args; va_list args;
@ -107,7 +107,7 @@ class ValidateSchema : public Operator {
ms_dbg_a(t, 4, s); ms_dbg_a(t, 4, s);
} }
static void null_error(void *ctx, const char *msg, ...) { static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
} }
private: private:

View File

@ -38,7 +38,7 @@ int VerifyCPF::convert_to_int(const char c) {
} }
bool VerifyCPF::verify(const char *cpfnumber, int len) { bool VerifyCPF::verify(const char *cpfnumber, int len) const {
int factor, part_1, part_2, var_len = len; int factor, part_1, part_2, var_len = len;
unsigned int sum = 0, i = 0, cpf_len = 11, c; unsigned int sum = 0, i = 0, cpf_len = 11, c;
int cpf[11]; int cpf[11];

View File

@ -45,7 +45,7 @@ class VerifyCPF : public Operator {
const std::string& input, const std::string& input,
RuleMessage &ruleMessage) override; RuleMessage &ruleMessage) override;
bool verify(const char *ssnumber, int len); bool verify(const char *ssnumber, int len) const;
private: private:
static int convert_to_int(const char c); static int convert_to_int(const char c);

View File

@ -24,7 +24,7 @@ int VerifySVNR::convert_to_int(const char c) {
} }
bool VerifySVNR::verify(const char *svnrnumber, int len) { bool VerifySVNR::verify(const char *svnrnumber, int len) const {
int var_len = len; int var_len = len;
int sum = 0; int sum = 0;
unsigned int i = 0, svnr_len = 10; unsigned int i = 0, svnr_len = 10;

View File

@ -31,7 +31,7 @@ class VerifySVNR : public Operator {
const std::string& input, const std::string& input,
RuleMessage &ruleMessage) override; RuleMessage &ruleMessage) override;
bool verify(const char *ssnumber, int len); bool verify(const char *ssnumber, int len) const;
private: private:
std::unique_ptr<Regex> m_re; std::unique_ptr<Regex> m_re;

View File

@ -107,9 +107,9 @@ int Driver::addSecRule(std::unique_ptr<RuleWithActions> r) {
} }
for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) { for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
Rules *rules = m_rulesSetPhases[i]; const Rules *rules = m_rulesSetPhases[i];
for (int j = 0; j < rules->size(); j++) { for (int j = 0; j < rules->size(); j++) {
RuleWithOperator *lr = dynamic_cast<RuleWithOperator *>(rules->at(j).get()); const RuleWithOperator *lr = dynamic_cast<RuleWithOperator *>(rules->at(j).get());
if (lr && lr->m_ruleId == rule->m_ruleId) { if (lr && lr->m_ruleId == rule->m_ruleId) {
m_parserError << "Rule id: " << std::to_string(rule->m_ruleId) \ m_parserError << "Rule id: " << std::to_string(rule->m_ruleId) \
<< " is duplicated" << std::endl; << " is duplicated" << std::endl;

View File

@ -131,7 +131,7 @@ int JSON::addArgument(const std::string& value) {
std::string path; std::string path;
for (size_t i = 0; i < m_containers.size(); i++) { for (size_t i = 0; i < m_containers.size(); i++) {
JSONContainerArray *a = dynamic_cast<JSONContainerArray *>( const JSONContainerArray *a = dynamic_cast<JSONContainerArray *>(
m_containers[i]); m_containers[i]);
path = path + m_containers[i]->m_name; path = path + m_containers[i]->m_name;
if (a != NULL) { if (a != NULL) {

View File

@ -79,7 +79,7 @@ class JSON {
static int yajl_end_array(void *ctx); static int yajl_end_array(void *ctx);
bool isPreviousArray() const { bool isPreviousArray() const {
JSONContainerArray *prev = NULL; const JSONContainerArray *prev = NULL;
if (m_containers.size() < 1) { if (m_containers.size() < 1) {
return false; return false;
} }

View File

@ -53,7 +53,7 @@ class XML {
static xmlParserInputBufferPtr unloadExternalEntity(const char *URI, static xmlParserInputBufferPtr unloadExternalEntity(const char *URI,
xmlCharEncoding enc); xmlCharEncoding enc);
static void null_error(void *ctx, const char *msg, ...) { static void null_error(void *ctx, const char *msg, ...) { // cppcheck-suppress[constParameterPointer,constParameterCallback]
} }

View File

@ -363,7 +363,7 @@ void RuleWithActions::executeTransformations(
std::make_shared<std::string>(path)); std::make_shared<std::string>(path));
} }
for (Action *a : m_transformations) { for (const Action *a : m_transformations) {
if (a->m_isNone) { if (a->m_isNone) {
none++; none++;
} }
@ -457,7 +457,7 @@ bool RuleWithActions::containsMsg(const std::string& name, Transaction *t) {
std::vector<actions::Action *> RuleWithActions::getActionsByName(const std::string& name, std::vector<actions::Action *> RuleWithActions::getActionsByName(const std::string& name,
Transaction *trans) { const Transaction *trans) {
std::vector<actions::Action *> ret; std::vector<actions::Action *> ret;
for (auto &z : m_actionsRuntimePos) { for (auto &z : m_actionsRuntimePos) {
if (*z->m_name.get() == name) { if (*z->m_name.get() == name) {

View File

@ -176,7 +176,7 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars,
} }
if (std::find_if(trans->m_ruleRemoveTargetById.begin(), if (std::find_if(trans->m_ruleRemoveTargetById.begin(),
trans->m_ruleRemoveTargetById.end(), trans->m_ruleRemoveTargetById.end(),
[&, variable, this](std::pair<int, std::string> &m) -> bool { [&, variable, this](const auto &m) -> bool {
return m.first == m_ruleId return m.first == m_ruleId
&& m.second == *variable->m_fullName.get(); && m.second == *variable->m_fullName.get();
}) != trans->m_ruleRemoveTargetById.end()) { }) != trans->m_ruleRemoveTargetById.end()) {
@ -185,7 +185,7 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars,
if (std::find_if(trans->m_ruleRemoveTargetByTag.begin(), if (std::find_if(trans->m_ruleRemoveTargetByTag.begin(),
trans->m_ruleRemoveTargetByTag.end(), trans->m_ruleRemoveTargetByTag.end(),
[&, variable, trans, this]( [&, variable, trans, this](
std::pair<std::string, std::string> &m) -> bool { const auto &m) -> bool {
return containsTag(m.first, trans) return containsTag(m.first, trans)
&& m.second == *variable->m_fullName.get(); && m.second == *variable->m_fullName.get();
}) != trans->m_ruleRemoveTargetByTag.end()) { }) != trans->m_ruleRemoveTargetByTag.end()) {
@ -203,7 +203,7 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars,
bool RuleWithOperator::evaluate(Transaction *trans, bool RuleWithOperator::evaluate(Transaction *trans,
RuleMessage &ruleMessage) { RuleMessage &ruleMessage) {
bool globalRet = false; bool globalRet = false;
variables::Variables *variables = this->m_variables; const variables::Variables *variables = this->m_variables; // cppcheck-suppress unreadVariable ; false positive
bool recursiveGlobalRet; bool recursiveGlobalRet;
bool containsBlock = hasBlockAction(); bool containsBlock = hasBlockAction();
std::string eparam; std::string eparam;
@ -270,23 +270,23 @@ bool RuleWithOperator::evaluate(Transaction *trans,
if (exclusion.contains(v) || if (exclusion.contains(v) ||
std::find_if(trans->m_ruleRemoveTargetById.begin(), std::find_if(trans->m_ruleRemoveTargetById.begin(),
trans->m_ruleRemoveTargetById.end(), trans->m_ruleRemoveTargetById.end(),
[&, v, this](std::pair<int, std::string> &m) -> bool { [&, v, this](const auto &m) -> bool {
return m.first == m_ruleId && m.second == v->getKeyWithCollection(); return m.first == m_ruleId && m.second == v->getKeyWithCollection();
}) != trans->m_ruleRemoveTargetById.end() }) != trans->m_ruleRemoveTargetById.end()
) { ) {
delete v; delete v;
v = NULL; v = nullptr;
continue; continue;
} }
if (exclusion.contains(v) || if (exclusion.contains(v) ||
std::find_if(trans->m_ruleRemoveTargetByTag.begin(), std::find_if(trans->m_ruleRemoveTargetByTag.begin(),
trans->m_ruleRemoveTargetByTag.end(), trans->m_ruleRemoveTargetByTag.end(),
[&, v, trans, this](std::pair<std::string, std::string> &m) -> bool { [&, v, trans, this](const auto &m) -> bool {
return containsTag(m.first, trans) && m.second == v->getKeyWithCollection(); return containsTag(m.first, trans) && m.second == v->getKeyWithCollection();
}) != trans->m_ruleRemoveTargetByTag.end() }) != trans->m_ruleRemoveTargetByTag.end()
) { ) {
delete v; delete v;
v = NULL; v = nullptr;
continue; continue;
} }
@ -360,7 +360,7 @@ end_exec:
} }
std::string RuleWithOperator::getOperatorName() const { return m_operator->m_op; } const std::string& RuleWithOperator::getOperatorName() const { return m_operator->m_op; }
} // namespace modsecurity } // namespace modsecurity

View File

@ -254,11 +254,11 @@ bool RulesExceptions::merge(RulesExceptions *from) {
p.second)); p.second));
} }
for (auto &p : from->m_remove_rule_by_msg) { for (const auto &p : from->m_remove_rule_by_msg) {
m_remove_rule_by_msg.push_back(p); m_remove_rule_by_msg.push_back(p);
} }
for (auto &p : from->m_remove_rule_by_tag) { for (const auto &p : from->m_remove_rule_by_tag) {
m_remove_rule_by_tag.push_back(p); m_remove_rule_by_tag.push_back(p);
} }

View File

@ -266,7 +266,7 @@ extern "C" RulesSet *msc_create_rules_set(void) {
} }
extern "C" void msc_rules_dump(RulesSet *rules) { extern "C" void msc_rules_dump(const RulesSet *rules) {
rules->dump(); rules->dump();
} }

View File

@ -30,9 +30,9 @@ void ConfigUnicodeMap::loadConfig(std::string f, double configCodePage,
RulesSetProperties *driver, std::string *errg) { RulesSetProperties *driver, std::string *errg) {
char *buf = NULL; char *buf = NULL;
char *hmap = NULL; char *hmap = NULL;
char *p = NULL; const char *p = NULL;
char *savedptr = NULL; char *savedptr = NULL;
char *ucode = NULL; const char *ucode = NULL;
int code = 0; int code = 0;
int found = 0; int found = 0;
int length = 0; int length = 0;

View File

@ -1175,7 +1175,7 @@ int Transaction::processResponseBody() {
int Transaction::appendResponseBody(const unsigned char *buf, size_t len) { int Transaction::appendResponseBody(const unsigned char *buf, size_t len) {
int current_size = this->m_responseBody.tellp(); int current_size = this->m_responseBody.tellp();
std::set<std::string> &bi = \ const std::set<std::string> &bi = \
this->m_rules->m_responseBodyTypeToBeInspected.m_value; this->m_rules->m_responseBodyTypeToBeInspected.m_value;
auto t = bi.find(m_variableResponseContentType.m_value); auto t = bi.find(m_variableResponseContentType.m_value);
if (t == bi.end() && bi.empty() == false) { if (t == bi.end() && bi.empty() == false) {
@ -1677,7 +1677,7 @@ std::string Transaction::toJSON(int parts) {
strlen("components")); strlen("components"));
yajl_gen_array_open(g); yajl_gen_array_open(g);
for (auto a : m_rules->m_components) { for (const auto &a : m_rules->m_components) {
yajl_gen_string(g, yajl_gen_string(g,
reinterpret_cast<const unsigned char*> reinterpret_cast<const unsigned char*>
(a.c_str()), a.length()); (a.c_str()), a.length());
@ -2197,7 +2197,7 @@ extern "C" void msc_intervention_cleanup(ModSecurityIntervention *it) {
* @retval NULL Nothing was updated. * @retval NULL Nothing was updated.
* *
*/ */
extern "C" const char *msc_get_response_body(Transaction *transaction) { extern "C" const char *msc_get_response_body(const Transaction *transaction) {
return transaction->getResponseBody(); return transaction->getResponseBody();
} }

View File

@ -154,7 +154,7 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
rc = pcre2_match(m_pc, pcre2_s, s.length(), rc = pcre2_match(m_pc, pcre2_s, s.length(),
offset, PCRE2_NO_JIT, match_data, NULL); offset, PCRE2_NO_JIT, match_data, NULL);
} }
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else #else
const char *subject = s.c_str(); const char *subject = s.c_str();
int ovector[OVECCOUNT]; int ovector[OVECCOUNT];
@ -207,7 +207,7 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, match_context); rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, match_context);
} }
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else #else
const char *subject = s.c_str(); const char *subject = s.c_str();
int ovector[OVECCOUNT]; int ovector[OVECCOUNT];
@ -261,7 +261,7 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
} }
int rc = pcre2_match(m_pc, pcre2_s, s.length(), int rc = pcre2_match(m_pc, pcre2_s, s.length(),
startOffset, pcre2_options, match_data, match_context); startOffset, pcre2_options, match_data, match_context);
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else #else
const char *subject = s.c_str(); const char *subject = s.c_str();

View File

@ -54,7 +54,7 @@ void Env::evaluate(Transaction *transaction,
} }
const auto hasName = m_name.length() > 0; const auto hasName = m_name.length() > 0;
for (auto& x : transaction->m_variableEnvs) { for (const auto& x : transaction->m_variableEnvs) {
#ifndef WIN32 #ifndef WIN32
if (hasName && x.first != m_name) { if (hasName && x.first != m_name) {
#else #else

View File

@ -48,23 +48,23 @@ Variable::Variable(const std::string &name)
} }
Variable::Variable(Variable *var) : Variable::Variable(const Variable *var) :
m_name(var->m_name), m_name(var->m_name),
m_collectionName(var->m_collectionName), m_collectionName(var->m_collectionName),
m_fullName(var->m_fullName) { } m_fullName(var->m_fullName) { }
void Variable::addsKeyExclusion(Variable *v) { void Variable::addsKeyExclusion(const Variable *v) {
std::unique_ptr<KeyExclusion> r; std::unique_ptr<KeyExclusion> r;
VariableModificatorExclusion *ve = \ const auto *ve = \
dynamic_cast<VariableModificatorExclusion *>(v); dynamic_cast<const VariableModificatorExclusion *>(v);
VariableRegex *vr; const VariableRegex *vr;
if (!ve) { if (!ve) {
return; return;
} }
vr = dynamic_cast<VariableRegex *>(ve->m_base.get()); vr = dynamic_cast<const VariableRegex *>(ve->m_base.get());
if (vr == NULL) { if (vr == NULL) {
r.reset(new KeyExclusionString(v->m_name)); r.reset(new KeyExclusionString(v->m_name));
@ -76,12 +76,12 @@ void Variable::addsKeyExclusion(Variable *v) {
} }
std::string operator+(const std::string &a, Variable *v) { std::string operator+(const std::string &a, const Variable *v) {
return a + *v->m_fullName.get(); return a + *v->m_fullName.get();
} }
std::string operator+(const std::string &a, Variables *v) { std::string operator+(const std::string &a, const Variables *v) {
std::string test; std::string test;
for (const auto &b : *v) { for (const auto &b : *v) {
if (test.empty()) { if (test.empty()) {

View File

@ -132,7 +132,7 @@ class KeyExclusionRegex : public KeyExclusion {
class KeyExclusionString : public KeyExclusion { class KeyExclusionString : public KeyExclusion {
public: public:
explicit KeyExclusionString(std::string &a) explicit KeyExclusionString(const std::string &a)
: m_key(utils::string::toupper(a)) { } : m_key(utils::string::toupper(a)) { }
~KeyExclusionString() override { } ~KeyExclusionString() override { }
@ -589,7 +589,7 @@ class VariableMonkeyResolution {
class Variable : public VariableMonkeyResolution { class Variable : public VariableMonkeyResolution {
public: public:
explicit Variable(const std::string &name); explicit Variable(const std::string &name);
explicit Variable(Variable *_name); explicit Variable(const Variable *_name);
virtual ~Variable() { } virtual ~Variable() { }
@ -608,7 +608,7 @@ class Variable : public VariableMonkeyResolution {
} }
void addsKeyExclusion(Variable *v); void addsKeyExclusion(const Variable *v);
bool operator==(const Variable& b) const { bool operator==(const Variable& b) const {
@ -718,8 +718,8 @@ class VariableModificatorCount : public Variable {
}; };
std::string operator+(const std::string &a, modsecurity::variables::Variable *v); std::string operator+(const std::string &a, const modsecurity::variables::Variable *v);
std::string operator+(const std::string &a, modsecurity::variables::Variables *v); std::string operator+(const std::string &a, const modsecurity::variables::Variables *v);
} // namespace variables } // namespace variables

View File

@ -44,7 +44,7 @@ char rules_file[] = "basic_rules.conf";
const char* const help_message = "Usage: benchmark [num_iterations|-h|-?|--help]"; const char* const help_message = "Usage: benchmark [num_iterations|-h|-?|--help]";
int main(int argc, char *argv[]) { int main(int argc, const char *argv[]) {
unsigned long long NUM_REQUESTS(1000000); unsigned long long NUM_REQUESTS(1000000);

View File

@ -89,7 +89,7 @@ template <class T>
void void
ModSecurityTest<T>::load_tests(const std::string &path) { ModSecurityTest<T>::load_tests(const std::string &path) {
DIR *dir; DIR *dir;
struct dirent *ent; const struct dirent *ent;
struct stat buffer; struct stat buffer;
if ((dir = opendir(path.c_str())) == nullptr) { if ((dir = opendir(path.c_str())) == nullptr) {

View File

@ -54,7 +54,7 @@ int main(int argc, char **argv) {
} }
for (auto &x : files) { for (const auto &x : files) {
std::cout << "Loading file: " << x << std::endl; std::cout << "Loading file: " << x << std::endl;
if (modsecRules->loadFromUri(x.c_str()) < 0) { if (modsecRules->loadFromUri(x.c_str()) < 0) {
std::cout << "Not able to load the rules" << std::endl; std::cout << "Not able to load the rules" << std::endl;
@ -96,8 +96,8 @@ int main(int argc, char **argv) {
} }
} }
if (auto rwo = dynamic_cast<modsecurity::RuleWithOperator *>(z.get())) { if (const auto *rwo = dynamic_cast<modsecurity::RuleWithOperator *>(z.get())) {
std::string op = rwo->getOperatorName(); const auto op = rwo->getOperatorName();
if (operators.count(op) > 0) { if (operators.count(op) > 0) {
operators[op] = 1 + operators[op]; operators[op] = 1 + operators[op];
} else { } else {

View File

@ -26,6 +26,7 @@
#include <string> #include <string>
#include <list> #include <list>
#include <algorithm> #include <algorithm>
#include <cassert>
#include "modsecurity/rules_set.h" #include "modsecurity/rules_set.h"
#include "modsecurity/modsecurity.h" #include "modsecurity/modsecurity.h"
@ -110,8 +111,8 @@ void actions(ModSecurityTestResults<RegressionTest> *r,
} }
} }
void perform_unit_test(ModSecurityTest<RegressionTest> *test, void perform_unit_test(const ModSecurityTest<RegressionTest> &test,
std::vector<std::unique_ptr<RegressionTest>> &tests, const std::vector<std::unique_ptr<RegressionTest>> &tests,
ModSecurityTestResults<RegressionTestResult> *res, int *count) ModSecurityTestResults<RegressionTestResult> *res, int *count)
{ {
for (auto &t : tests) { for (auto &t : tests) {
@ -131,7 +132,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
filename = t->filename; filename = t->filename;
} }
if (!test->m_automake_output) { if (!test.m_automake_output) {
std::cout << std::setw(3) << std::right << std::cout << std::setw(3) << std::right <<
std::to_string(*count) << " "; std::to_string(*count) << " ";
std::cout << std::setw(50) << std::left << filename; std::cout << std::setw(50) << std::left << filename;
@ -139,7 +140,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
} }
if (t->enabled == 0) { if (t->enabled == 0) {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: SKIP" << filename \ std::cout << ":test-result: SKIP" << filename \
<< ":" << t->name << std::endl; << ":" << t->name << std::endl;
} else { } else {
@ -173,7 +174,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
testRes->reason << KCYN << "compiled with support " << std::endl; testRes->reason << KCYN << "compiled with support " << std::endl;
testRes->reason << KCYN << "to: " << t->resource << std::endl; testRes->reason << KCYN << "to: " << t->resource << std::endl;
testRes->reason << RESET << std::endl; testRes->reason << RESET << std::endl;
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: SKIP " << filename \ std::cout << ":test-result: SKIP " << filename \
<< ":" << t->name << std::endl; << ":" << t->name << std::endl;
} else { } else {
@ -192,7 +193,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
* Not expecting any error, thus return the error to * Not expecting any error, thus return the error to
* the user. * the user.
*/ */
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl; << ":" << t->name << ":" << *count << std::endl;
} else { } else {
@ -213,7 +214,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
const auto s = context.m_modsec_rules.getParserError(); const auto s = context.m_modsec_rules.getParserError();
if (regex_search(s, &match, re)) { if (regex_search(s, &match, re)) {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: PASS " << filename \ std::cout << ":test-result: PASS " << filename \
<< ":" << t->name << std::endl; << ":" << t->name << std::endl;
} else { } else {
@ -227,7 +228,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
continue; continue;
} else { } else {
/* Parser error was expected, but with a different content */ /* Parser error was expected, but with a different content */
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl; << ":" << t->name << ":" << *count << std::endl;
} else { } else {
@ -249,7 +250,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
} else { } else {
/* Parser error was expected but never happened */ /* Parser error was expected but never happened */
if (t->parser_error.empty() == false) { if (t->parser_error.empty() == false) {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl; << ":" << t->name << ":" << *count << std::endl;
} else { } else {
@ -318,7 +319,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
const auto *d = static_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) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl; << ":" << t->name << ":" << *count << std::endl;
} else { } else {
@ -330,7 +331,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
<< t->debug_log + ""; << t->debug_log + "";
testRes->passed = false; testRes->passed = false;
} else if (r.status != t->http_code) { } else if (r.status != t->http_code) {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl; << ":" << t->name << ":" << *count << std::endl;
} else { } else {
@ -341,7 +342,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
" got: " + std::to_string(r.status) + "\n"; " got: " + std::to_string(r.status) + "\n";
testRes->passed = false; testRes->passed = false;
} else if (!contains(context.m_server_log.str(), t->error_log)) { } else if (!contains(context.m_server_log.str(), t->error_log)) {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << std::endl; << ":" << t->name << std::endl;
} else { } else {
@ -353,7 +354,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
<< t->error_log + ""; << t->error_log + "";
testRes->passed = false; testRes->passed = false;
} else if (!t->audit_log.empty() && !contains(getAuditLogContent(modsec_transaction.m_rules->m_auditLog->m_path1), t->audit_log)) { } else if (!t->audit_log.empty() && !contains(getAuditLogContent(modsec_transaction.m_rules->m_auditLog->m_path1), t->audit_log)) {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \ std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl; << ":" << t->name << ":" << *count << std::endl;
} else { } else {
@ -365,7 +366,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
<< t->audit_log + ""; << t->audit_log + "";
testRes->passed = false; testRes->passed = false;
} else { } else {
if (test->m_automake_output) { if (test.m_automake_output) {
std::cout << ":test-result: PASS " << filename \ std::cout << ":test-result: PASS " << filename \
<< ":" << t->name << std::endl; << ":" << t->name << std::endl;
} else { } else {
@ -471,8 +472,8 @@ int main(int argc, char **argv)
test_number++; test_number++;
if ((test.m_test_number == 0) if ((test.m_test_number == 0)
|| (test_number == test.m_test_number)) { || (test_number == test.m_test_number)) {
auto &tests = test[a]; const auto &tests = test[a];
perform_unit_test(&test, tests, &res, &counter); perform_unit_test(test, tests, &res, &counter);
} }
} }

View File

@ -92,7 +92,7 @@ struct TransformationTest {
return tfn; return tfn;
} }
static UnitTestResult eval(const ItemType &tfn, const UnitTest &t, modsecurity::Transaction &transaction) { static UnitTestResult eval(const ItemType &tfn, const UnitTest &t, const modsecurity::Transaction &transaction) {
auto ret = t.input; auto ret = t.input;
tfn.transform(ret, &transaction); tfn.transform(ret, &transaction);
return {1, ret}; return {1, ret};

View File

@ -75,7 +75,7 @@ void json2bin(std::string *str) {
} }
std::string UnitTest::print() { std::string UnitTest::print() const {
std::stringstream i; std::stringstream i;
i << KRED << "Test failed." << RESET; i << KRED << "Test failed." << RESET;

View File

@ -35,7 +35,7 @@ class UnitTest {
public: public:
static UnitTest *from_yajl_node(const yajl_val &); static UnitTest *from_yajl_node(const yajl_val &);
std::string print(); std::string print() const;
std::string param; std::string param;
std::string input; std::string input;