diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index ca5998bdd6b..643355ad2c3 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -81,6 +81,7 @@ class TestCppcheck : public TestFixture { TEST_CASE(getDumpFileContentsLibrary); TEST_CASE(premiumResultsCache); TEST_CASE(toomanyconfigs); + TEST_CASE(purgedConfiguration); } void getErrorMessages() const { @@ -584,6 +585,33 @@ class TestCppcheck : public TestFixture { ASSERT_EQUALS("a.c:0:0: information: Too many #ifdef configurations - cppcheck only checks 2 of 4 configurations. Use --force to check all configurations. [toomanyconfigs]", it->toString(false, templateFormat, "")); } + void purgedConfiguration() const + { + ScopedFile test_file("test.cpp", + "#ifdef X\n" + "#endif\n" + "int main() {}\n"); + + // this is the "simple" format + const auto s = dinit(Settings, + $.templateFormat = templateFormat, // TODO: remove when we only longer rely on toString() in unique message handling + $.severity.enable (Severity::information); + $.debugwarnings = true); + Suppressions supprs; + ErrorLogger2 errorLogger; + CppCheck cppcheck(s, supprs, errorLogger, false, {}); + ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file.path(), Path::identify(test_file.path(), false), 0))); + // TODO: how to properly disable these warnings? + errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) { + return msg.id == "logChecker"; + }), errorLogger.errmsgs.end()); + // the internal errorlist is cleared after each check() call + ASSERT_EQUALS(1, errorLogger.errmsgs.size()); + auto it = errorLogger.errmsgs.cbegin(); + ASSERT_EQUALS("test.cpp:0:0: information: The configuration 'X' was not checked because its code equals another one. [purgedConfiguration]", + it->toString(false, templateFormat, "")); + } + // TODO: test suppressions // TODO: test all with FS }; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f4d0ca61813..f4d39cc1acf 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -166,6 +166,7 @@ class TestNullPointer : public TestFixture { TEST_CASE(nullpointerStdStream); TEST_CASE(nullpointerSmartPointer); TEST_CASE(nullpointerOutOfMemory); + TEST_CASE(nullpointerOutOfResources); TEST_CASE(functioncall); TEST_CASE(functioncalllibrary); // use Library to parse function call TEST_CASE(functioncallDefaultArguments); @@ -4234,6 +4235,18 @@ class TestNullPointer : public TestFixture { } } + void nullpointerOutOfResources() { + check("void f() {\n" + " FILE* fid = fopen(\"x.txt\", \"w\");\n" + " fprintf(fid, \"abcdef\");\n" + " fclose(fid);\n" + "}\n"); + ASSERT_EQUALS( + "[test.cpp:3:13]: (warning) If resource allocation fails, then there is a possible null pointer dereference: fid [nullPointerOutOfResources]\n" + "[test.cpp:4:12]: (warning) If resource allocation fails, then there is a possible null pointer dereference: fid [nullPointerOutOfResources]\n", + errout_str()); + } + void functioncalllibrary() { SimpleTokenizer tokenizer(settingsDefault,*this,false); const char code[] = "void f() { int a,b,c; x(a,b,c); }";