Verification; Verify that function call argument values meet annotations

This commit is contained in:
Daniel Marjamäki 2019-12-25 09:23:07 +01:00
parent f0ac19514b
commit 0cd2935dc7
2 changed files with 35 additions and 3 deletions

View File

@ -1601,9 +1601,41 @@ void ExprEngine::runChecks(ErrorLogger *errorLogger, const Tokenizer *tokenizer,
parent = parent->astParent();
++num;
}
if (!parent || parent->str() != "(")
if (!parent || parent->str() != "(" || num == 0)
return;
if (parent->astOperand1() && parent->astOperand1()->function()) {
const Variable *arg = parent->astOperand1()->function()->getArgumentVar(num - 1);
if (arg->nameToken()) {
std::string bad;
MathLib::bigint low;
if (arg->nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, &low)) {
if (value.isLessThan(dataBase, low)) {
bad = "__cppcheck_low_(" + std::to_string(low) + ")";
}
}
MathLib::bigint high;
if (arg->nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, &high)) {
if (value.isLessThan(dataBase, low)) {
bad = "__cppcheck_low_(" + std::to_string(low) + ")";
}
}
if (!bad.empty()) {
dataBase->addError(tok->linenr());
std::list<const Token*> callstack{tok};
ErrorLogger::ErrorMessage errmsg(callstack,
&tokenizer->list,
Severity::SeverityType::error,
"verificationInvalidArgValue",
"There is function call, cannot determine that " + std::to_string(num) + getOrdinalText(num) + " argument value meets the attribute " + bad, CWE(0), false);
errorLogger->reportErr(errmsg);
}
}
}
// Check invalid function argument values..
for (const Library::InvalidArgValue &invalidArgValue : Library::getInvalidArgValues(settings->library.validarg(parent->astOperand1(), num))) {
bool err = false;
@ -1640,7 +1672,7 @@ void ExprEngine::runChecks(ErrorLogger *errorLogger, const Tokenizer *tokenizer,
if (err) {
dataBase->addError(tok->linenr());
std::list<const Token*> callstack{tok};
ErrorLogger::ErrorMessage errmsg(callstack, &tokenizer->list, Severity::SeverityType::error, "verificationInvalidArgValue", "There is function call, cannot determine that argument value is valid. Bad value: " + bad, CWE(0), false);
ErrorLogger::ErrorMessage errmsg(callstack, &tokenizer->list, Severity::SeverityType::error, "verificationInvalidArgValue", "There is function call, cannot determine that " + std::to_string(num) + getOrdinalText(num) + " argument value is valid. Bad value: " + bad, CWE(0), false);
errorLogger->reportErr(errmsg);
break;
}

View File

@ -253,7 +253,7 @@ namespace ExprEngine {
bool isEqual(DataBase *dataBase, int value) const override;
bool isGreaterThan(DataBase *dataBase, int value) const override;
virtual bool isLessThan(DataBase *dataBase, int value) const override;
bool isLessThan(DataBase *dataBase, int value) const override;
std::string getExpr(DataBase *dataBase) const;