| 1964 | } |
| 1965 | |
| 1966 | void CheckConditionImpl::checkCompareValueOutOfTypeRange() |
| 1967 | { |
| 1968 | if (!mSettings.severity.isEnabled(Severity::style) && !mSettings.isPremiumEnabled("compareValueOutOfTypeRangeError")) |
| 1969 | return; |
| 1970 | |
| 1971 | if (mSettings.platform.type == Platform::Type::Unspecified) |
| 1972 | return; |
| 1973 | |
| 1974 | logChecker("CheckCondition::checkCompareValueOutOfTypeRange"); // style,platform |
| 1975 | |
| 1976 | const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); |
| 1977 | for (const Scope * scope : symbolDatabase->functionScopes) { |
| 1978 | for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { |
| 1979 | if (!tok->isComparisonOp() || !tok->isBinaryOp()) |
| 1980 | continue; |
| 1981 | |
| 1982 | for (int i = 0; i < 2; ++i) { |
| 1983 | const Token * const valueTok = (i == 0) ? tok->astOperand1() : tok->astOperand2(); |
| 1984 | const Token * const typeTok = valueTok->astSibling(); |
| 1985 | if (!valueTok->hasKnownIntValue() || !typeTok->valueType() || typeTok->valueType()->pointer) |
| 1986 | continue; |
| 1987 | if (valueTok->getKnownIntValue() < 0 && valueTok->valueType() && valueTok->valueType()->sign != ValueType::Sign::SIGNED) |
| 1988 | continue; |
| 1989 | if (typeTok->isLiteral()) |
| 1990 | continue; |
| 1991 | std::uint8_t bits = 0; |
| 1992 | switch (typeTok->valueType()->type) { |
| 1993 | case ValueType::Type::BOOL: |
| 1994 | bits = 1; |
| 1995 | break; |
| 1996 | case ValueType::Type::CHAR: |
| 1997 | bits = mSettings.platform.char_bit; |
| 1998 | break; |
| 1999 | case ValueType::Type::SHORT: |
| 2000 | bits = mSettings.platform.short_bit; |
| 2001 | break; |
| 2002 | case ValueType::Type::INT: |
| 2003 | bits = mSettings.platform.int_bit; |
| 2004 | break; |
| 2005 | case ValueType::Type::LONG: |
| 2006 | bits = mSettings.platform.long_bit; |
| 2007 | break; |
| 2008 | case ValueType::Type::LONGLONG: |
| 2009 | bits = mSettings.platform.long_long_bit; |
| 2010 | break; |
| 2011 | default: |
| 2012 | break; |
| 2013 | } |
| 2014 | if (bits == 0 || bits >= 63) |
| 2015 | continue; |
| 2016 | |
| 2017 | const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::UNSIGNED) ? 0 : (-(1LL << (bits-1))); |
| 2018 | const auto unsignedTypeMaxValue = (1LL << bits) - 1LL; |
| 2019 | long long typeMaxValue; |
| 2020 | if (typeTok->valueType()->sign != ValueType::Sign::SIGNED) |
| 2021 | typeMaxValue = unsignedTypeMaxValue; |
| 2022 | else if (bits >= mSettings.platform.int_bit && (!valueTok->valueType() || valueTok->valueType()->sign != ValueType::Sign::SIGNED)) |
| 2023 | typeMaxValue = unsignedTypeMaxValue; |
no test coverage detected