CheckClass: Fix wrong 'public interface' warnings

This commit is contained in:
Daniel Marjamäki 2017-10-20 22:10:55 +02:00
parent 5bf29fae49
commit 8a7411abdb
2 changed files with 14 additions and 5 deletions

View File

@ -2448,17 +2448,17 @@ void CheckClass::checkPublicInterfaceDivZero(bool test)
const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i];
if (!test && scope->classDef->fileIndex() != 1)
const Scope * classScope = symbolDatabase->classAndStructScopes[i];
if (!test && classScope->classDef->fileIndex() != 1)
continue;
std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
for (func = classScope->functionList.begin(); func != classScope->functionList.end(); ++func) {
if (func->access != AccessControl::Public)
continue;
if (!func->hasBody())
continue;
for (const Token *tok = func->functionScope->classStart; tok; tok = tok->next()) {
if (tok->str() == "if")
if (Token::Match(tok, "if|}"))
break;
if (tok->str() != "/")
continue;
@ -2468,7 +2468,7 @@ void CheckClass::checkPublicInterfaceDivZero(bool test)
continue;
const Variable *var = tok->astOperand2()->variable();
if (var && var->isArgument())
publicInterfaceDivZeroError(tok, scope->className + "::" + func->name());
publicInterfaceDivZeroError(tok, classScope->className + "::" + func->name());
}
}
}

View File

@ -6518,6 +6518,15 @@ private:
"}\n"
"void A::dostuff(int x) { int a = 1000 / x; }");
ASSERT_EQUALS("[test.cpp:5]: (warning) Arbitrary usage of public method A::dostuff() could result in division by zero.\n", errout.str());
checkPublicInterfaceDivZero("class A {\n"
"public:\n"
" void f1();\n"
" void f2(int x);\n"
"}\n"
"void A::f1() {}\n"
"void A::f2(int x) { int a = 1000 / x; }");
ASSERT_EQUALS("[test.cpp:7]: (warning) Arbitrary usage of public method A::f2() could result in division by zero.\n", errout.str());
}
};