From bc7799dccdbea9de1cdacab8b3e2a656006515a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 May 2017 12:23:28 +0200 Subject: [PATCH] Fixed #7888 (false positive from knownConditionTrueFalse with sizeof(int) == sizeof(long)) --- cli/cmdlineparser.cpp | 8 ++++++-- lib/platform.cpp | 25 ++++--------------------- lib/valueflow.cpp | 33 ++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index c5c059e9b..0792dfde3 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -699,6 +699,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) else if (platform == "unix64") _settings->platform(Settings::Unix64); else if (platform == "native") + _settings->platform(Settings::Native); + else if (platform == "unspecified") _settings->platform(Settings::Unspecified); else if (!_settings->platformFile(platform)) { std::string message("cppcheck: error: unrecognized platform: \""); @@ -943,8 +945,10 @@ void CmdLineParser::PrintHelp() " * win64\n" " 64 bit Windows\n" " * native\n" - " Unspecified platform. Type sizes of host system\n" - " are assumed, but no further assumptions.\n" + " Type sizes of host system are assumed, but no\n" + " further assumptions.\n" + " * unspecified\n" + " Unknown type sizes\n" " -q, --quiet Do not show progress reports.\n" " -rp, --relative-paths\n" " -rp=, --relative-paths=\n" diff --git a/lib/platform.cpp b/lib/platform.cpp index f0ed68a30..33422aed9 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -36,26 +36,7 @@ cppcheck::Platform::Platform() bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type) { switch (type) { - case Unspecified: - 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 Unspecified: // unknown type sizes (sizes etc are set but are not known) case Native: // same as system this code was compile on platformType = type; sizeof_bool = sizeof(bool); @@ -69,7 +50,9 @@ bool cppcheck::Platform::platform(cppcheck::Platform::PlatformType type) sizeof_wchar_t = sizeof(wchar_t); sizeof_size_t = sizeof(std::size_t); sizeof_pointer = sizeof(void *); - { + if (type == Unspecified) { + defaultSign = '\0'; + } else { char x = -1; defaultSign = (x < 0) ? 's' : 'u'; } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ab96a34b1..575ebba70 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -643,7 +643,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti type->str() == "long" ? settings->sizeof_long : 0; } ValueFlow::Value value(size); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok), value, settings); setTokenValue(const_cast(tok->next()), value, settings); } else if (tok2->type() && tok2->type()->isEnumType()) { @@ -659,7 +660,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti } } ValueFlow::Value value(size); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok), value, settings); setTokenValue(const_cast(tok->next()), value, settings); } 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) && (Token::Match(sz2, "* %varid% )", varid1) || Token::Match(sz2, "%varid% [ 0 ] )", varid1))) { ValueFlow::Value value(sz1->variable()->dimension(0)); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->tokAt(4)), value, settings); } } else if (!tok2->type()) { const ValueType &vt = ValueType::parseDecl(tok2,settings); if (vt.pointer) { ValueFlow::Value value(settings->sizeof_pointer); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::CHAR) { ValueFlow::Value value(1); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::SHORT) { ValueFlow::Value value(settings->sizeof_short); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::INT) { ValueFlow::Value value(settings->sizeof_int); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::LONG) { ValueFlow::Value value(settings->sizeof_long); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::LONGLONG) { ValueFlow::Value value(settings->sizeof_long_long); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::FLOAT) { ValueFlow::Value value(settings->sizeof_float); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } else if (vt.type == ValueType::Type::DOUBLE) { ValueFlow::Value value(settings->sizeof_double); - value.setKnown(); + if (settings->platformType != cppcheck::Platform::Unspecified) + value.setKnown(); setTokenValue(const_cast(tok->next()), value, settings); } }