Skip to content

Commit b4587ff

Browse files
committed
Fix #14161 FN constVariablePointer (assignment to const pointer)
1 parent 413c87e commit b4587ff

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

lib/astutils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,10 @@ static bool isExpressionChangedAt(const F& getExprTok,
29162916
aliased = isAliasOf(tok, expr, &i);
29172917
if (!aliased)
29182918
return false;
2919-
if (isVariableChanged(tok, indirect + i, settings, depth))
2919+
i += indirect;
2920+
if (tok->valueType() && tok->valueType()->pointer)
2921+
i = std::min(i, tok->valueType()->pointer);
2922+
if (isVariableChanged(tok, i, settings, depth))
29202923
return true;
29212924
// TODO: Try to traverse the lambda function
29222925
if (Token::Match(tok, "%var% ("))

lib/checkother.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,8 +1916,13 @@ void CheckOther::checkConstPointer()
19161916
continue;
19171917
if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while"))
19181918
continue;
1919-
if (Token::simpleMatch(parent, "=") && parent->astOperand1() == tok)
1920-
continue;
1919+
if (Token::simpleMatch(parent, "=")) {
1920+
const Token* lhs = parent->astOperand1();
1921+
if (lhs == tok)
1922+
continue;
1923+
if (lhs && lhs->valueType() && lhs->valueType()->isConst(vt->pointer))
1924+
continue;
1925+
}
19211926
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) {
19221927
if (ftok->function()) {
19231928
const bool isCastArg = parent->isCast() && !ftok->function()->getOverloadedFunctions().empty(); // assume that cast changes the called function

test/testother.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4497,6 +4497,16 @@ class TestOther : public TestFixture {
44974497
ASSERT_EQUALS("[test.cpp:2:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n"
44984498
"[test.cpp:5:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n",
44994499
errout_str());
4500+
4501+
check("struct T;\n"
4502+
"void use(const T*);\n"
4503+
"void f(T* tok0) {\n"
4504+
" T *tok1 = tok0;\n"
4505+
" const T *tok2 = tok1;\n"
4506+
" use(tok2);\n"
4507+
"}\n");
4508+
ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n",
4509+
errout_str());
45004510
}
45014511

45024512
void constArray() {

0 commit comments

Comments
 (0)