From a6b0129725856bcac3135f2ddb8a39f11c3b45db Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 20 Jul 2023 10:45:44 +0200 Subject: [PATCH 01/16] Fix #11838 FP uninitvar with label matching variable name (#5251) --- lib/tokenize.cpp | 3 ++- test/testvarid.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4bb7268f5d9..b49aa46e4bb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4757,7 +4757,8 @@ void Tokenizer::setVarIdPass1() continue; } - if (!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) { + if ((!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) && + !Token::simpleMatch(tok->next(), ": ;")) { const std::map::const_iterator it = variableMap.map(globalNamespace).find(tok->str()); if (it != variableMap.map(globalNamespace).end()) { tok->varId(it->second); diff --git a/test/testvarid.cpp b/test/testvarid.cpp index e0ffc150020..90dfeafe604 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -2785,6 +2785,17 @@ class TestVarID : public TestFixture { " break;\n" " }\n" "}", "test.c")); + + ASSERT_EQUALS("1: int * f ( ) {\n" // #11838 + "2: int * label@1 ; label@1 = 0 ;\n" + "3: label : ;\n" + "4: return label@1 ;\n" + "5: }\n", + tokenize("int* f() {\n" + " int* label = 0;\n" + "label:\n" + " return label;\n" + "}")); } void varid_structinit() { // #6406 From 101ddea1e6f9074910f06366cf7c883a39eab414 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 21 Jul 2023 17:33:18 +0200 Subject: [PATCH 02/16] Fix #11840 FP constStatement with template parameters on operator (#5258) --- lib/tokenize.cpp | 3 ++- test/testtokenize.cpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b49aa46e4bb..302c202316c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5250,7 +5250,8 @@ void Tokenizer::createLinks2() } else if (token->str() == "<" && ((token->previous() && (token->previous()->isTemplate() || (token->previous()->isName() && !token->previous()->varId()) || - (token->strAt(-1) == "]" && (!Token::Match(token->linkAt(-1)->previous(), "%name%|)") || token->linkAt(-1)->previous()->isKeyword())))) || + (token->strAt(-1) == "]" && (!Token::Match(token->linkAt(-1)->previous(), "%name%|)") || token->linkAt(-1)->previous()->isKeyword())) || + (token->strAt(-1) == ")" && token->linkAt(-1)->strAt(-1) == "operator"))) || Token::Match(token->next(), ">|>>"))) { type.push(token); if (token->previous()->str() == "template") diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index fc8558bc921..2e0972d07b8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -3531,6 +3531,25 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(true, tok1->link() == tok2); ASSERT_EQUALS(true, tok2->link() == tok1); } + + { + const char code[] = "struct S {\n" // #11840 + " template\n" + " void operator() (int);\n" + "};\n" + "void f() {\n" + " S s;\n" + " s.operator()(1);\n" + "}\n"; + errout.str(""); + Tokenizer tokenizer(&settings0, this); + std::istringstream istr(code); + ASSERT(tokenizer.tokenize(istr, "test.cpp")); + const Token* tok1 = Token::findsimplematch(tokenizer.tokens(), "< int"); + const Token* tok2 = Token::findsimplematch(tok1, "> ("); + ASSERT_EQUALS(true, tok1->link() == tok2); + ASSERT_EQUALS(true, tok2->link() == tok1); + } } void simplifyString() { From 2cf4b3a6f35b20eb5496423e976bbb40b96be070 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 24 Jul 2023 16:26:23 +0200 Subject: [PATCH 03/16] Fix functionConst TODO (#5261) --- lib/symboldatabase.cpp | 2 +- test/testclass.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 41d99055f45..14e7ece42ad 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1107,7 +1107,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass) continue; bool isTemplateArg = false; - if (tok->next()->str() != "(") { + if (!Token::Match(tok->next(), "(|{")) { const Token *start = tok; while (Token::Match(start->tokAt(-2), "%name% ::")) start = start->tokAt(-2); diff --git a/test/testclass.cpp b/test/testclass.cpp index 53371d545c3..f79744666c7 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6061,7 +6061,7 @@ class TestClass : public TestFixture { " int i{};\n" " S f() { return S{ &i }; }\n" "};\n"); - TODO_ASSERT_EQUALS("[test.cpp:7]: (style, inconclusive) Technically the member function 'C::f' can be const.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (style, inconclusive) Technically the member function 'C::f' can be const.\n", errout.str()); checkConst("struct S {\n" " explicit S(const int* p) : mp(p) {}\n" From ba764eaddc87c83963b9070a0a606a207a5ca3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Tue, 25 Jul 2023 12:00:23 +0200 Subject: [PATCH 04/16] unusedFunction disabled warning is not printed if --cppcheck-build-dir is used (#5252) ususedFunction check is enabled if cppcheck build dir is used. Warning about disabled unusedFunction check should not be printed in such case. --- cli/cmdlineparser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index d76edbf9114..0848f483cd7 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1031,7 +1031,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) else if ((def || mSettings.preprocessOnly) && !maxconfigs) mSettings.maxConfigs = 1U; - if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1) { + if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1 && mSettings.buildDir.empty()) { printMessage("unusedFunction check can't be used with '-j' option. Disabling unusedFunction check."); } From 3186577f45e00f5aac9979b4977f81195b2c62df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Tue, 25 Jul 2023 17:47:19 +0200 Subject: [PATCH 05/16] Add author of #5252 to AUTHORS (#5263) Add me to AUTHORS after my first merged PR #5252 --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index e3610324298..4463df0c5ec 100644 --- a/AUTHORS +++ b/AUTHORS @@ -315,6 +315,7 @@ Ryan Pavlik Samir Aguiar Sam Truscott Samuel Degrande +Samuel Poláček Sandeep Dutta Savvas Etairidis Scott Furry From c5deb0a631f029bbfa85feff29a22ffa1f2d703b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Wed, 26 Jul 2023 07:54:59 +0200 Subject: [PATCH 06/16] Fix a typo in error message (#5264) --- lib/checkclass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index abf8319241c..a8573b2c8c5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2582,7 +2582,7 @@ void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const st "passed to the function. This change should not cause compiler errors but it does not " "necessarily make sense conceptually. Think about your design and the task of the function first - " "is it a function that must not access members of class instances? And maybe it is more appropriate " - "to move this function to a unnamed namespace.", CWE398, Certainty::inconclusive); + "to move this function to an unnamed namespace.", CWE398, Certainty::inconclusive); } //--------------------------------------------------------------------------- From 265759dfa4c901e89e0dce4a5ed5dd26b720efc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 27 Jul 2023 07:27:04 +0200 Subject: [PATCH 07/16] tokenlist.cpp: fixed GCC `-Wunused-variable` warning (#5266) Added https://trac.cppcheck.net/ticket/11847 about the false negative. --- lib/tokenlist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index b4af023ba0d..6a8a5edc858 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1747,7 +1747,7 @@ void TokenList::validateAst() const continue; } - if (const Token* lambdaEnd = findLambdaEndToken(tok)) { // skip lambda captures + if (findLambdaEndToken(tok)) { // skip lambda captures tok = tok->link(); continue; } From f10851dc372163baccea8845c03123a574b5c3d5 Mon Sep 17 00:00:00 2001 From: Tobias Date: Thu, 27 Jul 2023 07:50:46 +0200 Subject: [PATCH 08/16] posix.cfg: erand48/nrand48/jrand48 fixed argument direction (#5253) --- cfg/posix.cfg | 6 +++--- test/cfg/posix.c | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cfg/posix.cfg b/cfg/posix.cfg index f07f8fe8b44..7cde306a3ad 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -2197,7 +2197,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s - + @@ -2209,7 +2209,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s - + @@ -2221,7 +2221,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s - + diff --git a/test/cfg/posix.c b/test/cfg/posix.c index 3da4959536a..0621e983618 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -330,6 +330,13 @@ double nullPointer_erand48(unsigned short xsubi[3]) return erand48(xsubi); } +struct non_const_parameter_erand48_struct { unsigned short xsubi[3]; }; +// No warning is expected that dat can be const +double non_const_parameter_erand48(struct non_const_parameter_erand48_struct *dat) +{ + return erand48(dat->xsubi); +} + unsigned short *nullPointer_seed48(unsigned short seed16v[3]) { // cppcheck-suppress nullPointer From 99f7f88f39e3c2780abe029e3c39fcd7ae88ddb4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 27 Jul 2023 20:54:12 +0200 Subject: [PATCH 09/16] Fix FN constParameterPointer (#5270) Co-authored-by: chrchr-github --- lib/astutils.cpp | 2 +- test/testother.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 3652eefbe0d..94307ab276f 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2196,7 +2196,7 @@ T* getTokenArgumentFunctionImpl(T* tok, int& argn) parent = parent->astParent(); // passing variable to subfunction? - if (Token::Match(parent, "[(,{]")) + if (Token::Match(parent, "[[(,{]")) ; else if (Token::simpleMatch(parent, ":")) { while (Token::Match(parent, "[?:]")) diff --git a/test/testother.cpp b/test/testother.cpp index 91d1beef558..3a366df4474 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3754,6 +3754,13 @@ class TestOther : public TestFixture { " p = q;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("struct S { int a[1]; };\n" + "void f(S* s) {\n" + " if (s->a[0]) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as pointer to const\n", + errout.str()); } void switchRedundantAssignmentTest() { From bfaa7c075a145184b849ce5bc0519e47a38320ba Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 27 Jul 2023 22:18:34 +0200 Subject: [PATCH 10/16] Fix #11845 FP variableScope if buffer is passed to a conditionally called function (#5265) Co-authored-by: chrchr-github --- lib/checkother.cpp | 12 +++++++++++- lib/checkother.h | 2 +- test/testother.cpp | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2a167a068aa..22b57edca24 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -991,7 +991,7 @@ void CheckOther::checkVariableScope() } } -bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& used) +bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& used) const { const Scope* scope = tok->next()->scope(); bool loopVariable = scope->isLoopScope(); @@ -1071,6 +1071,16 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us if (scope->bodyStart && scope->bodyStart->isSimplifiedScope()) return false; // simplified if/for/switch init statement } + if (var->isArrayOrPointer()) { + int argn{}; + if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { // var passed to function? + if (ftok->function() && Function::returnsPointer(ftok->function())) + return false; + const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned + if (!ret.empty() && ret.back() == '*') + return false; + } + } } } diff --git a/lib/checkother.h b/lib/checkother.h index 14e0409145e..747f1e63857 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -123,7 +123,7 @@ class CPPCHECKLIB CheckOther : public Check { /** @brief %Check scope of variables */ void checkVariableScope(); - static bool checkInnerScope(const Token *tok, const Variable* var, bool& used); + bool checkInnerScope(const Token *tok, const Variable* var, bool& used) const; /** @brief %Check for comma separated statements in return */ void checkCommaSeparatedReturn(); diff --git a/test/testother.cpp b/test/testother.cpp index 3a366df4474..48c6700489c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -106,6 +106,7 @@ class TestOther : public TestFixture { TEST_CASE(varScope32); // #11441 TEST_CASE(varScope33); TEST_CASE(varScope34); + TEST_CASE(varScope35); TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1636,6 +1637,27 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout.str()); } + void varScope35() { // #11845 + check("void f(int err, const char* src) {\n" + " const char* msg = \"Success\";\n" + " char buf[42];\n" + " if (err != 0)\n" + " msg = strcpy(buf, src);\n" + " printf(\"%d: %s\\n\", err, msg);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("char* g(char* dst, const char* src);\n" + "void f(int err, const char* src) {\n" + " const char* msg = \"Success\";\n" + " char buf[42];\n" + " if (err != 0)\n" + " msg = g(buf, src);\n" + " printf(\"%d: %s\\n\", err, msg);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) void checkOldStylePointerCast_(const char code[], const char* file, int line) { // Clear the error buffer.. From e7a2585e0a3d5e8d4d1f0a992699c2e331a5ed6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Tue, 1 Aug 2023 11:21:23 +0200 Subject: [PATCH 11/16] noExplicitConstructor - Verbose error message edit (#5260) The goal is to use keywords like "converting constructor" and "implicit conversion" that will help users further study what this warning is about. I think that the old message provided no context to the uninitiated. --- lib/checkclass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a8573b2c8c5..934d237b7a8 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1067,7 +1067,7 @@ void CheckClass::noConstructorError(const Token *tok, const std::string &classna void CheckClass::noExplicitConstructorError(const Token *tok, const std::string &classname, bool isStruct) { const std::string message(std::string(isStruct ? "Struct" : "Class") + " '$symbol' has a constructor with 1 argument that is not explicit."); - const std::string verbose(message + " Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided."); + const std::string verbose(message + " Such, so called \"Converting constructors\", should in general be explicit for type safety reasons as that prevents unintended implicit conversions."); reportError(tok, Severity::style, "noExplicitConstructor", "$symbol:" + classname + '\n' + message + '\n' + verbose, CWE398, Certainty::normal); } From 5a322365a674360f27492433f9961eece0e585f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 1 Aug 2023 13:06:12 +0200 Subject: [PATCH 12/16] fixed `scriptcheck.yml` (#5268) --- .github/workflows/scriptcheck.yml | 5 ++--- addons/misra_9.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/scriptcheck.yml b/.github/workflows/scriptcheck.yml index 13b87c37ff5..6ef6d90ee4c 100644 --- a/.github/workflows/scriptcheck.yml +++ b/.github/workflows/scriptcheck.yml @@ -37,12 +37,11 @@ jobs: needs: build # 'ubuntu-22.04' removes Python 2.7, 3.5 and 3.6 so keep the previous LTS version + # 'ubutunu-20.04' no longer works on 2.7 - TODO: re-added in a different way or remove support for it? runs-on: ubuntu-20.04 - container: - image: python:2.7.18-buster strategy: matrix: - python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9, '3.10', '3.11'] + python-version: [3.5, 3.6, 3.7, 3.8, 3.9, '3.10', '3.11'] fail-fast: false steps: diff --git a/addons/misra_9.py b/addons/misra_9.py index 02940c440f7..f0f73371906 100644 --- a/addons/misra_9.py +++ b/addons/misra_9.py @@ -418,7 +418,7 @@ def misra_9_x(self, data, rule, rawTokens = None): has_var = True continue unknown_constant = True - cppcheckdata.reportError(sz, 'error', f'Unknown constant {t.str}, please review configuration', 'misra', 'config') + cppcheckdata.reportError(sz, 'error', 'Unknown constant {}, please review configuration'.format(t.str), 'misra', 'config') has_config_errors = True if t.isArithmeticalOp: tokens += [t.astOperand1, t.astOperand2] From bc54fab98941b5539e9fcdafcf0cb674639f1f33 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Aug 2023 14:28:13 +0200 Subject: [PATCH 13/16] Workaround for CI failure (#5285) From https://github.com/llvm/llvm-project/issues/64182#issuecomment-1658085767 --- .github/workflows/asan.yml | 1 + .github/workflows/clang-tidy.yml | 1 + .github/workflows/tsan.yml | 1 + .github/workflows/ubsan.yml | 1 + 4 files changed, 4 insertions(+) diff --git a/.github/workflows/asan.yml b/.github/workflows/asan.yml index df2cb0534d4..fdc029ad9f8 100644 --- a/.github/workflows/asan.yml +++ b/.github/workflows/asan.yml @@ -39,6 +39,7 @@ jobs: - name: Install clang run: | + sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14 wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 16 diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 5352c28118a..c8546db9ab2 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -27,6 +27,7 @@ jobs: - name: Install clang run: | + sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14 wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 16 diff --git a/.github/workflows/tsan.yml b/.github/workflows/tsan.yml index fc3af112481..5a5aaa2c900 100644 --- a/.github/workflows/tsan.yml +++ b/.github/workflows/tsan.yml @@ -39,6 +39,7 @@ jobs: - name: Install clang run: | + sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14 wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 16 diff --git a/.github/workflows/ubsan.yml b/.github/workflows/ubsan.yml index 1de89f66ff5..ed6d4e61f6c 100644 --- a/.github/workflows/ubsan.yml +++ b/.github/workflows/ubsan.yml @@ -39,6 +39,7 @@ jobs: - name: Install clang run: | + sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14 wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 16 From 4452ae6d09d2195c271e6663a69502047f2780db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 1 Aug 2023 22:56:44 +0200 Subject: [PATCH 14/16] iwyu.yml: applied `apt.llvm.org` workaround as well [skip ci] (#5286) --- .github/workflows/iwyu.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/iwyu.yml b/.github/workflows/iwyu.yml index 01c705b9da5..a6773e9938e 100644 --- a/.github/workflows/iwyu.yml +++ b/.github/workflows/iwyu.yml @@ -107,6 +107,7 @@ jobs: - name: Install clang run: | + sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14 wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 17 From 931a59a724679967d18c3c7b08793564703a3499 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:56:24 +0200 Subject: [PATCH 15/16] Fix FN variableScope (#5273) Co-authored-by: chrchr-github --- lib/checkother.cpp | 12 +++++++----- test/testother.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 22b57edca24..a0d2677ad58 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1074,11 +1074,13 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us if (var->isArrayOrPointer()) { int argn{}; if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { // var passed to function? - if (ftok->function() && Function::returnsPointer(ftok->function())) - return false; - const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned - if (!ret.empty() && ret.back() == '*') - return false; + if (ftok->next()->astParent()) { // return value used? + if (ftok->function() && Function::returnsPointer(ftok->function())) + return false; + const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned + if (!ret.empty() && ret.back() == '*') + return false; + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index 48c6700489c..2024a031f1d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1656,6 +1656,16 @@ class TestOther : public TestFixture { " printf(\"%d: %s\\n\", err, msg);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("char* g(char* dst, const char* src);\n" + "void f(int err, const char* src) {\n" + " const char* msg = \"Success\";\n" + " char buf[42];\n" + " if (err != 0)\n" + " g(buf, src);\n" + " printf(\"%d: %s\\n\", err, msg);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'buf' can be reduced.\n", errout.str()); } #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) From 389e446dc05297e72d4f2023ef3625a84b95aa9c Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 2 Aug 2023 04:29:19 -0400 Subject: [PATCH 16/16] Fix 11848: Assert failure in getParentValueTypes() (#5274) --- lib/symboldatabase.cpp | 28 ++++++++++++++++++++-------- lib/symboldatabase.h | 2 +- test/testvalueflow.cpp | 8 ++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 14e7ece42ad..99e3f48153d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3490,16 +3490,28 @@ const Token *Type::initBaseInfo(const Token *tok, const Token *tok1) return tok2; } -const std::string& Type::name() const +std::string Type::name() const { - const Token* next = classDef->next(); + const Token* start = classDef->next(); if (classScope && classScope->enumClass && isEnumType()) - return next->strAt(1); - if (next->str() == "class") - return next->strAt(1); - if (next->isName()) - return next->str(); - return emptyString; + start = start->tokAt(1); + else if (start->str() == "class") + start = start->tokAt(1); + else if (!start->isName()) + return emptyString; + const Token* next = start; + while (Token::Match(next, "::|<|>|(|)|[|]|*|&|&&|%name%")) { + if (Token::Match(next, "<|(|[") && next->link()) + next = next->link(); + next = next->next(); + } + std::string result; + for (const Token* tok = start; tok != next; tok = tok->next()) { + if (!result.empty()) + result += ' '; + result += tok->str(); + } + return result; } void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index ffd3217634e..aa7d644a69a 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -131,7 +131,7 @@ class CPPCHECKLIB Type { } } - const std::string& name() const; + std::string name() const; const std::string& type() const { return classDef ? classDef->str() : emptyString; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 2592eeec69a..4e322596175 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6812,6 +6812,14 @@ class TestValueFlow : public TestFixture { " dummy_resource::log.clear();\n" "}\n"; valueOfTok(code, "log"); + + code = "struct D : B {\n" + " D(int i, const std::string& s) : B(i, s) {}\n" + "};\n" + "template<> struct B::S {\n" + " int j;\n" + "};\n"; + valueOfTok(code, "B"); } void valueFlowCrash() {