From 11a1f22583dcc4849bd981e34162fcdfc78268c6 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 3 Jan 2025 23:32:59 +0100 Subject: [PATCH 01/27] Fix #13523 Mention including cpp file for error in header --- lib/check.h | 3 ++- lib/checkbufferoverrun.cpp | 3 ++- lib/checkclass.cpp | 18 ++++++++++-------- lib/checknullpointer.cpp | 16 +++++++++------- lib/checkuninitvar.cpp | 18 ++++++++++-------- test/cli/whole-program/nullpointer1.cpp | 1 + test/cli/whole-program/nullpointer1.h | 8 ++++++++ test/cli/whole-program/nullpointer1_1.h | 4 ++++ test/cli/whole-program_test.py | 25 +++++++++++++++++++++++++ 9 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 test/cli/whole-program/nullpointer1.cpp create mode 100644 test/cli/whole-program/nullpointer1.h create mode 100644 test/cli/whole-program/nullpointer1_1.h diff --git a/lib/check.h b/lib/check.h index ce128ee7d25..ce50bcd25df 100644 --- a/lib/check.h +++ b/lib/check.h @@ -102,11 +102,12 @@ class CPPCHECKLIB Check { /** Base class used for whole-program analysis */ class CPPCHECKLIB FileInfo { public: - FileInfo() = default; + explicit FileInfo(const std::string& f0 = {}) : file0(f0) {} virtual ~FileInfo() = default; virtual std::string toString() const { return std::string(); } + std::string file0; }; virtual FileInfo * getFileInfo(const Tokenizer& /*tokenizer*/, const Settings& /*settings*/) const { diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index a0326a9db1f..2049141413b 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -893,6 +893,7 @@ namespace /** data for multifile checking */ class MyFileInfo : public Check::FileInfo { public: + using Check::FileInfo::FileInfo; /** unsafe array index usage */ std::list unsafeArrayIndex; @@ -951,7 +952,7 @@ Check::FileInfo *CheckBufferOverrun::getFileInfo(const Tokenizer &tokenizer, con if (unsafeArrayIndex.empty() && unsafePointerArith.empty()) { return nullptr; } - auto *fileInfo = new MyFileInfo; + auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]); fileInfo->unsafeArrayIndex = unsafeArrayIndex; fileInfo->unsafePointerArith = unsafePointerArith; return fileInfo; diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a7c775166ac..fb29da96b5b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3566,6 +3566,7 @@ namespace /* multifile checking; one definition rule violations */ class MyFileInfo : public Check::FileInfo { public: + using Check::FileInfo::FileInfo; struct NameLoc { std::string className; std::string fileName; @@ -3727,14 +3728,15 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::listsecond.fileName, it->second.lineNumber, it->second.column); - const ErrorMessage errmsg(std::move(locationList), - emptyString, - Severity::error, - "$symbol:" + nameLoc.className + - "\nThe one definition rule is violated, different classes/structs have the same name '$symbol'", - "ctuOneDefinitionRuleViolation", - CWE_ONE_DEFINITION_RULE, - Certainty::normal); + ErrorMessage errmsg(std::move(locationList), + emptyString, + Severity::error, + "$symbol:" + nameLoc.className + + "\nThe one definition rule is violated, different classes/structs have the same name '$symbol'", + "ctuOneDefinitionRuleViolation", + CWE_ONE_DEFINITION_RULE, + Certainty::normal); + errmsg.file0 = fi->file0; errorLogger.reportErr(errmsg); foundErrors = true; diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 1a19acd4878..58b0b3b772c 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -600,6 +600,7 @@ namespace /* data for multifile checking */ class MyFileInfo : public Check::FileInfo { public: + using Check::FileInfo::FileInfo; /** function arguments that are dereferenced without checking if they are null */ std::list unsafeUsage; @@ -617,7 +618,7 @@ Check::FileInfo *CheckNullPointer::getFileInfo(const Tokenizer &tokenizer, const if (unsafeUsage.empty()) return nullptr; - auto *fileInfo = new MyFileInfo; + auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]); fileInfo->unsafeUsage = unsafeUsage; return fileInfo; } @@ -666,12 +667,13 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std:: if (locationList.empty()) continue; - const ErrorMessage errmsg(locationList, - emptyString, - warning ? Severity::warning : Severity::error, - "Null pointer dereference: " + unsafeUsage.myArgumentName, - "ctunullpointer", - CWE_NULL_POINTER_DEREFERENCE, Certainty::normal); + ErrorMessage errmsg(locationList, + emptyString, + warning ? Severity::warning : Severity::error, + "Null pointer dereference: " + unsafeUsage.myArgumentName, + "ctunullpointer", + CWE_NULL_POINTER_DEREFERENCE, Certainty::normal); + errmsg.file0 = fi->file0; errorLogger.reportErr(errmsg); foundErrors = true; diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 541a46c071f..93f3a7cd407 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1708,6 +1708,7 @@ namespace /* data for multifile checking */ class MyFileInfo : public Check::FileInfo { public: + using Check::FileInfo::FileInfo; /** function arguments that data are unconditionally read */ std::list unsafeUsage; @@ -1725,7 +1726,7 @@ Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer &tokenizer, const S if (unsafeUsage.empty()) return nullptr; - auto *fileInfo = new MyFileInfo; + auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]); fileInfo->unsafeUsage = unsafeUsage; return fileInfo; } @@ -1768,13 +1769,14 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li if (locationList.empty()) continue; - const ErrorMessage errmsg(locationList, - emptyString, - Severity::error, - "Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression, - "ctuuninitvar", - CWE_USE_OF_UNINITIALIZED_VARIABLE, - Certainty::normal); + ErrorMessage errmsg(locationList, + emptyString, + Severity::error, + "Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression, + "ctuuninitvar", + CWE_USE_OF_UNINITIALIZED_VARIABLE, + Certainty::normal); + errmsg.file0 = fi->file0; errorLogger.reportErr(errmsg); foundErrors = true; diff --git a/test/cli/whole-program/nullpointer1.cpp b/test/cli/whole-program/nullpointer1.cpp new file mode 100644 index 00000000000..376850209e2 --- /dev/null +++ b/test/cli/whole-program/nullpointer1.cpp @@ -0,0 +1 @@ +#include "nullpointer1.h" \ No newline at end of file diff --git a/test/cli/whole-program/nullpointer1.h b/test/cli/whole-program/nullpointer1.h new file mode 100644 index 00000000000..56fa6c49d84 --- /dev/null +++ b/test/cli/whole-program/nullpointer1.h @@ -0,0 +1,8 @@ +#include "nullpointer1_1.h" + +template +void f(T* p) { + if (sizeof(T) == 4) + p = nullptr; + g(p); +} diff --git a/test/cli/whole-program/nullpointer1_1.h b/test/cli/whole-program/nullpointer1_1.h new file mode 100644 index 00000000000..cbbc478cf2c --- /dev/null +++ b/test/cli/whole-program/nullpointer1_1.h @@ -0,0 +1,4 @@ +template +void g(T* p) { + *p = 0; +}; diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index c8115f06d19..dbfeac09927 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -4,6 +4,7 @@ import json import shutil from testutils import cppcheck +import xml.etree.ElementTree as ET __script_dir = os.path.dirname(os.path.abspath(__file__)) @@ -358,3 +359,27 @@ def test_checkclass_project_builddir_j(tmpdir): build_dir = os.path.join(tmpdir, 'b1') os.mkdir(build_dir) __test_checkclass_project(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)]) + +def __test_nullpointer_file0(extra_args): + args = [ + '-q', + '--xml', + '--error-exitcode=1', + 'whole-program/nullpointer1.cpp' + ] + + args += extra_args + + ret, stdout, stderr = cppcheck(args, cwd=__script_dir) + results = ET.fromstring(stdout) + file0 = '' + for e in root.findall('errors/error'): + if (e.attrib['id'] == 'ctunullpointer'): + file0 = e.attrib['file0'] + assert file0 == 'whole-program/nullpointer1.cpp' + assert stdout == '' + assert ret == 1, stdout + + +def test_nullpointer_file0(): + __test_checkclass(['-j1']) \ No newline at end of file From f864c2b0991007961982ef5ea92d006ecea48f5a Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 3 Jan 2025 23:51:37 +0100 Subject: [PATCH 02/27] Fix --- test/cli/whole-program_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index dbfeac09927..6fa08aa6e02 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -373,7 +373,7 @@ def __test_nullpointer_file0(extra_args): ret, stdout, stderr = cppcheck(args, cwd=__script_dir) results = ET.fromstring(stdout) file0 = '' - for e in root.findall('errors/error'): + for e in results.findall('errors/error'): if (e.attrib['id'] == 'ctunullpointer'): file0 = e.attrib['file0'] assert file0 == 'whole-program/nullpointer1.cpp' @@ -382,4 +382,4 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): - __test_checkclass(['-j1']) \ No newline at end of file + __test_nullpointer_file0(['-j1']) From bf3a9941e81eec7f82975806a32b26fe1664c88e Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 00:06:38 +0100 Subject: [PATCH 03/27] Fix --- lib/check.h | 2 +- test/cli/whole-program_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/check.h b/lib/check.h index ce50bcd25df..15d0611a908 100644 --- a/lib/check.h +++ b/lib/check.h @@ -102,7 +102,7 @@ class CPPCHECKLIB Check { /** Base class used for whole-program analysis */ class CPPCHECKLIB FileInfo { public: - explicit FileInfo(const std::string& f0 = {}) : file0(f0) {} + explicit FileInfo(std::string f0 = {}) : file0(std::move(f0)) {} virtual ~FileInfo() = default; virtual std::string toString() const { return std::string(); diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 6fa08aa6e02..ee0537012e6 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -370,7 +370,7 @@ def __test_nullpointer_file0(extra_args): args += extra_args - ret, stdout, stderr = cppcheck(args, cwd=__script_dir) + ret, _, stderr = cppcheck(args, cwd=__script_dir) results = ET.fromstring(stdout) file0 = '' for e in results.findall('errors/error'): From f6b31de847ab612c3c6881d0b0a8385cdc8cee3e Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 00:07:04 +0100 Subject: [PATCH 04/27] Fix --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index ee0537012e6..cc6941c70a0 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -371,7 +371,7 @@ def __test_nullpointer_file0(extra_args): args += extra_args ret, _, stderr = cppcheck(args, cwd=__script_dir) - results = ET.fromstring(stdout) + results = ET.fromstring(stderr) file0 = '' for e in results.findall('errors/error'): if (e.attrib['id'] == 'ctunullpointer'): From 46239d5959b418aab6ef86e82569772c0bb226f5 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 00:23:58 +0100 Subject: [PATCH 05/27] Fix --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index cc6941c70a0..766b51a22d8 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -370,7 +370,7 @@ def __test_nullpointer_file0(extra_args): args += extra_args - ret, _, stderr = cppcheck(args, cwd=__script_dir) + ret, stdout, stderr = cppcheck(args, cwd=__script_dir) results = ET.fromstring(stderr) file0 = '' for e in results.findall('errors/error'): From 12b2dd065162da83f6336f005ba3c722943e4b7f Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 00:46:43 +0100 Subject: [PATCH 06/27] debug --- test/cli/whole-program_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 766b51a22d8..d8e9c7e211c 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -378,6 +378,7 @@ def __test_nullpointer_file0(extra_args): file0 = e.attrib['file0'] assert file0 == 'whole-program/nullpointer1.cpp' assert stdout == '' + assert stderr == '' assert ret == 1, stdout From 1ab20e69b5e8eef08045e26c3e4b096c64f22b2a Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 00:48:59 +0100 Subject: [PATCH 07/27] debug --- test/cli/whole-program_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index d8e9c7e211c..3492af5dcb1 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -371,12 +371,12 @@ def __test_nullpointer_file0(extra_args): args += extra_args ret, stdout, stderr = cppcheck(args, cwd=__script_dir) - results = ET.fromstring(stderr) - file0 = '' - for e in results.findall('errors/error'): - if (e.attrib['id'] == 'ctunullpointer'): - file0 = e.attrib['file0'] - assert file0 == 'whole-program/nullpointer1.cpp' + #results = ET.fromstring(stderr) + #file0 = '' + #for e in results.findall('errors/error'): + # if (e.attrib['id'] == 'ctunullpointer'): + # file0 = e.attrib['file0'] + #assert file0 == 'whole-program/nullpointer1.cpp' assert stdout == '' assert stderr == '' assert ret == 1, stdout From 791b06612a7de8e69cd119b6ef2f6a2d99439a00 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 01:02:10 +0100 Subject: [PATCH 08/27] undo --- test/cli/whole-program_test.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 3492af5dcb1..f4f71417929 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -371,14 +371,14 @@ def __test_nullpointer_file0(extra_args): args += extra_args ret, stdout, stderr = cppcheck(args, cwd=__script_dir) - #results = ET.fromstring(stderr) - #file0 = '' - #for e in results.findall('errors/error'): - # if (e.attrib['id'] == 'ctunullpointer'): - # file0 = e.attrib['file0'] - #assert file0 == 'whole-program/nullpointer1.cpp' + results = ET.fromstring(stderr) + file0 = '' + for e in results.findall('errors/error'): + if e.attrib['id'] == 'ctunullpointer': + if 'file0' in e.attrib: + file0 = e.attrib['file0'] + assert file0 == 'whole-program/nullpointer1.cpp' assert stdout == '' - assert stderr == '' assert ret == 1, stdout From e04d0a602626c32f85e92721cc3c232ba26b3ded Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 11:34:54 +0100 Subject: [PATCH 09/27] Refactor --- lib/checkclass.cpp | 17 ++++++++--------- lib/checknullpointer.cpp | 13 ++++++------- lib/checkuninitvar.cpp | 15 +++++++-------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index fb29da96b5b..e6a3a5b2411 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3728,15 +3728,14 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::listsecond.fileName, it->second.lineNumber, it->second.column); - ErrorMessage errmsg(std::move(locationList), - emptyString, - Severity::error, - "$symbol:" + nameLoc.className + - "\nThe one definition rule is violated, different classes/structs have the same name '$symbol'", - "ctuOneDefinitionRuleViolation", - CWE_ONE_DEFINITION_RULE, - Certainty::normal); - errmsg.file0 = fi->file0; + const ErrorMessage errmsg(std::move(locationList), + fi->file0, + Severity::error, + "$symbol:" + nameLoc.className + + "\nThe one definition rule is violated, different classes/structs have the same name '$symbol'", + "ctuOneDefinitionRuleViolation", + CWE_ONE_DEFINITION_RULE, + Certainty::normal); errorLogger.reportErr(errmsg); foundErrors = true; diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 58b0b3b772c..dfdafbb5a57 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -667,13 +667,12 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std:: if (locationList.empty()) continue; - ErrorMessage errmsg(locationList, - emptyString, - warning ? Severity::warning : Severity::error, - "Null pointer dereference: " + unsafeUsage.myArgumentName, - "ctunullpointer", - CWE_NULL_POINTER_DEREFERENCE, Certainty::normal); - errmsg.file0 = fi->file0; + const ErrorMessage errmsg(locationList, + fi->file0, + warning ? Severity::warning : Severity::error, + "Null pointer dereference: " + unsafeUsage.myArgumentName, + "ctunullpointer", + CWE_NULL_POINTER_DEREFERENCE, Certainty::normal); errorLogger.reportErr(errmsg); foundErrors = true; diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 93f3a7cd407..d1587142554 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1769,14 +1769,13 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li if (locationList.empty()) continue; - ErrorMessage errmsg(locationList, - emptyString, - Severity::error, - "Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression, - "ctuuninitvar", - CWE_USE_OF_UNINITIALIZED_VARIABLE, - Certainty::normal); - errmsg.file0 = fi->file0; + const ErrorMessage errmsg(locationList, + fi->file0, + Severity::error, + "Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression, + "ctuuninitvar", + CWE_USE_OF_UNINITIALIZED_VARIABLE, + Certainty::normal); errorLogger.reportErr(errmsg); foundErrors = true; From 706c5cae7243e5e5ddf18eb9f2d5433418f4f505 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 11:39:25 +0100 Subject: [PATCH 10/27] Format [skip ci] --- test/cli/whole-program/nullpointer1.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program/nullpointer1.cpp b/test/cli/whole-program/nullpointer1.cpp index 376850209e2..d323e3ad1d3 100644 --- a/test/cli/whole-program/nullpointer1.cpp +++ b/test/cli/whole-program/nullpointer1.cpp @@ -1 +1 @@ -#include "nullpointer1.h" \ No newline at end of file +#include "nullpointer1.h" From 9a57c319921d09d96d02ec538dba19b7b2f1c758 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 12:04:21 +0100 Subject: [PATCH 11/27] Add -j2 --- test/cli/whole-program_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index f4f71417929..5d01f328ae7 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -384,3 +384,6 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) + +def test_nullpointer_file0_j(): + __test_nullpointer_file0(['-j2']) From e0a623cd8c759bfc53a15f0595a787d85462333e Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 12:05:23 +0100 Subject: [PATCH 12/27] Add file0 in remaining CTU checks --- lib/checkbufferoverrun.cpp | 9 +++++---- lib/checkbufferoverrun.h | 3 ++- lib/checkclass.cpp | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 2049141413b..e15389cae0a 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -999,14 +999,15 @@ bool CheckBufferOverrun::analyseWholeProgram(const CTU::FileInfo *ctu, const std if (!fi) continue; for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeArrayIndex) - foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 1, errorLogger, settings.maxCtuDepth); + foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 1, errorLogger, settings.maxCtuDepth, fi->file0); for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafePointerArith) - foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 2, errorLogger, settings.maxCtuDepth); + foundErrors |= analyseWholeProgram1(callsMap, unsafeUsage, 2, errorLogger, settings.maxCtuDepth, fi->file0); } return foundErrors; } -bool CheckBufferOverrun::analyseWholeProgram1(const std::map> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger, int maxCtuDepth) +bool CheckBufferOverrun::analyseWholeProgram1(const std::map> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, + int type, ErrorLogger &errorLogger, int maxCtuDepth, const std::string& file0) { const CTU::FileInfo::FunctionCall *functionCall = nullptr; @@ -1039,7 +1040,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger, int maxCtuDepth); + static bool analyseWholeProgram1(const std::map> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, + int type, ErrorLogger &errorLogger, int maxCtuDepth, const std::string& file0); static std::string myName() { diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index e6a3a5b2411..69be9cbaa8d 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3663,7 +3663,7 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer &tokenizer, const Setti if (classDefinitions.empty()) return nullptr; - auto *fileInfo = new MyFileInfo; + auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]); fileInfo->classDefinitions.swap(classDefinitions); return fileInfo; } From e28646c748ca429a355057da9b3806d835eca313 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 12:28:48 +0100 Subject: [PATCH 13/27] xfail --- test/cli/whole-program_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 5d01f328ae7..205e5dc4ef7 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -385,5 +385,6 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) +@pytest.mark.xfail(strict=True) # TODO: check error def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2']) From 65e54b5fdb2b762b969ce2e7e9448c5f7346ba86 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 19:25:08 +0100 Subject: [PATCH 14/27] Use build dir with -j2 --- test/cli/whole-program_test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 205e5dc4ef7..e560437c240 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -385,6 +385,7 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -@pytest.mark.xfail(strict=True) # TODO: check error def test_nullpointer_file0_j(): - __test_nullpointer_file0(['-j2']) + build_dir = os.path.join(tmpdir, 'b1') + os.mkdir(build_dir) + __test_nullpointer_file0(['-j2', '--cppcheck-build-dir={}'.format(build_dir)]) From 25d1801e4dd1a1eec2292daad6656a7ccea021fc Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 19:59:15 +0100 Subject: [PATCH 15/27] Fix, simplify --- test/cli/whole-program_test.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index e560437c240..ecbafc60455 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -373,10 +373,9 @@ def __test_nullpointer_file0(extra_args): ret, stdout, stderr = cppcheck(args, cwd=__script_dir) results = ET.fromstring(stderr) file0 = '' - for e in results.findall('errors/error'): - if e.attrib['id'] == 'ctunullpointer': - if 'file0' in e.attrib: - file0 = e.attrib['file0'] + for e in results.findall('errors/error[@id="ctunullpointer"]'): + if 'file0' in e.attrib: + file0 = e.attrib['file0'] assert file0 == 'whole-program/nullpointer1.cpp' assert stdout == '' assert ret == 1, stdout @@ -385,7 +384,7 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -def test_nullpointer_file0_j(): +def test_nullpointer_file0_j(tmpdir): build_dir = os.path.join(tmpdir, 'b1') os.mkdir(build_dir) __test_nullpointer_file0(['-j2', '--cppcheck-build-dir={}'.format(build_dir)]) From 7309cf36ea9f49086c2ea7a14c298cc6dc08948f Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 4 Jan 2025 20:21:19 +0100 Subject: [PATCH 16/27] xfail --- test/cli/whole-program_test.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index ecbafc60455..d4fd5c40881 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -384,7 +384,6 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -def test_nullpointer_file0_j(tmpdir): - build_dir = os.path.join(tmpdir, 'b1') - os.mkdir(build_dir) - __test_nullpointer_file0(['-j2', '--cppcheck-build-dir={}'.format(build_dir)]) +@pytest.mark.xfail(strict=True) # TODO: check error, still fails with build dir +def test_nullpointer_file0_j(): + __test_nullpointer_file0(['-j2']) From 9a411381ff04ae97e617579c5f281c10a88b344b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:40:43 +0100 Subject: [PATCH 17/27] Update cppcheck.cpp --- lib/cppcheck.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index a4b9a88445e..3feb0b7debc 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -2041,8 +2041,11 @@ unsigned int CppCheck::analyseWholeProgram(const std::string &buildDir, const st } // cppcheck-suppress shadowFunction - TODO: fix this for (const Check *check : Check::instances()) { - if (checkClassAttr == check->name()) - fileInfoList.push_back(check->loadFileInfoFromXml(e)); + if (checkClassAttr == check->name()) { + Check::FileInfo* fi = check->loadFileInfoFromXml(e); + fi->file0 = filesTxtLine.substr(firstColon + 2); + fileInfoList.push_back(fi); + } } } } From fbe7f8778096a6e42bcfae66661b8584d666e098 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:42:12 +0100 Subject: [PATCH 18/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index d4fd5c40881..87050935cb8 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -384,6 +384,11 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -@pytest.mark.xfail(strict=True) # TODO: check error, still fails with build dir +@pytest.mark.xfail(strict=True) # TODO: check error def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2']) + +def test_nullpointer_file0_builddir_j(tmpdir): + build_dir = os.path.join(tmpdir, 'b1') + os.mkdir(build_dir) + __test_nullpointer_file0(['-j2', '--cppcheck-build-dir={}'.format(build_dir)]) From 1a219d85670a1f2b9f736d440d95ab1aa8fbb9c9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:08:56 +0100 Subject: [PATCH 19/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 87050935cb8..2bba8b698f0 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -384,7 +384,6 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -@pytest.mark.xfail(strict=True) # TODO: check error def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2']) From a9b9de4b087aba6e7975364be39135ab2443daa0 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:28:12 +0100 Subject: [PATCH 20/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 2bba8b698f0..85006bb6545 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -374,7 +374,7 @@ def __test_nullpointer_file0(extra_args): results = ET.fromstring(stderr) file0 = '' for e in results.findall('errors/error[@id="ctunullpointer"]'): - if 'file0' in e.attrib: + if 'file0' in e.attrib and len(file0) == 0: file0 = e.attrib['file0'] assert file0 == 'whole-program/nullpointer1.cpp' assert stdout == '' From 750b92c8cacb9e99c91ca944480b610319b27bf3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:43:27 +0100 Subject: [PATCH 21/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 85006bb6545..1e39bb4e609 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -374,8 +374,7 @@ def __test_nullpointer_file0(extra_args): results = ET.fromstring(stderr) file0 = '' for e in results.findall('errors/error[@id="ctunullpointer"]'): - if 'file0' in e.attrib and len(file0) == 0: - file0 = e.attrib['file0'] + file0 = e.attrib['file0'] assert file0 == 'whole-program/nullpointer1.cpp' assert stdout == '' assert ret == 1, stdout @@ -384,6 +383,7 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) +@pytest.mark.skip(reason="flaky!?") def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2']) From f65bc605d9d0b8fc6712d3deb2559792de29c5e5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:39:25 +0100 Subject: [PATCH 22/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 1e39bb4e609..47baa822896 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -375,15 +375,15 @@ def __test_nullpointer_file0(extra_args): file0 = '' for e in results.findall('errors/error[@id="ctunullpointer"]'): file0 = e.attrib['file0'] - assert file0 == 'whole-program/nullpointer1.cpp' - assert stdout == '' - assert ret == 1, stdout + assert ret == 1, , stdout if stdout else stderr + assert stdout == '' + assert file0 == 'whole-program/nullpointer1.cpp', stderr def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -@pytest.mark.skip(reason="flaky!?") +@pytest.mark.xfail(strict=True) # no CTU without builddir def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2']) From 33cedb531c117cae3dcf1a94d025e5b1bd195e79 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:50:35 +0100 Subject: [PATCH 23/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 47baa822896..1f42b89dd2f 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -376,7 +376,7 @@ def __test_nullpointer_file0(extra_args): for e in results.findall('errors/error[@id="ctunullpointer"]'): file0 = e.attrib['file0'] - assert ret == 1, , stdout if stdout else stderr + assert ret == 1, stdout if stdout else stderr assert stdout == '' assert file0 == 'whole-program/nullpointer1.cpp', stderr From cc608825756f07d367769af7de093384152889e7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:31:46 +0100 Subject: [PATCH 24/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 1f42b89dd2f..bb4a59b1d4b 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -383,7 +383,7 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -@pytest.mark.xfail(strict=True) # no CTU without builddir +#@pytest.mark.xfail(strict=True) # no CTU without builddir def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2']) From 4f288ff9973e6ec1284d00fd0a608edfeba7d233 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:56:08 +0100 Subject: [PATCH 25/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index bb4a59b1d4b..48d3a218819 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -381,11 +381,11 @@ def __test_nullpointer_file0(extra_args): assert file0 == 'whole-program/nullpointer1.cpp', stderr def test_nullpointer_file0(): - __test_nullpointer_file0(['-j1']) + __test_nullpointer_file0(['-j1', '--no-cppcheck-build-dir']) #@pytest.mark.xfail(strict=True) # no CTU without builddir def test_nullpointer_file0_j(): - __test_nullpointer_file0(['-j2']) + __test_nullpointer_file0(['-j2', '--no-cppcheck-build-dir']) def test_nullpointer_file0_builddir_j(tmpdir): build_dir = os.path.join(tmpdir, 'b1') From 155eea96ca2b37ef964f3cc05a6b4f378c95b40b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 8 Jan 2025 00:10:17 +0100 Subject: [PATCH 26/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index 48d3a218819..c700f1d6899 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -381,7 +381,7 @@ def __test_nullpointer_file0(extra_args): assert file0 == 'whole-program/nullpointer1.cpp', stderr def test_nullpointer_file0(): - __test_nullpointer_file0(['-j1', '--no-cppcheck-build-dir']) + __test_nullpointer_file0(['-j1']) #@pytest.mark.xfail(strict=True) # no CTU without builddir def test_nullpointer_file0_j(): From e6c30a27bb6bead1dc364998aa72fb4d3d53726c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 8 Jan 2025 00:23:17 +0100 Subject: [PATCH 27/27] Update whole-program_test.py --- test/cli/whole-program_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py index c700f1d6899..f45966e9389 100644 --- a/test/cli/whole-program_test.py +++ b/test/cli/whole-program_test.py @@ -383,7 +383,7 @@ def __test_nullpointer_file0(extra_args): def test_nullpointer_file0(): __test_nullpointer_file0(['-j1']) -#@pytest.mark.xfail(strict=True) # no CTU without builddir +@pytest.mark.xfail(strict=True) # no CTU without builddir def test_nullpointer_file0_j(): __test_nullpointer_file0(['-j2', '--no-cppcheck-build-dir'])