Skip to content

Commit 77bfec4

Browse files
Fix #12119 FN constVariablePointer with reassigned pointer (#5592)
1 parent cae27c5 commit 77bfec4

6 files changed

Lines changed: 15 additions & 11 deletions

File tree

lib/astutils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,11 +2543,12 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
25432543
tok2 = skipRedundantPtrOp(tok2, tok2->astParent());
25442544

25452545
if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) {
2546-
if (tok2 == tok2->astParent()->astOperand1())
2546+
if ((indirect == 0 || tok2 != tok) && tok2 == tok2->astParent()->astOperand1())
25472547
return true;
25482548
// Check if assigning to a non-const lvalue
25492549
const Variable * var = getLHSVariable(tok2->astParent());
2550-
if (var && var->isReference() && !var->isConst() && var->nameToken() && var->nameToken()->next() == tok2->astParent()) {
2550+
if (var && var->isReference() && !var->isConst() &&
2551+
((var->nameToken() && var->nameToken()->next() == tok2->astParent()) || var->isPointer())) {
25512552
if (!var->isLocal() || isVariableChanged(var, settings, cpp, depth - 1))
25522553
return true;
25532554
}

lib/checkother.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,8 @@ void CheckOther::checkConstPointer()
17051705
continue;
17061706
if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
17071707
continue;
1708+
if (p->typeStartToken() && !p->typeStartToken()->originalName().empty())
1709+
continue;
17081710
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
17091711
}
17101712
}

lib/clangimport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
992992

993993
mData->enumValue = 0;
994994
Token *enumtok = addtoken(tokenList, "enum");
995-
Token *nametok = nullptr;
995+
const Token *nametok = nullptr;
996996
{
997997
int nameIndex = mExtTokens.size() - 1;
998998
while (nameIndex > colIndex && mExtTokens[nameIndex][0] == '\'')

lib/tokenlist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ void TokenList::simplifyPlatformTypes()
19141914
tok = tok->previous();
19151915
tok->deleteThis();
19161916
}
1917+
tok->originalName(tok->str());
19171918
Token *typeToken;
19181919
if (platformtype->mConstPtr) {
19191920
tok->str("const");
@@ -1930,7 +1931,6 @@ void TokenList::simplifyPlatformTypes()
19301931
tok->insertToken("*");
19311932
tok->insertToken("*");
19321933
} else {
1933-
tok->originalName(tok->str());
19341934
tok->str(platformtype->mType);
19351935
typeToken = tok;
19361936
}

test/cfg/gtk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void g_new_if_test()
269269
int b;
270270
};
271271

272-
struct a * pNew3;
272+
const struct a * pNew3;
273273
if (pNew3 = g_new(struct a, 6)) {
274274
printf("%p", pNew3);
275275
}

test/testother.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8946,14 +8946,14 @@ class TestOther : public TestFixture {
89468946
" state_t *x = NULL;\n"
89478947
" x = dostuff();\n"
89488948
"}");
8949-
ASSERT_EQUALS("", errout.str());
8949+
ASSERT_EQUALS("test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str());
89508950

89518951
check("void f() {\n"
89528952
" state_t *x;\n"
89538953
" x = NULL;\n"
89548954
" x = dostuff();\n"
89558955
"}");
8956-
ASSERT_EQUALS("", errout.str());
8956+
ASSERT_EQUALS("test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str());
89578957

89588958
check("int foo() {\n" // #4420
89598959
" int x;\n"
@@ -9086,7 +9086,8 @@ class TestOther : public TestFixture {
90869086
" barney(x);\n"
90879087
" }\n"
90889088
"}");
9089-
ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n",
9089+
ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n"
9090+
"test.cpp:2:style:Variable 'p' can be declared as pointer to const\n",
90909091
errout.str());
90919092

90929093
check("void foo() {\n"
@@ -9178,14 +9179,14 @@ class TestOther : public TestFixture {
91789179
" a = (void*)0;\n"
91799180
" a = p;\n"
91809181
"}");
9181-
ASSERT_EQUALS("", errout.str());
9182+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str());
91829183

91839184
check("void f() {\n"
91849185
" void* a;\n"
91859186
" a = (void*)0U;\n"
91869187
" a = p;\n"
91879188
"}");
9188-
ASSERT_EQUALS("", errout.str());
9189+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str());
91899190
}
91909191

91919192
void redundantVarAssignment_struct() {
@@ -9442,7 +9443,7 @@ class TestOther : public TestFixture {
94429443
" int *p = NULL;\n"
94439444
" p = dostuff();\n"
94449445
"}");
9445-
ASSERT_EQUALS("", errout.str());
9446+
ASSERT_EQUALS("test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", errout.str());
94469447

94479448
// "trivial" initialization => do not warn
94489449
check("void f() {\n"

0 commit comments

Comments
 (0)