From 8a7411abdb641c32622bbaf42b64d1f2804389e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Oct 2017 22:10:55 +0200 Subject: [PATCH] CheckClass: Fix wrong 'public interface' warnings --- lib/checkclass.cpp | 10 +++++----- test/testclass.cpp | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) 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()); } };