From 15d814e609d5a7dfb309ca4ae1cbeabeda6ad777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 21 Oct 2017 13:00:52 +0200 Subject: [PATCH] classPublicInterfaceDivZero: Try to make the error message a bit better. Added variable name and what the bad input value is. --- lib/checkclass.cpp | 11 +++++++---- lib/checkclass.h | 4 ++-- test/testclass.cpp | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 4c312909d..6b5a7cea3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2469,14 +2469,17 @@ void CheckClass::checkPublicInterfaceDivZero(bool test) if (!tok->astOperand2()) continue; const Variable *var = tok->astOperand2()->variable(); - if (var && var->isArgument()) - publicInterfaceDivZeroError(tok, classScope->className + "::" + func->name()); + if (!var || !var->isArgument()) + continue; + publicInterfaceDivZeroError(tok, classScope->className, func->name(), var->name()); + break; } } } } -void CheckClass::publicInterfaceDivZeroError(const Token *tok, const std::string &functionName) +void CheckClass::publicInterfaceDivZeroError(const Token *tok, const std::string &className, const std::string &methodName, const std::string &varName) { - reportError(tok, Severity::warning, "classPublicInterfaceDivZero", "Arbitrary usage of public method " + functionName + "() could result in division by zero."); + const std::string s = className + "::" + methodName + "()"; + 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."); } diff --git a/lib/checkclass.h b/lib/checkclass.h index 8c8746513..0633ab6f0 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -187,7 +187,7 @@ private: void callsPureVirtualFunctionError(const Function & scopeFunction, const std::list & tokStack, const std::string &purefuncname); void duplInheritedMembersError(const Token* tok1, const Token* tok2, const std::string &derivedname, const std::string &basename, const std::string &variablename, bool derivedIsStruct, bool baseIsStruct); void copyCtorAndEqOperatorError(const Token *tok, const std::string &classname, bool isStruct, bool hasCopyCtor); - void publicInterfaceDivZeroError(const Token *tok, const std::string &functionName); + void publicInterfaceDivZeroError(const Token *tok, const std::string &className, const std::string &methodName, const std::string &varName); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { CheckClass c(nullptr, settings, errorLogger); @@ -218,7 +218,7 @@ private: c.selfInitializationError(nullptr, "var"); c.duplInheritedMembersError(nullptr, nullptr, "class", "class", "variable", false, false); c.copyCtorAndEqOperatorError(nullptr, "class", false, false); - c.publicInterfaceDivZeroError(nullptr, "Class::dostuff"); + c.publicInterfaceDivZeroError(nullptr, "Class", "dostuff", "x"); } static std::string myName() { diff --git a/test/testclass.cpp b/test/testclass.cpp index 3495ee7e6..2e1efafb9 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6517,7 +6517,7 @@ private: " void dostuff(int x);\n" "}\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()); + 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()); checkPublicInterfaceDivZero("class A {\n" "public:\n" @@ -6526,7 +6526,7 @@ private: "}\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()); + 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()); checkPublicInterfaceDivZero("class A {\n" "public:\n"