Skip to content

Commit 15d814e

Browse files
committed
classPublicInterfaceDivZero: Try to make the error message a bit better. Added variable name and what the bad input value is.
1 parent 7a67bce commit 15d814e

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lib/checkclass.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,14 +2469,17 @@ void CheckClass::checkPublicInterfaceDivZero(bool test)
24692469
if (!tok->astOperand2())
24702470
continue;
24712471
const Variable *var = tok->astOperand2()->variable();
2472-
if (var && var->isArgument())
2473-
publicInterfaceDivZeroError(tok, classScope->className + "::" + func->name());
2472+
if (!var || !var->isArgument())
2473+
continue;
2474+
publicInterfaceDivZeroError(tok, classScope->className, func->name(), var->name());
2475+
break;
24742476
}
24752477
}
24762478
}
24772479
}
24782480

2479-
void CheckClass::publicInterfaceDivZeroError(const Token *tok, const std::string &functionName)
2481+
void CheckClass::publicInterfaceDivZeroError(const Token *tok, const std::string &className, const std::string &methodName, const std::string &varName)
24802482
{
2481-
reportError(tok, Severity::warning, "classPublicInterfaceDivZero", "Arbitrary usage of public method " + functionName + "() could result in division by zero.");
2483+
const std::string s = className + "::" + methodName + "()";
2484+
reportError(tok, Severity::warning, "classPublicInterfaceDivZero", "Public interface of " + className + " is not safe. When calling " + s + ", if parameter " + varName + " is 0 that leads to division by zero.");
24822485
}

lib/checkclass.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class CPPCHECKLIB CheckClass : public Check {
187187
void callsPureVirtualFunctionError(const Function & scopeFunction, const std::list<const Token *> & tokStack, const std::string &purefuncname);
188188
void duplInheritedMembersError(const Token* tok1, const Token* tok2, const std::string &derivedname, const std::string &basename, const std::string &variablename, bool derivedIsStruct, bool baseIsStruct);
189189
void copyCtorAndEqOperatorError(const Token *tok, const std::string &classname, bool isStruct, bool hasCopyCtor);
190-
void publicInterfaceDivZeroError(const Token *tok, const std::string &functionName);
190+
void publicInterfaceDivZeroError(const Token *tok, const std::string &className, const std::string &methodName, const std::string &varName);
191191

192192
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
193193
CheckClass c(nullptr, settings, errorLogger);
@@ -218,7 +218,7 @@ class CPPCHECKLIB CheckClass : public Check {
218218
c.selfInitializationError(nullptr, "var");
219219
c.duplInheritedMembersError(nullptr, nullptr, "class", "class", "variable", false, false);
220220
c.copyCtorAndEqOperatorError(nullptr, "class", false, false);
221-
c.publicInterfaceDivZeroError(nullptr, "Class::dostuff");
221+
c.publicInterfaceDivZeroError(nullptr, "Class", "dostuff", "x");
222222
}
223223

224224
static std::string myName() {

test/testclass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6517,7 +6517,7 @@ class TestClass : public TestFixture {
65176517
" void dostuff(int x);\n"
65186518
"}\n"
65196519
"void A::dostuff(int x) { int a = 1000 / x; }");
6520-
ASSERT_EQUALS("[test.cpp:5]: (warning) Arbitrary usage of public method A::dostuff() could result in division by zero.\n", errout.str());
6520+
ASSERT_EQUALS("[test.cpp:5]: (warning) Public interface of A is not safe. When calling A::dostuff(), if parameter x is 0 that leads to division by zero.\n", errout.str());
65216521

65226522
checkPublicInterfaceDivZero("class A {\n"
65236523
"public:\n"
@@ -6526,7 +6526,7 @@ class TestClass : public TestFixture {
65266526
"}\n"
65276527
"void A::f1() {}\n"
65286528
"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());
6529+
ASSERT_EQUALS("[test.cpp:7]: (warning) Public interface of A is not safe. When calling A::f2(), if parameter x is 0 that leads to division by zero.\n", errout.str());
65306530

65316531
checkPublicInterfaceDivZero("class A {\n"
65326532
"public:\n"

0 commit comments

Comments
 (0)