Fixed #7888 (false positive from knownConditionTrueFalse with sizeof(int) == sizeof(long))

This commit is contained in:
Daniel Marjamäki 2017-05-01 12:23:28 +02:00
parent 34a56506cb
commit bc7799dccd
3 changed files with 32 additions and 34 deletions

View File

@ -699,6 +699,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
else if (platform == "unix64") else if (platform == "unix64")
_settings->platform(Settings::Unix64); _settings->platform(Settings::Unix64);
else if (platform == "native") else if (platform == "native")
_settings->platform(Settings::Native);
else if (platform == "unspecified")
_settings->platform(Settings::Unspecified); _settings->platform(Settings::Unspecified);
else if (!_settings->platformFile(platform)) { else if (!_settings->platformFile(platform)) {
std::string message("cppcheck: error: unrecognized platform: \""); std::string message("cppcheck: error: unrecognized platform: \"");
@ -943,8 +945,10 @@ void CmdLineParser::PrintHelp()
" * win64\n" " * win64\n"
" 64 bit Windows\n" " 64 bit Windows\n"
" * native\n" " * native\n"
" Unspecified platform. Type sizes of host system\n" " Type sizes of host system are assumed, but no\n"
" are assumed, but no further assumptions.\n" " further assumptions.\n"
" * unspecified\n"
" Unknown type sizes\n"
" -q, --quiet Do not show progress reports.\n" " -q, --quiet Do not show progress reports.\n"
" -rp, --relative-paths\n" " -rp, --relative-paths\n"
" -rp=<paths>, --relative-paths=<paths>\n" " -rp=<paths>, --relative-paths=<paths>\n"

View File

@ -36,26 +36,7 @@ cppcheck::Platform::Platform()
bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type) bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type)
{ {
switch (type) { switch (type) {
case Unspecified: case Unspecified: // unknown type sizes (sizes etc are set but are not known)
platformType = type;
sizeof_bool = sizeof(bool);
sizeof_short = sizeof(short);
sizeof_int = sizeof(int);
sizeof_long = sizeof(long);
sizeof_long_long = sizeof(long long);
sizeof_float = sizeof(float);
sizeof_double = sizeof(double);
sizeof_long_double = sizeof(long double);
sizeof_wchar_t = sizeof(wchar_t);
sizeof_size_t = sizeof(std::size_t);
sizeof_pointer = sizeof(void *);
defaultSign = '\0';
char_bit = 8;
short_bit = char_bit * sizeof_short;
int_bit = char_bit * sizeof_int;
long_bit = char_bit * sizeof_long;
long_long_bit = char_bit * sizeof_long_long;
return true;
case Native: // same as system this code was compile on case Native: // same as system this code was compile on
platformType = type; platformType = type;
sizeof_bool = sizeof(bool); sizeof_bool = sizeof(bool);
@ -69,7 +50,9 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type)
sizeof_wchar_t = sizeof(wchar_t); sizeof_wchar_t = sizeof(wchar_t);
sizeof_size_t = sizeof(std::size_t); sizeof_size_t = sizeof(std::size_t);
sizeof_pointer = sizeof(void *); sizeof_pointer = sizeof(void *);
{ if (type == Unspecified) {
defaultSign = '\0';
} else {
char x = -1; char x = -1;
defaultSign = (x < 0) ? 's' : 'u'; defaultSign = (x < 0) ? 's' : 'u';
} }

View File

@ -643,7 +643,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti
type->str() == "long" ? settings->sizeof_long : 0; type->str() == "long" ? settings->sizeof_long : 0;
} }
ValueFlow::Value value(size); ValueFlow::Value value(size);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok), value, settings); setTokenValue(const_cast<Token *>(tok), value, settings);
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (tok2->type() && tok2->type()->isEnumType()) { } else if (tok2->type() && tok2->type()->isEnumType()) {
@ -659,7 +660,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti
} }
} }
ValueFlow::Value value(size); ValueFlow::Value value(size);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok), value, settings); setTokenValue(const_cast<Token *>(tok), value, settings);
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (Token::Match(tok, "sizeof ( %var% ) / sizeof (") && tok->next()->astParent() == tok->tokAt(4)) { } else if (Token::Match(tok, "sizeof ( %var% ) / sizeof (") && tok->next()->astParent() == tok->tokAt(4)) {
@ -674,42 +676,51 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti
sz1->variable()->dimensionKnown(0) && sz1->variable()->dimensionKnown(0) &&
(Token::Match(sz2, "* %varid% )", varid1) || Token::Match(sz2, "%varid% [ 0 ] )", varid1))) { (Token::Match(sz2, "* %varid% )", varid1) || Token::Match(sz2, "%varid% [ 0 ] )", varid1))) {
ValueFlow::Value value(sz1->variable()->dimension(0)); ValueFlow::Value value(sz1->variable()->dimension(0));
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->tokAt(4)), value, settings); setTokenValue(const_cast<Token *>(tok->tokAt(4)), value, settings);
} }
} else if (!tok2->type()) { } else if (!tok2->type()) {
const ValueType &vt = ValueType::parseDecl(tok2,settings); const ValueType &vt = ValueType::parseDecl(tok2,settings);
if (vt.pointer) { if (vt.pointer) {
ValueFlow::Value value(settings->sizeof_pointer); ValueFlow::Value value(settings->sizeof_pointer);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::CHAR) { } else if (vt.type == ValueType::Type::CHAR) {
ValueFlow::Value value(1); ValueFlow::Value value(1);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::SHORT) { } else if (vt.type == ValueType::Type::SHORT) {
ValueFlow::Value value(settings->sizeof_short); ValueFlow::Value value(settings->sizeof_short);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::INT) { } else if (vt.type == ValueType::Type::INT) {
ValueFlow::Value value(settings->sizeof_int); ValueFlow::Value value(settings->sizeof_int);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::LONG) { } else if (vt.type == ValueType::Type::LONG) {
ValueFlow::Value value(settings->sizeof_long); ValueFlow::Value value(settings->sizeof_long);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::LONGLONG) { } else if (vt.type == ValueType::Type::LONGLONG) {
ValueFlow::Value value(settings->sizeof_long_long); ValueFlow::Value value(settings->sizeof_long_long);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::FLOAT) { } else if (vt.type == ValueType::Type::FLOAT) {
ValueFlow::Value value(settings->sizeof_float); ValueFlow::Value value(settings->sizeof_float);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} else if (vt.type == ValueType::Type::DOUBLE) { } else if (vt.type == ValueType::Type::DOUBLE) {
ValueFlow::Value value(settings->sizeof_double); ValueFlow::Value value(settings->sizeof_double);
value.setKnown(); if (settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();
setTokenValue(const_cast<Token *>(tok->next()), value, settings); setTokenValue(const_cast<Token *>(tok->next()), value, settings);
} }
} }