Skip to content

Commit 107d10e

Browse files
Fix #13858, #13863 Add tests for nullpointerOutOfResources, purgedConfiguration (#7906)
Co-authored-by: chrchr-github <[email protected]>
1 parent 901a331 commit 107d10e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

test/testcppcheck.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class TestCppcheck : public TestFixture {
8181
TEST_CASE(getDumpFileContentsLibrary);
8282
TEST_CASE(premiumResultsCache);
8383
TEST_CASE(toomanyconfigs);
84+
TEST_CASE(purgedConfiguration);
8485
}
8586

8687
void getErrorMessages() const {
@@ -584,6 +585,33 @@ class TestCppcheck : public TestFixture {
584585
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, ""));
585586
}
586587

588+
void purgedConfiguration() const
589+
{
590+
ScopedFile test_file("test.cpp",
591+
"#ifdef X\n"
592+
"#endif\n"
593+
"int main() {}\n");
594+
595+
// this is the "simple" format
596+
const auto s = dinit(Settings,
597+
$.templateFormat = templateFormat, // TODO: remove when we only longer rely on toString() in unique message handling
598+
$.severity.enable (Severity::information);
599+
$.debugwarnings = true);
600+
Suppressions supprs;
601+
ErrorLogger2 errorLogger;
602+
CppCheck cppcheck(s, supprs, errorLogger, false, {});
603+
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file.path(), Path::identify(test_file.path(), false), 0)));
604+
// TODO: how to properly disable these warnings?
605+
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
606+
return msg.id == "logChecker";
607+
}), errorLogger.errmsgs.end());
608+
// the internal errorlist is cleared after each check() call
609+
ASSERT_EQUALS(1, errorLogger.errmsgs.size());
610+
auto it = errorLogger.errmsgs.cbegin();
611+
ASSERT_EQUALS("test.cpp:0:0: information: The configuration 'X' was not checked because its code equals another one. [purgedConfiguration]",
612+
it->toString(false, templateFormat, ""));
613+
}
614+
587615
// TODO: test suppressions
588616
// TODO: test all with FS
589617
};

test/testnullpointer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class TestNullPointer : public TestFixture {
166166
TEST_CASE(nullpointerStdStream);
167167
TEST_CASE(nullpointerSmartPointer);
168168
TEST_CASE(nullpointerOutOfMemory);
169+
TEST_CASE(nullpointerOutOfResources);
169170
TEST_CASE(functioncall);
170171
TEST_CASE(functioncalllibrary); // use Library to parse function call
171172
TEST_CASE(functioncallDefaultArguments);
@@ -4234,6 +4235,18 @@ class TestNullPointer : public TestFixture {
42344235
}
42354236
}
42364237

4238+
void nullpointerOutOfResources() {
4239+
check("void f() {\n"
4240+
" FILE* fid = fopen(\"x.txt\", \"w\");\n"
4241+
" fprintf(fid, \"abcdef\");\n"
4242+
" fclose(fid);\n"
4243+
"}\n");
4244+
ASSERT_EQUALS(
4245+
"[test.cpp:3:13]: (warning) If resource allocation fails, then there is a possible null pointer dereference: fid [nullPointerOutOfResources]\n"
4246+
"[test.cpp:4:12]: (warning) If resource allocation fails, then there is a possible null pointer dereference: fid [nullPointerOutOfResources]\n",
4247+
errout_str());
4248+
}
4249+
42374250
void functioncalllibrary() {
42384251
SimpleTokenizer tokenizer(settingsDefault,*this,false);
42394252
const char code[] = "void f() { int a,b,c; x(a,b,c); }";

0 commit comments

Comments
 (0)