Skip to content

Commit e2efe8e

Browse files
Fix #13085 FN: constVariablePointer for nested array access (danmar#6790)
1 parent a36e896 commit e2efe8e

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/checkother.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,8 +1645,9 @@ void CheckOther::checkConstPointer()
16451645
pointers.emplace(var);
16461646
const Token* parent = tok->astParent();
16471647
enum Deref : std::uint8_t { NONE, DEREF, MEMBER } deref = NONE;
1648-
bool hasIncDec = false;
1649-
if (parent && (parent->isUnaryOp("*") || (hasIncDec = parent->isIncDecOp() && parent->astParent() && parent->astParent()->isUnaryOp("*"))))
1648+
bool hasIncDecPlus = false;
1649+
if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlus = parent->isIncDecOp()) || (hasIncDecPlus = (parent->str() == "+"))) &&
1650+
parent->astParent() && parent->astParent()->isUnaryOp("*"))))
16501651
deref = DEREF;
16511652
else if (Token::simpleMatch(parent, "[") && parent->astOperand1() == tok && tok != nameTok)
16521653
deref = DEREF;
@@ -1672,7 +1673,7 @@ void CheckOther::checkConstPointer()
16721673
}
16731674
if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*"))
16741675
continue;
1675-
if (hasIncDec) {
1676+
if (hasIncDecPlus) {
16761677
parent = gparent;
16771678
gparent = gparent ? gparent->astParent() : nullptr;
16781679
}

test/testother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4199,6 +4199,13 @@ class TestOther : public TestFixture {
41994199
"}\n");
42004200
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n",
42014201
errout_str());
4202+
4203+
check("int f(int* a, int* b, int i) {\n" // #13085
4204+
" a[*(b + i)] = 0;\n"
4205+
" return *(b + i);\n"
4206+
"}\n");
4207+
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n",
4208+
errout_str());
42024209
}
42034210

42044211
void constArray() {

0 commit comments

Comments
 (0)