Safe checks: container parameters

This commit is contained in:
Daniel Marjamäki 2019-07-24 11:39:18 +02:00
parent 377f6f29b5
commit 10be2a1941
2 changed files with 26 additions and 1 deletions

View File

@ -5448,12 +5448,28 @@ static void valueFlowSafeFunctions(TokenList *tokenlist, SymbolDatabase *symbold
if (!function) if (!function)
continue; 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) { for (const Variable &arg : function->argumentList) {
if (!arg.nameToken()) if (!arg.nameToken())
continue; 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; MathLib::bigint low, high;
bool isLow = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, &low); bool isLow = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, &low);
bool isHigh = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, &high); bool isHigh = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, &high);

View File

@ -3910,6 +3910,7 @@ private:
const char *code; const char *code;
std::list<ValueFlow::Value> values; std::list<ValueFlow::Value> values;
Settings s; Settings s;
LOAD_LIB_2(s.library, "std.cfg");
s.safeChecks.classes = s.safeChecks.externalFunctions = s.safeChecks.internalFunctions = true; s.safeChecks.classes = s.safeChecks.externalFunctions = s.safeChecks.internalFunctions = true;
code = "short f(short x) {\n" code = "short f(short x) {\n"
@ -3920,6 +3921,14 @@ private:
ASSERT_EQUALS(-0x8000, values.front().intvalue); ASSERT_EQUALS(-0x8000, values.front().intvalue);
ASSERT_EQUALS(0x7fff, values.back().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" code = "short f(__cppcheck_in_range__(0,100) short x) {\n"
" return x + 0;\n" " return x + 0;\n"
"}"; "}";