Skip to content

Commit 8a7411a

Browse files
committed
CheckClass: Fix wrong 'public interface' warnings
1 parent 5bf29fa commit 8a7411a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/checkclass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,17 +2448,17 @@ void CheckClass::checkPublicInterfaceDivZero(bool test)
24482448

24492449
const std::size_t classes = symbolDatabase->classAndStructScopes.size();
24502450
for (std::size_t i = 0; i < classes; ++i) {
2451-
const Scope * scope = symbolDatabase->classAndStructScopes[i];
2452-
if (!test && scope->classDef->fileIndex() != 1)
2451+
const Scope * classScope = symbolDatabase->classAndStructScopes[i];
2452+
if (!test && classScope->classDef->fileIndex() != 1)
24532453
continue;
24542454
std::list<Function>::const_iterator func;
2455-
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
2455+
for (func = classScope->functionList.begin(); func != classScope->functionList.end(); ++func) {
24562456
if (func->access != AccessControl::Public)
24572457
continue;
24582458
if (!func->hasBody())
24592459
continue;
24602460
for (const Token *tok = func->functionScope->classStart; tok; tok = tok->next()) {
2461-
if (tok->str() == "if")
2461+
if (Token::Match(tok, "if|}"))
24622462
break;
24632463
if (tok->str() != "/")
24642464
continue;
@@ -2468,7 +2468,7 @@ void CheckClass::checkPublicInterfaceDivZero(bool test)
24682468
continue;
24692469
const Variable *var = tok->astOperand2()->variable();
24702470
if (var && var->isArgument())
2471-
publicInterfaceDivZeroError(tok, scope->className + "::" + func->name());
2471+
publicInterfaceDivZeroError(tok, classScope->className + "::" + func->name());
24722472
}
24732473
}
24742474
}

test/testclass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6518,6 +6518,15 @@ class TestClass : public TestFixture {
65186518
"}\n"
65196519
"void A::dostuff(int x) { int a = 1000 / x; }");
65206520
ASSERT_EQUALS("[test.cpp:5]: (warning) Arbitrary usage of public method A::dostuff() could result in division by zero.\n", errout.str());
6521+
6522+
checkPublicInterfaceDivZero("class A {\n"
6523+
"public:\n"
6524+
" void f1();\n"
6525+
" void f2(int x);\n"
6526+
"}\n"
6527+
"void A::f1() {}\n"
6528+
"void A::f2(int x) { int a = 1000 / x; }");
6529+
ASSERT_EQUALS("[test.cpp:7]: (warning) Arbitrary usage of public method A::f2() could result in division by zero.\n", errout.str());
65216530
}
65226531
};
65236532

0 commit comments

Comments
 (0)