diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 3c86bddd7..34c44d213 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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::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()); } } } diff --git a/test/testclass.cpp b/test/testclass.cpp index f1c08eeea..82e70b33e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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()); } };