Skip to content

Fix #12795 (FP knownConditionTrueFalse: object read through pointer may be changed by function call) - #9043

Open
tropibyte wants to merge 1 commit into
cppcheck-opensource:mainfrom
tropibyte:fix-12795
Open

tropibyte wants to merge 1 commit into
cppcheck-opensource:mainfrom
tropibyte:fix-12795

Conversation

@tropibyte

Copy link
Copy Markdown
  • Reading a member through a pointer (p->i, *p, p[i]) is no longer assumed unchanged across a function call, unless the pointer is known to point at a local object, or at memory from new or a library allocation function (malloc, calloc, ...) whose address never escapes the function.
  • Deliberately conservative: passing p or p->i to a function, taking &p->i, binding a reference, calling a member function through p, returning or reassigning p, use in a lambda, placement new, and new of a type with a constructor or base class all keep the check quiet.
  • Known limitation: taking &p->i into a local that never escapes is also treated as an escape, so that rare true positive is not reported.
  • Tests: converts the TODO_ASSERT for #12795 and adds cases for new, calloc (C), static_cast<S*>(std::malloc(...)), and the escape variants.

…ay be changed by function call)

A member read through a local pointer ('p->i') was assumed unchanged across a
function call, so 'o = p->i; modify(); if (p->i == o)' was reported as always
true even when modify() can change the object.

Dereferencing a pointer is now treated as non-local unless the pointer is
known to point at a local object, or at memory from 'new' or a library
allocation function (malloc, calloc, ...) whose address never escapes the
function. 'new' of a type with a constructor or base class is treated
conservatively, since the constructor could store 'this'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant