From 19af9bc216ef8c82f488dbd54a6bc03e04d8e7b4 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Fri, 3 Nov 2017 16:05:23 +0300 Subject: [PATCH] Fix UB when converting float to int (#984) --- lib/valueflow.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 245ca3413..56d028e6e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -263,7 +263,11 @@ static ValueFlow::Value castValue(ValueFlow::Value value, const ValueType::Sign { if (value.isFloatValue()) { value.valueType = ValueFlow::Value::INT; - value.intvalue = value.floatValue; + if (value.floatValue >= std::numeric_limits::min() && value.floatValue <= std::numeric_limits::max()) { + value.intvalue = value.floatValue; + } else { // don't perform UB + value.intvalue = 0; + } } if (bit < MathLib::bigint_bits) { const MathLib::biguint one = 1;