safe checks: Handle float parameters

This commit is contained in:
Daniel Marjamäki 2019-07-24 12:08:56 +02:00
parent 2be981d68d
commit cab9f61b79
2 changed files with 31 additions and 2 deletions

View File

@ -5452,10 +5452,10 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold
const bool all = safe && settings->platformType != cppcheck::Platform::PlatformType::Unspecified; const bool all = safe && settings->platformType != cppcheck::Platform::PlatformType::Unspecified;
for (const Variable &arg : function->argumentList) { for (const Variable &arg : function->argumentList) {
if (!arg.nameToken()) if (!arg.nameToken() || !arg.valueType())
continue; continue;
if (arg.valueType() && arg.valueType()->type == ValueType::Type::CONTAINER) { if (arg.valueType()->type == ValueType::Type::CONTAINER) {
if (!safe) if (!safe)
continue; continue;
std::list<ValueFlow::Value> argValues; std::list<ValueFlow::Value> argValues;
@ -5485,6 +5485,27 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold
if (!isHigh) if (!isHigh)
high = maxValue; high = maxValue;
isLow = isHigh = true; 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<ValueFlow::Value> 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<Token *>(functionScope->bodyStart->next()),
functionScope->bodyEnd,
&arg,
arg.declarationId(),
argValues,
false,
false,
tokenlist,
errorLogger,
settings);
continue;
} }
} }

View File

@ -3929,6 +3929,14 @@ private:
ASSERT_EQUALS(0, values.front().intvalue); ASSERT_EQUALS(0, values.front().intvalue);
ASSERT_EQUALS(1000000, values.back().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" code = "short f(__cppcheck_in_range__(0,100) short x) {\n"
" return x + 0;\n" " return x + 0;\n"
"}"; "}";