Fix UB when converting float to int (#984)

This commit is contained in:
Ayaz Salikhov 2017-11-03 16:05:23 +03:00 committed by Daniel Marjamäki
parent 4f6f1e20dd
commit 19af9bc216
1 changed files with 5 additions and 1 deletions

View File

@ -263,7 +263,11 @@ static ValueFlow::Value castValue(ValueFlow::Value value, const ValueType::Sign
{ {
if (value.isFloatValue()) { if (value.isFloatValue()) {
value.valueType = ValueFlow::Value::INT; value.valueType = ValueFlow::Value::INT;
value.intvalue = value.floatValue; if (value.floatValue >= std::numeric_limits<int>::min() && value.floatValue <= std::numeric_limits<int>::max()) {
value.intvalue = value.floatValue;
} else { // don't perform UB
value.intvalue = 0;
}
} }
if (bit < MathLib::bigint_bits) { if (bit < MathLib::bigint_bits) {
const MathLib::biguint one = 1; const MathLib::biguint one = 1;