Skip to content

Commit

Permalink
Fix #13085 FN: constVariablePointer for nested array access (#6790)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Sep 10, 2024
1 parent a36e896 commit e2efe8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,8 +1645,9 @@ void CheckOther::checkConstPointer()
pointers.emplace(var);
const Token* parent = tok->astParent();
enum Deref : std::uint8_t { NONE, DEREF, MEMBER } deref = NONE;
bool hasIncDec = false;
if (parent && (parent->isUnaryOp("*") || (hasIncDec = parent->isIncDecOp() && parent->astParent() && parent->astParent()->isUnaryOp("*"))))
bool hasIncDecPlus = false;
if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlus = parent->isIncDecOp()) || (hasIncDecPlus = (parent->str() == "+"))) &&
parent->astParent() && parent->astParent()->isUnaryOp("*"))))
deref = DEREF;
else if (Token::simpleMatch(parent, "[") && parent->astOperand1() == tok && tok != nameTok)
deref = DEREF;
Expand All @@ -1672,7 +1673,7 @@ void CheckOther::checkConstPointer()
}
if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*"))
continue;
if (hasIncDec) {
if (hasIncDecPlus) {
parent = gparent;
gparent = gparent ? gparent->astParent() : nullptr;
}
Expand Down
7 changes: 7 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4199,6 +4199,13 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n",
errout_str());

check("int f(int* a, int* b, int i) {\n" // #13085
" a[*(b + i)] = 0;\n"
" return *(b + i);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n",
errout_str());
}

void constArray() {
Expand Down

0 comments on commit e2efe8e

Please sign in to comment.