Refactorizations:

- Fixed a few more MSVC warnings by using correct types
- Store severity as enum instead of string in Settings::Rule
This commit is contained in:
PKEuS 2015-11-28 12:30:03 +01:00
parent 174317429f
commit ac17541ca9
5 changed files with 18 additions and 17 deletions

View File

@ -648,7 +648,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
if (message) {
tinyxml2::XMLElement *severity = message->FirstChildElement("severity");
if (severity)
rule.severity = severity->GetText();
rule.severity = Severity::fromString(severity->GetText());
tinyxml2::XMLElement *id = message->FirstChildElement("id");
if (id)

View File

@ -303,7 +303,7 @@ static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
}
//---------------------------------------------------------------------------
static bool checkMinSizes(const std::list<Library::ArgumentChecks::MinSize> &minsizes, const Token * const ftok, const std::size_t arraySize, const Token **charSizeToken, const Settings * const settings)
static bool checkMinSizes(const std::list<Library::ArgumentChecks::MinSize> &minsizes, const Token * const ftok, const MathLib::bigint arraySize, const Token **charSizeToken, const Settings * const settings)
{
if (charSizeToken)
*charSizeToken = nullptr;
@ -534,7 +534,7 @@ void CheckBufferOverrun::checkFunctionCall(const Token *tok, const ArrayInfo &ar
void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::string> &varname, const ArrayInfo &arrayInfo)
{
const MathLib::bigint size = arrayInfo.num(0);
if (size == 0) // unknown size
if (size <= 0) // unknown size
return;
if (tok->str() == "return") {
@ -748,7 +748,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
}
}
static std::vector<ValueFlow::Value> valueFlowGetArrayIndexes(const Token * const tok, bool conditional, unsigned int dimensions)
static std::vector<ValueFlow::Value> valueFlowGetArrayIndexes(const Token * const tok, bool conditional, std::size_t dimensions)
{
unsigned int indexvarid = 0;
const std::vector<ValueFlow::Value> empty;
@ -852,7 +852,7 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const
void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo)
{
assert(tok->previous() != nullptr);
const MathLib::bigint total_size = arrayInfo.num(0) * arrayInfo.element_size();
const MathLib::biguint total_size = arrayInfo.num(0) * arrayInfo.element_size();
const unsigned int declarationId = arrayInfo.declarationId();
@ -913,7 +913,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
if (printWarning && printInconclusive && Token::Match(tok, "strncpy|memcpy|memmove ( %varid% , %str% , %num% )", declarationId)) {
if (Token::getStrLength(tok->tokAt(4)) >= total_size) {
const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(6));
const MathLib::biguint num = MathLib::toULongNumber(tok->strAt(6));
if (total_size == num)
bufferNotZeroTerminatedError(tok, tok->strAt(2), tok->str());
}
@ -925,7 +925,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
// check for strncpy which is not terminated
if (tok->str() == "strncpy") {
// strncpy takes entire variable length as input size
const MathLib::bigint num = MathLib::toLongNumber(param3->str());
const MathLib::biguint num = MathLib::toULongNumber(param3->str());
// this is currently 'inconclusive'. See TestBufferOverrun::terminateStrncpy3
if (printInconclusive && num >= total_size) {
@ -945,14 +945,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
// Dangerous usage of strncat..
else if (tok->str() == "strncat") {
const MathLib::bigint n = MathLib::toLongNumber(param3->str());
const MathLib::biguint n = MathLib::toULongNumber(param3->str());
if (n >= total_size)
strncatUsageError(tok);
}
// Dangerous usage of strncpy + strncat..
if (Token::Match(param3->tokAt(2), "; strncat ( %varid% ,", declarationId) && Token::Match(param3->linkAt(4)->tokAt(-2), ", %num% )")) {
const MathLib::bigint n = MathLib::toLongNumber(param3->str()) + MathLib::toLongNumber(param3->linkAt(4)->strAt(-1));
const MathLib::biguint n = MathLib::toULongNumber(param3->str()) + MathLib::toULongNumber(param3->linkAt(4)->strAt(-1));
if (n > total_size)
strncatUsageError(param3->tokAt(3));
}
@ -969,7 +969,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
// Detect few strcat() calls
if (total_size > 0) {
std::size_t charactersAppend = 0;
MathLib::biguint charactersAppend = 0;
const Token *tok2 = tok;
while (Token::Match(tok2, "strcat ( %varid% , %str% ) ;", declarationId)) {

View File

@ -1014,7 +1014,7 @@ void CheckClass::checkMemset()
else if (Token::simpleMatch(arg3, "sizeof ( * this ) )") || Token::simpleMatch(arg1, "this ,")) {
type = findFunctionOf(arg3->scope());
} else if (Token::Match(arg1, "&|*|%var%")) {
int numIndirToVariableType = 0; // Offset to the actual type in terms of dereference/addressof
std::size_t numIndirToVariableType = 0; // Offset to the actual type in terms of dereference/addressof
for (;; arg1 = arg1->next()) {
if (arg1->str() == "&")
++numIndirToVariableType;

View File

@ -456,7 +456,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token
for (std::list<Settings::Rule>::const_iterator it = _settings.rules.begin(); it != _settings.rules.end(); ++it) {
const Settings::Rule &rule = *it;
if (rule.pattern.empty() || rule.id.empty() || rule.severity.empty() || rule.tokenlist != tokenlist)
if (rule.pattern.empty() || rule.id.empty() || rule.severity == Severity::none || rule.tokenlist != tokenlist)
continue;
const char *error = nullptr;
@ -507,7 +507,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token
summary = "found '" + str.substr(pos1, pos2 - pos1) + "'";
else
summary = rule.summary;
const ErrorLogger::ErrorMessage errmsg(callStack, Severity::fromString(rule.severity), summary, rule.id, false);
const ErrorLogger::ErrorMessage errmsg(callStack, rule.severity, summary, rule.id, false);
// Report error
reportErr(errmsg);

View File

@ -29,6 +29,7 @@
#include "library.h"
#include "suppressions.h"
#include "standards.h"
#include "errorlogger.h"
#include "timer.h"
/// @addtogroup Core
@ -216,16 +217,16 @@ public:
class CPPCHECKLIB Rule {
public:
Rule()
: tokenlist("simple") // use simple tokenlist
, id("rule") // default id
, severity("style") { // default severity
: tokenlist("simple") // use simple tokenlist
, id("rule") // default id
, severity(Severity::style) { // default severity
}
std::string tokenlist;
std::string pattern;
std::string id;
std::string severity;
std::string summary;
Severity::SeverityType severity;
};
/**