diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 73319f8a6..0b2612b21 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1037,8 +1037,8 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco checkMemsetType(start, tok, typeScope, allocation, parsedTypes); // check for float - else if (var->isFloatingType() && _settings->isEnabled("portability")) - memsetErrorFloat(tok, tok->str(), type->classDef->str()); + else if (tok->str() == "memset" && var->isFloatingType() && _settings->isEnabled("portability")) + memsetErrorFloat(tok, type->classDef->str()); } } } @@ -1079,10 +1079,10 @@ void CheckClass::memsetErrorReference(const Token *tok, const std::string &memfu reportError(tok, Severity::error, "memsetClassReference", "Using '" + memfunc + "' on " + type + " that contains a reference."); } -void CheckClass::memsetErrorFloat(const Token *tok, const std::string &memfunc, const std::string &type) +void CheckClass::memsetErrorFloat(const Token *tok, const std::string &type) { - reportError(tok, Severity::portability, "memsetClassFloat", "Using '" + memfunc + "' on " + type + " which contains a floating point number.\n" - "Using '" + memfunc + "' on " + type + " which contains a floating point number. This is not portable because memset() sets each byte of a block of memory to a specific value and" + reportError(tok, Severity::portability, "memsetClassFloat", "Using memset() on " + type + " which contains a floating point number.\n" + "Using memset() on " + type + " which contains a floating point number. This is not portable because memset() sets each byte of a block of memory to a specific value and" " the actual representation of a floating-point value is implementation defined."); } diff --git a/lib/checkclass.h b/lib/checkclass.h index 0266ba0f0..6e995ae46 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -144,7 +144,7 @@ private: void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname); void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type); void memsetErrorReference(const Token *tok, const std::string &memfunc, const std::string &type); - void memsetErrorFloat(const Token *tok, const std::string &memfunc, const std::string &type); + void memsetErrorFloat(const Token *tok, const std::string &type); void mallocOnClassError(const Token* tok, const std::string &memfunc, const Token* classTok, const std::string &classname); void mallocOnClassWarning(const Token* tok, const std::string &memfunc, const Token* classTok); void operatorEqReturnError(const Token *tok, const std::string &className); @@ -171,7 +171,7 @@ private: c.unusedPrivateFunctionError(0, "classname", "funcname"); c.memsetError(0, "memfunc", "classname", "class"); c.memsetErrorReference(0, "memfunc", "class"); - c.memsetErrorFloat(0, "memfunc", "class"); + c.memsetErrorFloat(0, "class"); c.mallocOnClassWarning(0, "malloc", 0); c.mallocOnClassError(0, "malloc", 0, "std::string"); c.operatorEqReturnError(0, "class"); diff --git a/test/testclass.cpp b/test/testclass.cpp index baa35f362..ff3355171 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -2481,7 +2481,7 @@ private: " A a;\n" " memset(&a, 0, sizeof(A));\n" "}", false, true); - ASSERT_EQUALS("[test.cpp:6]: (portability) Using 'memset' on struct which contains a floating point number.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:6]: (portability) Using memset() on struct which contains a floating point number.\n", errout.str()); checkNoMemset("struct A {\n" " float f[4];\n" @@ -2490,7 +2490,16 @@ private: " A a;\n" " memset(&a, 0, sizeof(A));\n" "}", false, true); - ASSERT_EQUALS("[test.cpp:6]: (portability) Using 'memset' on struct which contains a floating point number.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:6]: (portability) Using memset() on struct which contains a floating point number.\n", errout.str()); + + checkNoMemset("struct A {\n" + " float f[4];\n" + "};\n" + "void f(const A& b) {\n" + " A a;\n" + " memcpy(&a, &b, sizeof(A));\n" + "}", false, true); + ASSERT_EQUALS("", errout.str()); checkNoMemset("struct A {\n" " float* f;\n"