From 60c166fed72ec725695e9434fb99e532136d3ede Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 9 Jan 2025 13:39:33 +0100 Subject: [PATCH] refs #11883 - CTU path information from build dir was not being used --- lib/ctu.cpp | 2 +- test/cli/other_test.py | 52 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/ctu.cpp b/lib/ctu.cpp index 949980cc6e4..171087f447c 100644 --- a/lib/ctu.cpp +++ b/lib/ctu.cpp @@ -212,7 +212,7 @@ bool CTU::FileInfo::FunctionCall::loadFromXml(const tinyxml2::XMLElement *xmlEle const int line = readAttrInt(e2, ATTR_LOC_LINENR, &error); const int column = readAttrInt(e2, ATTR_LOC_COLUMN, &error); ErrorMessage::FileLocation loc(file, std::move(info), line, column); - (void)loc; // TODO: loc is unused + callValuePath.emplace_back(std::move(loc)); } return !error; } diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 655e45de28b..ecd24b1e8bd 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -2784,4 +2784,54 @@ def test_addon_suppr_cli_file_line(tmp_path): def test_addon_suppr_cli_absfile_line(tmp_path): test_file = tmp_path / 'test.c' - __test_addon_suppr(tmp_path, ['--suppress=misra-c2012-2.3:{}:3'.format(test_file)]) \ No newline at end of file + __test_addon_suppr(tmp_path, ['--suppress=misra-c2012-2.3:{}:3'.format(test_file)]) + + +def test_ctu_path_builddir(tmp_path): # #11883 + build_dir = tmp_path / 'b1' + os.mkdir(build_dir) + + test_file = tmp_path / 'test.c' + with open(test_file, 'wt') as f: + f.write(""" +void f(int *p) { *p = 3; } +int main() { + int *p = 0; +f(p); +} + """) + + args = [ + '-q', + '--enable=style', + '--suppress=nullPointer', # we only care about the CTU findings + '--cppcheck-build-dir={}'.format(build_dir), + str(test_file) + ] + + # the CTU path was not properly read leading to missing location information + stderr_exp = [ + '{}:2:19: error: Null pointer dereference: p [ctunullpointer]'.format(test_file), + 'void f(int *p) { *p = 3; }', + ' ^', + "{}:4:14: note: Assignment 'p=0', assigned value is 0".format(test_file), + ' int *p = 0;', + ' ^', + '{}:5:2: note: Calling function f, 1st argument is null'.format(test_file), + 'f(p);', + ' ^', + '{}:2:19: note: Dereferencing argument p that is null'.format(test_file), + 'void f(int *p) { *p = 3; }', + ' ^' + ] + + exitcode_1, stdout_1, stderr_1 = cppcheck(args) + print(stderr_1) + assert exitcode_1 == 0, stdout_1 + assert stdout_1 == '' + assert stderr_1.splitlines() == stderr_exp + + exitcode_2, stdout_2, stderr_2 = cppcheck(args) + assert exitcode_2 == 0, stdout_2 + assert stdout_2 == '' + assert stderr_2.splitlines() == stderr_exp