Fixed #9230 (Improve check: integer conversion overflow in return)

This commit is contained in:
Daniel Marjamäki 2019-07-24 16:17:52 +02:00
parent f29e88a8a5
commit 7c0b011c05
3 changed files with 55 additions and 36 deletions

View File

@ -367,6 +367,7 @@ void CheckType::checkFloatToIntegerOverflow()
vtint = tok->valueType();
vtfloat = tok->astOperand1()->valueType();
floatValues = &tok->astOperand1()->values();
checkFloatToIntegerOverflow(tok, vtint, vtfloat, floatValues);
}
// Assignment
@ -374,18 +375,30 @@ void CheckType::checkFloatToIntegerOverflow()
vtint = tok->astOperand1()->valueType();
vtfloat = tok->astOperand2()->valueType();
floatValues = &tok->astOperand2()->values();
checkFloatToIntegerOverflow(tok, vtint, vtfloat, floatValues);
}
// TODO: function call
else
continue;
else if (tok->str() == "return" && tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->isFloat()) {
const Scope *scope = tok->scope();
while (scope && scope->type != Scope::ScopeType::eLambda && scope->type != Scope::ScopeType::eFunction)
scope = scope->nestedIn;
if (scope && scope->type == Scope::ScopeType::eFunction && scope->function && scope->function->retDef) {
const ValueType &valueType = ValueType::parseDecl(scope->function->retDef, mSettings);
vtfloat = tok->astOperand1()->valueType();
floatValues = &tok->astOperand1()->values();
checkFloatToIntegerOverflow(tok, &valueType, vtfloat, floatValues);
}
}
}
}
void CheckType::checkFloatToIntegerOverflow(const Token *tok, const ValueType *vtint, const ValueType *vtfloat, const std::list<ValueFlow::Value> *floatValues)
{
// Conversion of float to integer?
if (!vtint || !vtint->isIntegral())
continue;
return;
if (!vtfloat || !vtfloat->isFloat())
continue;
return;
for (const ValueFlow::Value &f : *floatValues) {
if (f.valueType != ValueFlow::Value::ValueType::FLOAT)
@ -414,7 +427,6 @@ void CheckType::checkFloatToIntegerOverflow()
floatToIntegerOverflowError(tok, f);
}
}
}
}
void CheckType::floatToIntegerOverflowError(const Token *tok, const ValueFlow::Value &value)

View File

@ -73,6 +73,8 @@ public:
/** @brief %Check for float to integer overflow */
void checkFloatToIntegerOverflow();
void checkFloatToIntegerOverflow(const Token *tok, const ValueType *vtint, const ValueType *vtfloat, const std::list<ValueFlow::Value> *floatValues);
private:
// Error messages..

View File

@ -318,6 +318,11 @@ private:
" char c = 1234.5;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout.str()));
check("char f(void) {\n"
" return 1234.5;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout.str()));
}
};