From 22be5f29aabec3d6526dc3b2f17a860a5313f610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 12 Jul 2022 17:39:01 +0200 Subject: [PATCH] fixed #11183 (checkLibraryFunction with parameter to "std::string()") / added rudimentary tests for `CheckFunctions::checkLibraryMatchFunctions()` / added test for #10105 (#4265) --- lib/checkfunctions.cpp | 14 ++++++++++++- test/testfunctions.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 83329c40f..75be215ed 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -624,7 +624,19 @@ void CheckFunctions::checkLibraryMatchFunctions() continue; const std::string &functionName = mSettings->library.getFunctionName(tok); - if (functionName.empty() || mSettings->library.functions.find(functionName) != mSettings->library.functions.end()) + if (functionName.empty()) + continue; + + if (mSettings->library.functions.find(functionName) != mSettings->library.functions.end()) + continue; + + if (mSettings->library.smartPointers.find(functionName) != mSettings->library.smartPointers.end()) + continue; + + const Token* start = tok; + while (Token::Match(start->tokAt(-2), "%name% ::")) + start = start->tokAt(-2); + if (mSettings->library.detectContainerOrIterator(start)) continue; reportError(tok, diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 7690311df..6758ddff7 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -99,6 +99,8 @@ private: TEST_CASE(returnLocalStdMove5); TEST_CASE(negativeMemoryAllocationSizeError); // #389 + + TEST_CASE(checkLibraryMatchFunctions); } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) @@ -1769,6 +1771,51 @@ private: ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'alloca' called.\n" "[test.cpp:3]: (error) Invalid alloca() argument nr 1. The value is -10 but the valid values are '0:'.\n", errout.str()); } + + void checkLibraryMatchFunctions() { + settings.checkLibrary = true; + auto severity_old = settings.severity; + settings.severity.enable(Severity::information); + + check("void f() {\n" + " lib_func();" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(void* v) {\n" + " lib_func(v);" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout.str()); + + // #10105 + check("class TestFixture {\n" + "protected:\n" + " bool prepareTest(const char testname[]);\n" + "};\n" + "\n" + "class TestMemleak : private TestFixture {\n" + " void run() {\n" + " do { prepareTest(\"testFunctionReturnType\"); } while (false);\n" + " }\n" + "\n" + " void testFunctionReturnType() {\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + // #11183 + check("#include \n" + "\n" + "extern void cb(const std::string&);\n" + "\n" + "void f() {\n" + " cb(std::string(\"\"));\n" + "}"); + TODO_ASSERT_EQUALS("", "[test.cpp:6]: (information) --check-library: There is no matching configuration for function cb()\n", errout.str()); + + settings.severity = severity_old; + settings.checkLibrary = false; + } }; REGISTER_TEST(TestFunctions)