From 753501cd4744b4cc154805f77728a5c60392e308 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 8 Sep 2025 20:09:27 +0200 Subject: [PATCH 1/6] Fix #14119 FN constVariablePointer for variable declared in for loop --- lib/checkother.cpp | 4 ++-- test/testother.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 5af22c7b2c5..3bf4dda3e3c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1893,7 +1893,7 @@ void CheckOther::checkConstPointer() if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst()) takingRef = true; if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 && - parent->valueType() && parent->valueType()->pointer) + parent->valueType() && parent->valueType()->pointer && lhs->variable() != var) nonConstPtrAssignment = true; if (!takingRef && !nonConstPtrAssignment) continue; @@ -1910,7 +1910,7 @@ void CheckOther::checkConstPointer() } } else { int argn = -1; - if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<")) + if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;")) continue; if (hasIncDecPlus && !parent->astParent()) continue; diff --git a/test/testother.cpp b/test/testother.cpp index a0583ec13df..34c90e8771a 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4497,6 +4497,20 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:2:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:5:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str()); + + check("struct S { S* next; };\n" // #14119 + "void f(S* s) {\n" + " for (S* p = s->next; p != nullptr; p = p->next) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3:13]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); + + check("void f(int* p) {\n" + " for (int* q = p; q;)\n" + " break;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); } void constArray() { From 94c2aa3e3d6dba52806ca7da7f1a9b18ff63c8a4 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 8 Sep 2025 20:31:49 +0200 Subject: [PATCH 2/6] const --- externals/simplecpp/simplecpp.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/externals/simplecpp/simplecpp.cpp b/externals/simplecpp/simplecpp.cpp index 5093b4b74f2..152d50a6c73 100755 --- a/externals/simplecpp/simplecpp.cpp +++ b/externals/simplecpp/simplecpp.cpp @@ -2516,7 +2516,7 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::mapnext; + const simplecpp::Token *tok2 = tok1->next; if (!tok2) { throw std::runtime_error("missing sizeof argument"); } @@ -2531,7 +2531,7 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::mapnext) { + for (const simplecpp::Token *typeToken = tok1; typeToken != tok2; typeToken = typeToken->next) { if ((typeToken->str() == "unsigned" || typeToken->str() == "signed") && typeToken->next->name) continue; if (typeToken->str() == "*" && type.find('*') != std::string::npos) @@ -2586,7 +2586,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI if (!tok1) { throw std::runtime_error("missing __has_include argument"); } - simplecpp::Token *tok2 = tok1->next; + const simplecpp::Token *tok2 = tok1->next; if (!tok2) { throw std::runtime_error("missing __has_include argument"); } @@ -2604,7 +2604,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI const bool systemheader = (tok1 && tok1->op == '<'); std::string header; if (systemheader) { - simplecpp::Token *tok3 = tok1->next; + const simplecpp::Token *tok3 = tok1->next; if (!tok3) { throw std::runtime_error("missing __has_include closing angular bracket"); } @@ -2615,7 +2615,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI } } - for (simplecpp::Token *headerToken = tok1->next; headerToken != tok3; headerToken = headerToken->next) + for (const simplecpp::Token *headerToken = tok1->next; headerToken != tok3; headerToken = headerToken->next) header += headerToken->str(); } else { header = tok1->str().substr(1U, tok1->str().size() - 2U); From 70c7ca6abcbd48f0e5642ebbb690066f8d216bd1 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 8 Sep 2025 22:16:14 +0200 Subject: [PATCH 3/6] const --- externals/simplecpp/simplecpp.cpp | 2 +- lib/token.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/externals/simplecpp/simplecpp.cpp b/externals/simplecpp/simplecpp.cpp index 152d50a6c73..6821745c024 100755 --- a/externals/simplecpp/simplecpp.cpp +++ b/externals/simplecpp/simplecpp.cpp @@ -2582,7 +2582,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) { if (tok->str() != HAS_INCLUDE) continue; - simplecpp::Token *tok1 = tok->next; + const simplecpp::Token *tok1 = tok->next; if (!tok1) { throw std::runtime_error("missing __has_include argument"); } diff --git a/lib/token.cpp b/lib/token.cpp index 98f79b4bb1d..e1d075b693a 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2669,7 +2669,7 @@ void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, M bool TokenImpl::getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const { - CppcheckAttributes *attr = mCppcheckAttributes; + const CppcheckAttributes *attr = mCppcheckAttributes; while (attr && attr->type != type) attr = attr->next; if (attr) From f8339076fcf889bfaf87be0c83e04749d63e12f4 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 22 Sep 2025 21:38:16 +0200 Subject: [PATCH 4/6] Partial fix for #14148 --- lib/checkother.cpp | 2 +- test/testother.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3bf4dda3e3c..c77871edcef 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1912,7 +1912,7 @@ void CheckOther::checkConstPointer() int argn = -1; if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;")) continue; - if (hasIncDecPlus && !parent->astParent()) + if (hasIncDecPlus && (!parent->astParent() || parent->astParent()->str() == ";")) continue; if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while")) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 34c90e8771a..5ed9537fbce 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4511,6 +4511,16 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n", errout_str()); + + check("void g(const int*);\n" // #14148 + "void f() {\n" + " int a[] = {1, 2, 3};\n" + " for (int* p = a; *p != 3; p++) {\n" + " g(p);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); } void constArray() { From 83407ef7cd1bcf5c27073a0b9617f64330d1a777 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 2 Oct 2025 12:23:21 +0200 Subject: [PATCH 5/6] Update testother.cpp --- test/testother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testother.cpp b/test/testother.cpp index 28db1132366..c9be6eeb319 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4511,7 +4511,7 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n", errout_str()); - + check("struct S { S* next; };\n" // #14119 "void f(S* s) {\n" " for (S* p = s->next; p != nullptr; p = p->next) {}\n" From 49490e3f2f0ee428a5d6917ced70a1d9c8c059eb Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 3 Oct 2025 10:11:32 +0200 Subject: [PATCH 6/6] Update simplecpp.cpp --- externals/simplecpp/simplecpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/externals/simplecpp/simplecpp.cpp b/externals/simplecpp/simplecpp.cpp index 18d270fe248..bfdde8ed481 100755 --- a/externals/simplecpp/simplecpp.cpp +++ b/externals/simplecpp/simplecpp.cpp @@ -2512,7 +2512,7 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::mapnext) { if (tok->str() != "sizeof") continue; - simplecpp::Token *tok1 = tok->next; + const simplecpp::Token *tok1 = tok->next; if (!tok1) { throw std::runtime_error("missing sizeof argument"); }