From 65505543bd0712c64fa3fd94c2ebb081e777a6f3 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 23 Oct 2023 16:56:48 +0200 Subject: [PATCH 1/2] CppCheck: small refactoring in `checkNormalTokens()` --- lib/cppcheck.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 3c672f7d8d7..c6006b00a44 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1097,12 +1097,11 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) if (mSettings.useSingleJob() || !mSettings.buildDir.empty()) { // Analyse the tokens.. - CTU::FileInfo *fi1 = CTU::getFileInfo(&tokenizer); - if (fi1) { - if (mSettings.useSingleJob()) - mFileInfo.push_back(fi1); + if (CTU::FileInfo * const fi1 = CTU::getFileInfo(&tokenizer)) { if (!mSettings.buildDir.empty()) mAnalyzerInformation.setFileInfo("ctu", fi1->toString()); + if (mSettings.useSingleJob()) + mFileInfo.push_back(fi1); } // cppcheck-suppress shadowFunction - TODO: fix this @@ -1110,12 +1109,11 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) if (doUnusedFunctionOnly && dynamic_cast(check) == nullptr) continue; - Check::FileInfo *fi = check->getFileInfo(&tokenizer, &mSettings); - if (fi != nullptr) { - if (mSettings.useSingleJob()) - mFileInfo.push_back(fi); + if (Check::FileInfo * const fi = check->getFileInfo(&tokenizer, &mSettings)) { if (!mSettings.buildDir.empty()) mAnalyzerInformation.setFileInfo(check->name(), fi->toString()); + if (mSettings.useSingleJob()) + mFileInfo.push_back(fi); } } } From 833c1058fce2c254f5505534abe8965bf6eb3c38 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 23 Oct 2023 16:57:36 +0200 Subject: [PATCH 2/2] fixed #12111 - memory leak with `-j2` and `--cppcheck-build-dir` --- lib/cppcheck.cpp | 4 ++++ test/cli/test-other.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index c6006b00a44..53705ed101c 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1102,6 +1102,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) mAnalyzerInformation.setFileInfo("ctu", fi1->toString()); if (mSettings.useSingleJob()) mFileInfo.push_back(fi1); + else + delete fi1; } // cppcheck-suppress shadowFunction - TODO: fix this @@ -1114,6 +1116,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) mAnalyzerInformation.setFileInfo(check->name(), fi->toString()); if (mSettings.useSingleJob()) mFileInfo.push_back(fi); + else + delete fi; } } } diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 8787fcb6611..819f1d6a64f 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -843,3 +843,19 @@ def test_file_ignore(tmpdir): ] assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines) + + +def test_build_dir_j_memleak(tmpdir): #12111 + build_dir = os.path.join(tmpdir, 'build-dir') + os.mkdir(build_dir) + + test_file = os.path.join(tmpdir, 'test.cpp') + with open(test_file, 'wt') as f: + f.write('int main() {}') + + args = ['--cppcheck-build-dir={}'.format(build_dir), '-j2', test_file] + out_lines = [ + 'Checking {} ...'.format(test_file) + ] + + assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)