Fix #11098 FP compare with negated constant (#4282)

This commit is contained in:
chrchr-github 2022-07-16 07:28:15 +02:00 committed by GitHub
parent 71d386819e
commit 9e74da6126
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -1150,12 +1150,22 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings *settings)
return 0;
}
static bool getMinMaxValues(const ValueType* vt, const cppcheck::Platform& platform, MathLib::bigint* minValue, MathLib::bigint* maxValue);
// Handle various constants..
static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp)
{
if ((tok->isNumber() && MathLib::isInt(tok->str())) || (tok->tokType() == Token::eChar)) {
try {
ValueFlow::Value value(MathLib::toLongNumber(tok->str()));
MathLib::bigint signedValue = MathLib::toLongNumber(tok->str());
const ValueType* vt = tok->valueType();
if (vt && vt->sign == ValueType::UNSIGNED && signedValue < 0 && ValueFlow::getSizeOf(*vt, settings) < sizeof(MathLib::bigint)) {
MathLib::bigint minValue{}, maxValue{};
if (getMinMaxValues(tok->valueType(), *settings, &minValue, &maxValue))
signedValue += maxValue + 1;
}
ValueFlow::Value value(signedValue);
if (!tok->isTemplateArg())
value.setKnown();
setTokenValue(tok, value, settings);
@ -8303,7 +8313,7 @@ static void valueFlowDynamicBufferSize(TokenList* tokenlist, SymbolDatabase* sym
}
}
static bool getMinMaxValues(const ValueType *vt, const cppcheck::Platform &platform, MathLib::bigint *minValue, MathLib::bigint *maxValue)
bool getMinMaxValues(const ValueType *vt, const cppcheck::Platform &platform, MathLib::bigint *minValue, MathLib::bigint *maxValue)
{
if (!vt || !vt->isIntegral() || vt->pointer)
return false;

View File

@ -4581,6 +4581,10 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #11098
check("void f(unsigned int x) { if (x == -1u) {} }\n");
ASSERT_EQUALS("", errout.str());
}
void alwaysTrueContainer() {