Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/testcppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class TestCppcheck : public TestFixture {
TEST_CASE(getDumpFileContentsLibrary);
TEST_CASE(premiumResultsCache);
TEST_CASE(toomanyconfigs);
TEST_CASE(purgedConfiguration);
}

void getErrorMessages() const {
Expand Down Expand Up @@ -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
};
Expand Down
13 changes: 13 additions & 0 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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); }";
Expand Down