From cab9f61b7945c433a46ff7c1b4e5dc6852f6565e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 24 Jul 2019 12:08:56 +0200 Subject: [PATCH] safe checks: Handle float parameters --- lib/valueflow.cpp | 25 +++++++++++++++++++++++-- test/testvalueflow.cpp | 8 ++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 67f57f51e..3534e8b7c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5452,10 +5452,10 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold const bool all = safe && settings->platformType != cppcheck::Platform::PlatformType::Unspecified; for (const Variable &arg : function->argumentList) { - if (!arg.nameToken()) + if (!arg.nameToken() || !arg.valueType()) continue; - if (arg.valueType() && arg.valueType()->type == ValueType::Type::CONTAINER) { + if (arg.valueType()->type == ValueType::Type::CONTAINER) { if (!safe) continue; std::list argValues; @@ -5485,6 +5485,27 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold if (!isHigh) high = maxValue; isLow = isHigh = true; + } else if (arg.valueType()->type == ValueType::Type::FLOAT || arg.valueType()->type == ValueType::Type::DOUBLE || arg.valueType()->type == ValueType::Type::LONGDOUBLE) { + std::list argValues; + argValues.emplace_back(0); + argValues.back().valueType = ValueFlow::Value::ValueType::FLOAT; + argValues.back().floatValue = isLow ? low : -1E25f; + argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming argument has value " + MathLib::toString(argValues.back().floatValue)); + argValues.emplace_back(1); + argValues.back().valueType = ValueFlow::Value::ValueType::FLOAT; + argValues.back().floatValue = isHigh ? high : 1E25f; + argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming argument has value " + MathLib::toString(argValues.back().floatValue)); + valueFlowForward(const_cast(functionScope->bodyStart->next()), + functionScope->bodyEnd, + &arg, + arg.declarationId(), + argValues, + false, + false, + tokenlist, + errorLogger, + settings); + continue; } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index d4c3b5756..f63b715a9 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3929,6 +3929,14 @@ private: ASSERT_EQUALS(0, values.front().intvalue); ASSERT_EQUALS(1000000, values.back().intvalue); + code = "int f(float x) {\n" + " return x;\n" + "}"; + values = tokenValues(code, "x ;", &s); + ASSERT_EQUALS(2, values.size()); + ASSERT(values.front().floatValue < -1E20); + ASSERT(values.back().floatValue > 1E20); + code = "short f(__cppcheck_in_range__(0,100) short x) {\n" " return x + 0;\n" "}";