Safe checks: container parameters
This commit is contained in:
parent
377f6f29b5
commit
10be2a1941
|
@ -5448,12 +5448,28 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold
|
|||
if (!function)
|
||||
continue;
|
||||
|
||||
const bool all = function->isSafe(settings) && settings->platformType != cppcheck::Platform::PlatformType::Unspecified;
|
||||
const bool safe = function->isSafe(settings);
|
||||
const bool all = safe && settings->platformType != cppcheck::Platform::PlatformType::Unspecified;
|
||||
|
||||
for (const Variable &arg : function->argumentList) {
|
||||
if (!arg.nameToken())
|
||||
continue;
|
||||
|
||||
if (arg.nameToken()->valueType() && arg.nameToken()->valueType()->type == ValueType::Type::CONTAINER) {
|
||||
if (!safe)
|
||||
continue;
|
||||
std::list<ValueFlow::Value> argValues;
|
||||
argValues.emplace_back(0);
|
||||
argValues.back().valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
|
||||
argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming " + arg.name() + " is empty");
|
||||
argValues.emplace_back(1000000);
|
||||
argValues.back().valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
|
||||
argValues.back().errorPath.emplace_back(arg.nameToken(), "Assuming " + arg.name() + " size is 1000000");
|
||||
for (const ValueFlow::Value &value : argValues)
|
||||
valueFlowContainerForward(const_cast<Token*>(functionScope->bodyStart), arg.declarationId(), value, settings, tokenlist->isCPP());
|
||||
continue;
|
||||
}
|
||||
|
||||
MathLib::bigint low, high;
|
||||
bool isLow = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, &low);
|
||||
bool isHigh = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, &high);
|
||||
|
|
|
@ -3910,6 +3910,7 @@ private:
|
|||
const char *code;
|
||||
std::list<ValueFlow::Value> values;
|
||||
Settings s;
|
||||
LOAD_LIB_2(s.library, "std.cfg");
|
||||
s.safeChecks.classes = s.safeChecks.externalFunctions = s.safeChecks.internalFunctions = true;
|
||||
|
||||
code = "short f(short x) {\n"
|
||||
|
@ -3920,6 +3921,14 @@ private:
|
|||
ASSERT_EQUALS(-0x8000, values.front().intvalue);
|
||||
ASSERT_EQUALS(0x7fff, values.back().intvalue);
|
||||
|
||||
code = "short f(std::string x) {\n"
|
||||
" return x[10];\n"
|
||||
"}";
|
||||
values = tokenValues(code, "x [", &s);
|
||||
ASSERT_EQUALS(2, values.size());
|
||||
ASSERT_EQUALS(0, values.front().intvalue);
|
||||
ASSERT_EQUALS(1000000, values.back().intvalue);
|
||||
|
||||
code = "short f(__cppcheck_in_range__(0,100) short x) {\n"
|
||||
" return x + 0;\n"
|
||||
"}";
|
||||
|
|
Loading…
Reference in New Issue