From 4f1c37d71f800500cd4e719fb2d61631d5c56336 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 2 Jun 2023 11:47:33 +0200 Subject: [PATCH 1/5] Fix #9104 reopened Improve check: missing virtual destructor --- cfg/std.cfg | 2 +- lib/library.cpp | 2 +- test/testclass.cpp | 21 +++++++++++++++++++++ test/testfunctions.cpp | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index 9760afb7c1f..43e59ab4ed4 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -6707,7 +6707,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun false - + false diff --git a/lib/library.cpp b/lib/library.cpp index 028945a02a3..60754cad87b 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1250,7 +1250,7 @@ bool Library::matchArguments(const Token *ftok, const std::string &functionName) const int callargs = numberOfArgumentsWithoutAst(ftok); const std::unordered_map::const_iterator it = functions.find(functionName); if (it == functions.cend()) - return (callargs == 0); + return false; int args = 0; int firstOptionalArg = -1; for (const std::pair & argCheck : it->second.argumentChecks) { diff --git a/test/testclass.cpp b/test/testclass.cpp index 6c12a4998a7..7be1486d675 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -2630,6 +2630,27 @@ class TestClass : public TestFixture { " delete p;\n" "}"); ASSERT_EQUALS("[test.cpp:7]: (error) Class 'Base' which is inherited by class 'Derived' does not have a virtual destructor.\n", errout.str()); + + checkVirtualDestructor("using namespace std;\n" + "struct A\n" + "{\n" + " A() { cout << \"A is constructing\\n\"; }\n" + " ~A() { cout << \"A is destructing\\n\"; }\n" + "};\n" + " \n" + "struct Base {};\n" + " \n" + "struct Derived : Base\n" + "{\n" + " A a;\n" + "};\n" + " \n" + "int main(void)\n" + "{\n" + " Base* p = new Derived();\n" + " delete p;\n" + "}"); + ASSERT_EQUALS("[test.cpp:8]: (error) Class 'Base' which is inherited by class 'Derived' does not have a virtual destructor.\n", errout.str()); } void virtualDestructor3() { diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 427194eacfe..d6475518199 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1823,7 +1823,7 @@ class TestFunctions : public TestFixture { check("void f() {\n" " lib_func();" "}", "test.cpp", &s); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout.str()); check("void f(void* v) {\n" " lib_func(v);" From 9f30f58f7ab50f393af8260e3a9b1ad4eb349145 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 2 Jun 2023 12:27:45 +0200 Subject: [PATCH 2/5] Add support for missing container members --- cfg/std.cfg | 16 ++++++++++++---- test/cfg/std.cpp | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index 43e59ab4ed4..e49db0da29e 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -6609,7 +6609,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun - + @@ -6682,7 +6682,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun - + @@ -6703,11 +6703,19 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun - + false - + + + false + + + + false + + false diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 7b8e6b02b84..5fcd61ed713 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4634,10 +4634,11 @@ void stdspan() // cppcheck-suppress unreadVariable std::span spn2 = spn; + //cppcheck-suppress ignoredReturnValue spn.begin(); + //cppcheck-suppress ignoredReturnValue spn.end(); spn.rbegin(); - spn.end(); spn.front(); spn.back(); From 89b9fe615c0acb97f81b7a0a6180f3101cdd32f7 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 2 Jun 2023 12:53:46 +0200 Subject: [PATCH 3/5] Add more stuff --- cfg/std.cfg | 20 +++++++++++++++++++- test/cfg/std.cpp | 5 +++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index e49db0da29e..9dc5e02315a 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -6682,7 +6682,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun - + @@ -6719,6 +6719,10 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun false + + + false + false @@ -6764,6 +6768,14 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + + + + + + + false + @@ -7360,6 +7372,12 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + + + false + + + diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 5fcd61ed713..fca7be269ab 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4638,15 +4638,20 @@ void stdspan() spn.begin(); //cppcheck-suppress ignoredReturnValue spn.end(); + //cppcheck-suppress ignoredReturnValue spn.rbegin(); + //cppcheck-suppress ignoredReturnValue spn.front(); + //cppcheck-suppress ignoredReturnValue spn.back(); //cppcheck-suppress constStatement spn[0]; spn.data(); + //cppcheck-suppress ignoredReturnValue spn.size(); spn.size_bytes(); + //cppcheck-suppress ignoredReturnValue spn.empty(); //cppcheck-suppress ignoredReturnValue spn.first(2); From 9d749b3c0c4395fc249aeeca28ec31b6ed11c50d Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 2 Jun 2023 13:03:10 +0200 Subject: [PATCH 4/5] Improve qt.cfg --- cfg/qt.cfg | 15 +++++++++++++-- cfg/std.cfg | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cfg/qt.cfg b/cfg/qt.cfg index b6aa70ea7a7..0cd395ffc8b 100644 --- a/cfg/qt.cfg +++ b/cfg/qt.cfg @@ -885,11 +885,21 @@ + + + false + + false + + + false + + false @@ -935,7 +945,7 @@ - + false @@ -1132,7 +1142,8 @@ - + + false diff --git a/cfg/std.cfg b/cfg/std.cfg index 9dc5e02315a..96dce98b728 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -8396,7 +8396,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init - + false From e7b1709cbaf6db445bcd614ceb2f226dcd75b0cf Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 2 Jun 2023 14:47:28 +0200 Subject: [PATCH 5/5] Improve wxwidgets.cfg, qt.cfg --- cfg/qt.cfg | 13 ++++++++++++- cfg/wxwidgets.cfg | 20 ++++++++++++++++++-- test/cfg/openssl.c | 1 + test/cfg/qt.cpp | 1 + test/cfg/std.cpp | 3 +++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/cfg/qt.cfg b/cfg/qt.cfg index 0cd395ffc8b..b7c29297ddc 100644 --- a/cfg/qt.cfg +++ b/cfg/qt.cfg @@ -910,6 +910,11 @@ false + + + false + + false @@ -4086,10 +4091,16 @@ - + false + + + false + + + false diff --git a/cfg/wxwidgets.cfg b/cfg/wxwidgets.cfg index 3723bd360e1..b8f825088d2 100644 --- a/cfg/wxwidgets.cfg +++ b/cfg/wxwidgets.cfg @@ -5485,7 +5485,7 @@ - + false @@ -8953,6 +8953,22 @@ + + + false + + + + + false + + + + + + + false + false @@ -14219,7 +14235,7 @@ wxItemKind kind = wxITEM_NORMAL) --> - + diff --git a/test/cfg/openssl.c b/test/cfg/openssl.c index 9c12cd37100..99b467cd1ca 100644 --- a/test/cfg/openssl.c +++ b/test/cfg/openssl.c @@ -28,6 +28,7 @@ int valid_code_do_crypt(char *outfile) FILE *out; ctx = EVP_CIPHER_CTX_new(); + // cppcheck-suppress checkLibraryFunction EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv); if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) { diff --git a/test/cfg/qt.cpp b/test/cfg/qt.cpp index eeeb1659daa..584d1015a38 100644 --- a/test/cfg/qt.cpp +++ b/test/cfg/qt.cpp @@ -441,6 +441,7 @@ void validCode(int * pIntPtr, QString & qstrArg, double d) printf(QT_TR_NOOP("Hi")); + // cppcheck-suppress checkLibraryFunction Q_DECLARE_LOGGING_CATEGORY(logging_category_test); QT_FORWARD_DECLARE_CLASS(forwardDeclaredClass); QT_FORWARD_DECLARE_STRUCT(forwardDeclaredStruct); diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index fca7be269ab..c7210bd0d9d 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4489,6 +4489,7 @@ void getline() in.close(); } +// cppcheck-suppress passedByValue void stream_write(std::ofstream& s, std::vector v) { if (v.empty()) {} s.write(v.data(), v.size()); @@ -4647,9 +4648,11 @@ void stdspan() spn.back(); //cppcheck-suppress constStatement spn[0]; + //cppcheck-suppress ignoredReturnValue spn.data(); //cppcheck-suppress ignoredReturnValue spn.size(); + //cppcheck-suppress ignoredReturnValue spn.size_bytes(); //cppcheck-suppress ignoredReturnValue spn.empty();