From 1484802547aae4a3d2a28f3d80e8188660f9131f Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 24 Oct 2023 15:56:50 +0200 Subject: [PATCH 1/5] Fix #12119 FN constVariablePointer with reassigned pointer --- lib/astutils.cpp | 4 ++-- test/testother.cpp | 13 +++++++------ test/testuninitvar.cpp | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e735a078fc1..e837c22ca15 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2537,11 +2537,11 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, tok2 = skipRedundantPtrOp(tok2, tok2->astParent()); if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) { - if (tok2 == tok2->astParent()->astOperand1()) + if ((indirect == 0 || tok2 != tok) && tok2 == tok2->astParent()->astOperand1()) return true; // Check if assigning to a non-const lvalue const Variable * var = getLHSVariable(tok2->astParent()); - if (var && var->isReference() && !var->isConst() && var->nameToken() && var->nameToken()->next() == tok2->astParent()) { + if (var && var->isReference() && !var->isConst()) { if (!var->isLocal() || isVariableChanged(var, settings, cpp, depth - 1)) return true; } diff --git a/test/testother.cpp b/test/testother.cpp index b3d0fc0d2b3..6eb18a14215 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -8915,14 +8915,14 @@ class TestOther : public TestFixture { " state_t *x = NULL;\n" " x = dostuff();\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str()); check("void f() {\n" " state_t *x;\n" " x = NULL;\n" " x = dostuff();\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str()); check("int foo() {\n" // #4420 " int x;\n" @@ -9055,7 +9055,8 @@ class TestOther : public TestFixture { " barney(x);\n" " }\n" "}"); - ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n", + ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n" + "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", errout.str()); check("void foo() {\n" @@ -9147,14 +9148,14 @@ class TestOther : public TestFixture { " a = (void*)0;\n" " a = p;\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str()); check("void f() {\n" " void* a;\n" " a = (void*)0U;\n" " a = p;\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str()); } void redundantVarAssignment_struct() { @@ -9411,7 +9412,7 @@ class TestOther : public TestFixture { " int *p = NULL;\n" " p = dostuff();\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", errout.str()); // "trivial" initialization => do not warn check("void f() {\n" diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 74ca1a898dc..144283e7294 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6269,7 +6269,7 @@ class TestUninitVar : public TestFixture { " int i;\n" " x = i;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", "", errout.str()); valueFlowUninit("void f() {\n" // #11890 " int x;\n" From 5f6dd434516bd84a0b2a514d303a16ef435f0422 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 24 Oct 2023 16:42:14 +0200 Subject: [PATCH 2/5] const --- test/cfg/gtk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index 74c947a596b..b7e4f21485d 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -269,7 +269,7 @@ void g_new_if_test() int b; }; - struct a * pNew3; + const struct a * pNew3; if (pNew3 = g_new(struct a, 6)) { printf("%p", pNew3); } From ad78142d8a59f91882469c84325ccb999d159f02 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 24 Oct 2023 17:14:20 +0200 Subject: [PATCH 3/5] const --- lib/clangimport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index b4bba09cdaa..805741fd00b 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -992,7 +992,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList) mData->enumValue = 0; Token *enumtok = addtoken(tokenList, "enum"); - Token *nametok = nullptr; + const Token *nametok = nullptr; { int nameIndex = mExtTokens.size() - 1; while (nameIndex > colIndex && mExtTokens[nameIndex][0] == '\'') From aa6c193e0fa9e380238a4b459a8245d0838f94ca Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 24 Oct 2023 17:25:15 +0200 Subject: [PATCH 4/5] Set originalName for platform types --- lib/checkother.cpp | 2 ++ lib/tokenlist.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d937e625b97..418c9f06bc2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1705,6 +1705,8 @@ void CheckOther::checkConstPointer() continue; if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) continue; + if (p->typeStartToken() && !p->typeStartToken()->originalName().empty()) + continue; constVariableError(p, p->isArgument() ? p->scope()->function : nullptr); } } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 2827b3cc7d0..0b892cc98e1 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1914,6 +1914,7 @@ void TokenList::simplifyPlatformTypes() tok = tok->previous(); tok->deleteThis(); } + tok->originalName(tok->str()); Token *typeToken; if (platformtype->mConstPtr) { tok->str("const"); @@ -1930,7 +1931,6 @@ void TokenList::simplifyPlatformTypes() tok->insertToken("*"); tok->insertToken("*"); } else { - tok->originalName(tok->str()); tok->str(platformtype->mType); typeToken = tok; } From d6743f2d2648609ea09a1e74c4214513ea03573c Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 24 Oct 2023 17:39:45 +0200 Subject: [PATCH 5/5] Fix test --- lib/astutils.cpp | 3 ++- test/testuninitvar.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e837c22ca15..fa6aacb7aa1 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2541,7 +2541,8 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, return true; // Check if assigning to a non-const lvalue const Variable * var = getLHSVariable(tok2->astParent()); - if (var && var->isReference() && !var->isConst()) { + if (var && var->isReference() && !var->isConst() && + ((var->nameToken() && var->nameToken()->next() == tok2->astParent()) || var->isPointer())) { if (!var->isLocal() || isVariableChanged(var, settings, cpp, depth - 1)) return true; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 144283e7294..74ca1a898dc 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6269,7 +6269,7 @@ class TestUninitVar : public TestFixture { " int i;\n" " x = i;\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str()); valueFlowUninit("void f() {\n" // #11890 " int x;\n"