Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2017,8 +2017,8 @@ void CheckOtherImpl::checkConstPointer()
pointers.emplace(var);
const Token* parent = tok->astParent();
enum Deref : std::uint8_t { NONE, DEREF, MEMBER } deref = NONE;
bool hasIncDecPlus = false;
if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlus = parent->isIncDecOp()) || (hasIncDecPlus = (parent->str() == "+"))) &&
bool hasIncDecPlusMinus = false;
if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlusMinus = parent->isIncDecOp()) || (hasIncDecPlusMinus = Token::Match(parent, "[+-]"))) &&
parent->astParent() && parent->astParent()->isUnaryOp("*"))))
deref = DEREF;
else if (Token::simpleMatch(parent, "[") && parent->astOperand1() == tok && tok != nameTok)
Expand Down Expand Up @@ -2054,7 +2054,7 @@ void CheckOtherImpl::checkConstPointer()
}
}
}
if (hasIncDecPlus) {
if (hasIncDecPlusMinus) {
parent = gparent;
gparent = gparent ? gparent->astParent() : nullptr;
}
Expand Down Expand Up @@ -2089,9 +2089,11 @@ void CheckOtherImpl::checkConstPointer()
}
} else {
int argn = -1;
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;"))
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|<<|;"))
continue;
if (hasIncDecPlus && (!parent->astParent() || parent->astParent()->str() == ";"))
if (Token::simpleMatch(parent, "-") && parent->valueType() && parent->valueType()->pointer == 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This drops warnings when the pointer subtraction is only read. int f(int* p) { return *(p - 1); }, char c = *(p - 1); and return (p - 1)[0]; all gave constParameterPointer before this PR and give nothing now, while *(p + 1) is still reported. The deref detection above (around line 2021) handles + through hasIncDecPlus but not -, so *(p - 1) reaches this branch and marks p as non-const.

Treating - like + there (parent->str() == "+" || parent->str() == "-") should keep these warnings and still fix #15219.

Also, when the - token has no valueType (e.g. p - n > 0 with n of an unknown type), this no longer continues, so that case is a new false negative too.

continue;
if (hasIncDecPlusMinus && (!parent->astParent() || parent->astParent()->str() == ";"))
continue;
if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while"))
continue;
Expand Down
9 changes: 9 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5087,6 +5087,15 @@ class TestOther : public TestFixture {
"void g(__attribute__((unused)) int *p) {}\n"
"void h(int *p) {}\n");
ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());

check("int* f(int* s) {\n" // #15219
" int* q = s - 1;\n"
" return q;\n"
"}\n"
"int g(int* p) {\n"
" return *(p - 1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());
}

void constArray() {
Expand Down
Loading