Skip to content

Commit b3d8256

Browse files
authored
Fix #15212 (FN ctunullpointer through a wrapper function with --cppcheck-build-dir) (#9024)
1 parent 275e673 commit b3d8256

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ test/testmathlib.o: test/testmathlib.cpp lib/check.h lib/checkers.h lib/color.h
844844
test/testmemleak.o: test/testmemleak.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checkmemoryleak.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
845845
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testmemleak.cpp
846846

847-
test/testnullpointer.o: test/testnullpointer.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checknullpointer.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
847+
test/testnullpointer.o: test/testnullpointer.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/checkimpl.h lib/checknullpointer.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h test/fixture.h test/helpers.h
848848
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testnullpointer.cpp
849849

850850
test/testoptions.o: test/testoptions.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/utils.h test/fixture.h test/options.h

‎lib/ctu.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ std::string CTU::FileInfo::FunctionCall::toXmlString() const
124124
std::string CTU::FileInfo::NestedCall::toXmlString() const
125125
{
126126
std::ostringstream out;
127-
out << "<function-call"
127+
out << "<nested-call"
128128
<< toBaseXmlString()
129129
<< " " << ATTR_MY_ID << "=\"" << myId << "\""
130130
<< " " << ATTR_MY_ARGNR << "=\"" << myArgNr << "\""

‎test/cli/whole-program_test.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,51 @@ def test_nullpointer_out_of_memory(tmpdir, single_file):
430430
else:
431431
# the bug is found using ctu analysis
432432
assert results == ['[ctunullpointerOutOfMemory]']
433+
434+
435+
def __test_nullpointer_nested_call(tmpdir, extra_args):
436+
with open(tmpdir / 'header.h', 'wt') as f:
437+
f.write('void wrapper(int *p);\n')
438+
with open(tmpdir / 'test1.c', 'wt') as f:
439+
f.write('#include "header.h"\n'
440+
'int g;\n'
441+
'void deref(int *p) { g = *p; }\n'
442+
'void wrapper(int *p) { deref(p); }\n')
443+
with open(tmpdir / 'test2.c', 'wt') as f:
444+
f.write('#include "header.h"\n'
445+
'void f(void) { wrapper(0); }\n')
446+
447+
args = [
448+
'-q',
449+
'--template=simple',
450+
'--error-exitcode=1',
451+
'test1.c',
452+
'test2.c'
453+
]
454+
455+
args += extra_args
456+
457+
ret, stdout, stderr = cppcheck(args, cwd=tmpdir)
458+
lines = stderr.splitlines()
459+
assert lines == [
460+
'test1.c:3:27: error: Null pointer dereference: p [ctunullpointer]'
461+
]
462+
assert stdout == ''
463+
assert ret == 1, stdout
464+
465+
466+
def test_nullpointer_nested_call(tmpdir):
467+
__test_nullpointer_nested_call(tmpdir, ['-j1'])
468+
469+
470+
def test_nullpointer_nested_call_builddir_cached(tmpdir):
471+
build_dir = os.path.join(tmpdir, 'b1')
472+
os.mkdir(build_dir)
473+
__test_nullpointer_nested_call(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
474+
__test_nullpointer_nested_call(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
475+
476+
477+
def test_nullpointer_nested_call_builddir_j(tmpdir):
478+
build_dir = os.path.join(tmpdir, 'b1')
479+
os.mkdir(build_dir)
480+
__test_nullpointer_nested_call(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])

‎test/testnullpointer.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "settings.h"
2727
#include "standards.h"
2828
#include "token.h"
29+
#include "xml.h"
2930

3031
#include <cstddef>
3132
#include <list>
@@ -188,6 +189,7 @@ class TestNullPointer : public TestFixture {
188189
TEST_CASE(isPointerDeRefFunctionDecl);
189190

190191
TEST_CASE(ctuTest);
192+
TEST_CASE(ctuXml);
191193
}
192194

193195
struct CheckOptions
@@ -5323,6 +5325,26 @@ class TestNullPointer : public TestFixture {
53235325
"}\n");
53245326
ASSERT_EQUALS("", errout_str());
53255327
}
5328+
5329+
void ctuXml() {
5330+
SimpleTokenizer tokenizer(settings, *this);
5331+
ASSERT(tokenizer.tokenize("void use(int *p) { a = *p + 3; }\n"
5332+
"void call(int x, int *p) { x++; use(p); }\n"
5333+
"int main() {\n"
5334+
" call(4,0);\n"
5335+
"}\n"));
5336+
const CTU::FileInfo *fi1 = CTU::getFileInfo(tokenizer);
5337+
ASSERT_EQUALS(1U, fi1->nestedCalls.size());
5338+
5339+
tinyxml2::XMLDocument doc;
5340+
const tinyxml2::XMLError xmlError = doc.Parse(("<FileInfo>" + fi1->toString() + "</FileInfo>").c_str());
5341+
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
5342+
CTU::FileInfo fi2;
5343+
fi2.loadFromXml(doc.FirstChildElement());
5344+
ASSERT_EQUALS(1U, fi2.nestedCalls.size());
5345+
ASSERT_EQUALS(fi1->toString(), fi2.toString());
5346+
delete fi1;
5347+
}
53265348
};
53275349

53285350
REGISTER_TEST(TestNullPointer)

0 commit comments

Comments
 (0)