Code cosmetics: reduce the amount of cppcheck warnings

This commit is contained in:
Felipe Zimmerle 2015-08-12 15:22:00 -03:00
parent 21400ba454
commit d5fe21ce3c
24 changed files with 49 additions and 56 deletions

View File

@ -227,10 +227,10 @@ class Assay {
int getResponseBodyLenth();
std::list<std::pair<std::string, std::string>>
resolve_variable(std::string var);
std::string* resolve_variable_first(std::string);
std::string* resolve_variable_first(const std::string collectionName,
std::string var);
resolve_variable(const std::string& var);
std::string* resolve_variable_first(const std::string& key);
std::string* resolve_variable_first(const std::string& collectionName,
const std::string& var);
void store_variable(std::string, std::string value);
bool update_variable_first(std::string var, const std::string &value);

View File

@ -47,6 +47,7 @@ class Rules : public RulesProperties {
Rules()
: debugLog(NULL),
RulesProperties(NULL),
m_referenceCount(0),
unicode_codepage(0) {
unicode_map_table = reinterpret_cast<int *>(
malloc(sizeof(int)*65536));
@ -55,6 +56,7 @@ class Rules : public RulesProperties {
explicit Rules(DebugLog *customLog)
: debugLog(NULL),
m_referenceCount(0),
unicode_codepage(0),
RulesProperties(customLog) {
unicode_map_table = reinterpret_cast<int *>(

View File

@ -45,8 +45,11 @@ class RulesProperties {
customDebugLog(NULL),
remoteRulesActionOnFailed(AbortOnFailedRemoteRulesAction),
requestBodyLimit(0),
secRequestBodyAccess(false),
secResponseBodyAccess(false),
requestBodyLimitAction(ProcessPartialBodyLimitAction),
responseBodyLimit(0),
debugLevel(0),
responseBodyLimitAction(ProcessPartialBodyLimitAction),
secRuleEngine(DetectionOnlyRuleEngine) { }
@ -54,6 +57,9 @@ class RulesProperties {
: audit_log(NULL),
customDebugLog(customDebugLog),
remoteRulesActionOnFailed(AbortOnFailedRemoteRulesAction),
secRequestBodyAccess(false),
secResponseBodyAccess(false),
debugLevel(0),
requestBodyLimit(0),
requestBodyLimitAction(ProcessPartialBodyLimitAction),
responseBodyLimit(0),
@ -157,14 +163,12 @@ class RulesProperties {
DebugLog *customDebugLog;
int sec_audit_type;
bool sec_audit_engine;
bool sec_request_body_access;
bool sec_response_body_access;
bool secRequestBodyAccess;
bool secResponseBodyAccess;
std::string audit_log_path;
std::string audit_log_parts;
std::string debug_log_path;
int debug_level;
int debugLevel;
std::list<std::string> components;

View File

@ -31,7 +31,7 @@ SetVar::SetVar(std::string action)
bool SetVar::init(std::string *error) {
size_t pos = std::string::npos;
size_t pos;
// Resolv operation
operation = setToOne;

View File

@ -37,7 +37,7 @@ namespace transformations {
std::string CssDecode::evaluate(std::string value,
Assay *assay) {
char *tmp = strdup(value.c_str());
int res = css_decode_inplace((unsigned char *)tmp, value.size());
css_decode_inplace((unsigned char *)tmp, value.size());
std::string ret(tmp, 0, value.size());
free(tmp);
return ret;

View File

@ -37,7 +37,7 @@ namespace transformations {
std::string HtmlEntityDecode::evaluate(std::string value,
Assay *assay) {
char *tmp = strdup(value.c_str());
int res = html_entities_decode_inplace((unsigned char *)tmp, value.size());
html_entities_decode_inplace((unsigned char *)tmp, value.size());
std::string ret("");
ret.assign(tmp);
free(tmp);

View File

@ -37,7 +37,7 @@ namespace transformations {
std::string JsDecode::evaluate(std::string value,
Assay *assay) {
char *tmp = strdup(value.c_str());
int res = js_decode_nonstrict_inplace((unsigned char *)tmp, value.size());
js_decode_nonstrict_inplace((unsigned char *)tmp, value.size());
std::string ret("");
ret.assign(tmp);
free(tmp);

View File

@ -38,7 +38,7 @@ std::string NormalisePathWin::evaluate(std::string value,
Assay *assay) {
int changed;
char *tmp = strdup(value.c_str());
int res = normalize_path_inplace((unsigned char *)tmp,
normalize_path_inplace((unsigned char *)tmp,
value.size(), 1, &changed);
std::string ret("");
ret.assign(tmp);

View File

@ -38,7 +38,7 @@ std::string UrlDecodeUni::evaluate(std::string value,
Assay *assay) {
int changed = 0;
char *tmp = strdup(value.c_str());
int res = urldecode_uni_nonstrict_inplace_ex(assay, (unsigned char *)tmp,
urldecode_uni_nonstrict_inplace_ex(assay, (unsigned char *)tmp,
value.size(), &changed);
std::string ret("");
ret.assign(tmp);

View File

@ -579,7 +579,6 @@ int Assay::processRequestBody() {
*
*/
char sep1 = '&';
const char *pos = strchr(content.c_str(), '?');
std::vector<std::string> key_value = split(content.c_str(), sep1);
@ -741,7 +740,6 @@ int Assay::processResponseHeaders() {
*/
int Assay::addResponseHeader(const std::string& key,
const std::string& value) {
std::string *names = resolve_variable_first("RESPONSE_HEADERS_NAMES");
m_responseHeadersNames->assign(*m_responseHeadersNames + " " + key);
this->store_variable("RESPONSE_HEADERS:" + key, value);
@ -1324,7 +1322,7 @@ void Assay::delete_variable(std::string key) {
std::list<std::pair<std::string, std::string>>
Assay::resolve_variable(std::string var) {
Assay::resolve_variable(const std::string& var) {
std::list<std::pair<std::string, std::string>> l;
std::pair<std::string, std::string> pair;
@ -1335,7 +1333,7 @@ std::list<std::pair<std::string, std::string>>
l.push_back(pair);
}
if (l.size() == 0) {
if (l.empty()) {
for (auto &x : m_variables_strings) {
if ((x.first.substr(0, var.size() + 1).compare(var + ":") != 0)
&& (x.first != var)) {
@ -1359,7 +1357,7 @@ std::list<std::pair<std::string, std::string>>
l.push_back(pair);
}
if (l.size() == 0) {
if (l.empty()) {
for (auto &x : *a.second) {
if ((x.first.substr(0, var.size() + 1).compare(var + ":") != 0)
&& (x.first != var)) {
@ -1383,7 +1381,7 @@ void Assay::serverLog(const std::string& msg) {
std::cerr << "Server log is not ready : " << msg << std::endl;
}
std::string* Assay::resolve_variable_first(std::string var) {
std::string* Assay::resolve_variable_first(const std::string& var) {
auto range = m_variables_strings.equal_range(var);
for (auto it = range.first; it != range.second; ++it) {
@ -1400,8 +1398,8 @@ std::string* Assay::resolve_variable_first(std::string var) {
}
std::string* Assay::resolve_variable_first(const std::string collectionName,
std::string var) {
std::string* Assay::resolve_variable_first(const std::string& collectionName,
const std::string& var) {
for (auto &a : collections) {
if (tolower(a.first) == tolower(collectionName)) {
auto range = a.second->equal_range(toupper(collectionName)
@ -1418,9 +1416,9 @@ std::string* Assay::resolve_variable_first(const std::string collectionName,
void Assay::setCollection(const std::string& collectionName,
const std::string& variableName,
const std::string& targetValue) {
ModSecurityStringVariables *collection;
try {
ModSecurityStringVariables *collection;
collection = collections.at(toupper(collectionName));
collection->storeOrUpdateVariable(toupper(collectionName) + ":"
+ variableName, targetValue);

View File

@ -47,7 +47,6 @@ inline std::string AuditLogWriterParallel::logFilePath(time_t *t,
int part) {
struct tm timeinfo;
char tstr[300];
size_t len;
std::string name("");
localtime_r(t, &timeinfo);

View File

@ -27,7 +27,7 @@ int ValidateUrlEncoding::validate_url_encoding(const char *input,
uint64_t input_length) {
int i;
if ((input == NULL) || (input_length < 0)) {
if ((input == NULL) || (input_length <= 0)) {
return -1;
}

View File

@ -70,7 +70,6 @@ bool VerifyCC::evaluate(Assay *assay, const std::string &i) {
int offset = 0;
bool is_cc = false;
int target_length = i.length();
const char *target = i.c_str();
for (offset = 0; offset < target_length; offset++) {
std::string shiftedString(i, offset, i.length() - offset);

View File

@ -311,19 +311,19 @@ expression:
}
| CONFIG_DIR_REQ_BODY SPACE CONFIG_VALUE_ON
{
driver.sec_request_body_access = true;
driver.secRequestBodyAccess = true;
}
| CONFIG_DIR_REQ_BODY SPACE CONFIG_VALUE_OFF
{
driver.sec_request_body_access = false;
driver.secRequestBodyAccess = false;
}
| CONFIG_DIR_RES_BODY SPACE CONFIG_VALUE_ON
{
driver.sec_request_body_access = true;
driver.secResponseBodyAccess = true;
}
| CONFIG_DIR_RES_BODY SPACE CONFIG_VALUE_OFF
{
driver.sec_request_body_access = false;
driver.secResponseBodyAccess = false;
}
| CONFIG_COMPONENT_SIG
{
@ -332,7 +332,7 @@ expression:
/* Debug log: start */
| CONFIG_DIR_DEBUG_LVL
{
driver.debug_level = atoi($1.c_str());
driver.debugLevel = atoi($1.c_str());
}
| CONFIG_DIR_DEBUG_LOG
{

View File

@ -56,7 +56,7 @@ bool MultipartBlob::processContent() {
}
bool contentTypeLine = processContentTypeLine(secondLine);
if (dispositionLine == false) {
if (contentTypeLine == false) {
return false;
}

View File

@ -188,12 +188,10 @@ int Rules::merge(Driver *from) {
}
this->secRuleEngine = from->secRuleEngine;
this->sec_audit_type = from->sec_audit_type;
this->sec_audit_engine = from->sec_audit_engine;
this->sec_request_body_access = from->sec_request_body_access;
this->sec_response_body_access = from->sec_response_body_access;
this->secRequestBodyAccess = from->secRequestBodyAccess;
this->secResponseBodyAccess = from->secResponseBodyAccess;
this->debug_log_path = from->debug_log_path;
this->debug_level = from->debug_level;
this->debugLevel = from->debugLevel;
this->components = from->components;
this->requestBodyLimit = from->requestBodyLimit;
this->responseBodyLimit = from->responseBodyLimit;
@ -210,7 +208,7 @@ int Rules::merge(Driver *from) {
this->audit_log = from->audit_log;
this->audit_log->refCountIncrease();
this->debugLog->setDebugLevel(this->debug_level);
this->debugLog->setDebugLevel(this->debugLevel);
this->debugLog->setOutputFile(this->debug_log_path);
return 0;
@ -228,10 +226,8 @@ int Rules::merge(Rules *from) {
}
this->secRuleEngine = from->secRuleEngine;
this->sec_audit_type = from->sec_audit_type;
this->sec_audit_engine = from->sec_audit_engine;
this->sec_request_body_access = from->sec_request_body_access;
this->sec_response_body_access = from->sec_response_body_access;
this->secRequestBodyAccess = from->secRequestBodyAccess;
this->secResponseBodyAccess = from->secResponseBodyAccess;
this->components = from->components;
this->requestBodyLimit = from->requestBodyLimit;
this->responseBodyLimit = from->responseBodyLimit;
@ -248,7 +244,7 @@ int Rules::merge(Rules *from) {
this->audit_log = from->audit_log;
this->audit_log->refCountIncrease();
this->debugLog->setDebugLevel(this->debug_level);
this->debugLog->setDebugLevel(this->debugLevel);
this->debugLog->setOutputFile(this->debug_log_path);
return 0;

View File

@ -190,7 +190,7 @@ std::string UniqueId::ethernetMacAddress() {
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO *>malloc(ulOutBufLen));
pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO *>(malloc(ulOutBufLen));
if (!pAdapterInfo) {
goto failed;
}

View File

@ -73,7 +73,7 @@ double random_number(const double from, const double to) {
std::string dash_if_empty(const std::string& str) {
if (&str == NULL || str.empty()) {
if (str.empty()) {
return "-";
}

View File

@ -80,10 +80,9 @@ IpTree::~IpTree() {
bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
char *error_msg = NULL;
int res = 0;
for (std::string line; std::getline(*ss, line); ) {
res = ip_tree_from_param(line.c_str(), &m_tree, &error_msg);
int res = ip_tree_from_param(line.c_str(), &m_tree, &error_msg);
if (res != 0) {
if (error_msg != NULL) {
error->assign(error_msg);

View File

@ -354,7 +354,7 @@ std::ostream& operator<<(std::ostream& out, MD5 md5)
//////////////////////////////
std::string md5(const std::string str)
std::string md5(const std::string& str)
{
MD5 md5 = MD5(str);

View File

@ -88,6 +88,6 @@ private:
static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
};
std::string md5(const std::string str);
std::string md5(const std::string& str);
#endif

View File

@ -53,7 +53,6 @@ int regex_search(const std::string& s, SMatch *match,
}
int regex_search(const std::string& s, Regex regex) {
std::string match;
pcrecpp::RE re(regex.pattern);
return re.PartialMatch(s);
}

View File

@ -45,7 +45,6 @@ std::string ModSecurityTest<T>::header() {
template <class T>
bool ModSecurityTest<T>::load_test_json(std::string file) {
std::vector<yajl_val> tests;
char errbuf[1024];
yajl_val node;
@ -128,9 +127,8 @@ std::pair<std::string, std::vector<T *>>* ModSecurityTest<T>::load_tests() {
template <class T>
void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
int option_char;
#if HAS_GETOPT
int option_char;
GetOpt getopt(argc, argv, "hvct:");
while ((option_char = getopt()) != EOF) {

View File

@ -58,13 +58,12 @@ void print_help() {
void perform_unit_test(UnitTest *t, ModSecurityTestResults<UnitTest>* res) {
const char *error = NULL;
int ret = 0;
if (t->type == "op") {
Operator *op = Operator::instantiate("\"@" + t->name + \
" " + t->param + "\"");
op->init(&error);
ret = op->evaluate(NULL, t->input);
int ret = op->evaluate(NULL, t->input);
if (ret != t->ret) {
t->obtained = ret;
res->push_back(t);