Skip to content

Commit cab9f61

Browse files
committed
safe checks: Handle float parameters
1 parent 2be981d commit cab9f61

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5452,10 +5452,10 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold
54525452
const bool all = safe && settings->platformType != cppcheck::Platform::PlatformType::Unspecified;
54535453

54545454
for (const Variable &arg : function->argumentList) {
5455-
if (!arg.nameToken())
5455+
if (!arg.nameToken() || !arg.valueType())
54565456
continue;
54575457

5458-
if (arg.valueType() && arg.valueType()->type == ValueType::Type::CONTAINER) {
5458+
if (arg.valueType()->type == ValueType::Type::CONTAINER) {
54595459
if (!safe)
54605460
continue;
54615461
std::list<ValueFlow::Value> argValues;
@@ -5485,6 +5485,27 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold
54855485
if (!isHigh)
54865486
high = maxValue;
54875487
isLow = isHigh = true;
5488+
} else if (arg.valueType()->type == ValueType::Type::FLOAT || arg.valueType()->type == ValueType::Type::DOUBLE || arg.valueType()->type == ValueType::Type::LONGDOUBLE) {
5489+
std::list<ValueFlow::Value> argValues;
5490+
argValues.emplace_back(0);
5491+
argValues.back().valueType = ValueFlow::Value::ValueType::FLOAT;
5492+
argValues.back().floatValue = isLow ? low : -1E25f;
5493+
argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming argument has value " + MathLib::toString(argValues.back().floatValue));
5494+
argValues.emplace_back(1);
5495+
argValues.back().valueType = ValueFlow::Value::ValueType::FLOAT;
5496+
argValues.back().floatValue = isHigh ? high : 1E25f;
5497+
argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming argument has value " + MathLib::toString(argValues.back().floatValue));
5498+
valueFlowForward(const_cast<Token *>(functionScope->bodyStart->next()),
5499+
functionScope->bodyEnd,
5500+
&arg,
5501+
arg.declarationId(),
5502+
argValues,
5503+
false,
5504+
false,
5505+
tokenlist,
5506+
errorLogger,
5507+
settings);
5508+
continue;
54885509
}
54895510
}
54905511

‎test/testvalueflow.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3929,6 +3929,14 @@ class TestValueFlow : public TestFixture {
39293929
ASSERT_EQUALS(0, values.front().intvalue);
39303930
ASSERT_EQUALS(1000000, values.back().intvalue);
39313931

3932+
code = "int f(float x) {\n"
3933+
" return x;\n"
3934+
"}";
3935+
values = tokenValues(code, "x ;", &s);
3936+
ASSERT_EQUALS(2, values.size());
3937+
ASSERT(values.front().floatValue < -1E20);
3938+
ASSERT(values.back().floatValue > 1E20);
3939+
39323940
code = "short f(__cppcheck_in_range__(0,100) short x) {\n"
39333941
" return x + 0;\n"
39343942
"}";

0 commit comments

Comments
 (0)