Skip to content

Fix #15267 FN constVariablePointer when an alias of the pointer is moved with += or -= - #9069

Draft
autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-constpointer-compound-assign
Draft

autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-constpointer-compound-assign

Conversation

@autoantwort

Copy link
Copy Markdown
Contributor

Trac: https://trac.cppcheck.net/ticket/15267

int f(int** pp) {
    int* s = *pp;
    *pp += 2;
    return *s;
}

Expected: Variable 's' can be declared as pointer to const [constVariablePointer]. Actual: no warning. Since 4b66976 (#15042) symbolic values also go through pointer casts, so this now also hides a warning that was reported before (e.g. S* s = (S*)(*pp); *pp += 2;).

Cause: the alias (*pp) has the symbolic value s, so findVariableChanged() reports *pp += 2 as a change of *s: isVariableChanged() returns true for the left operand of any assignment. isConstPointerVariable() already accepts the left side of a plain =, but not of += / -=.

Fix: isConstPointerVariable() also accepts the left side of += / -= when it is a pointer: moving the alias changes the pointer, not the pointee. *p += 2 (which writes through the pointer) still prevents the warning (test added).

Verification: new tests fail without the fix, testrunner passes. On a large internal code base the only change vs main is one restored true positive.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

…ved with += or -=

Moving an alias of the pointer does not change the object, like assigning it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants