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 <noreply@anthropic.com>

@dmcppcheck dmcppcheck left a comment

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

Main finding: the new escape check only looks at the expression right above the pointer, so a member reached through a nested access can escape without being noticed. pointsToLocalAllocation() calls isLocalDerefUse() on the ->, * or [ token next to the pointer. isLocalDerefUse() then looks only at that token's direct parent. When the access goes one level deeper (p->t.i, p[0].t.i), that parent is another ., so these escapes are not seen:

  • g(p->t.i) where g takes int&
  • gp = &p->t.i
  • p->t.f()

The pointer is then wrongly treated as pointing to a private local allocation, and knownConditionTrueFalse is still reported for p->t.i == o after a function call. The PR says it handles exactly this kind of escape. Base reports this case too, so it is not a regression, but it is a false positive that the PR claims to fix and does not.

I found no other problems. All tests pass, and the cases I tried with local objects, pointers to globals, pointer arguments and arrays of pointers behave as expected.

Comments

# Comment By Where Status
1 Escape check sees only the direct parent; g(p->t.i), &p->t.i and p->t.f() are missed AI review lib/vf_analyzers.cpp:1355 🆕 New

@tropibyte, please fix, resolve or reply to the open and new comments: #1.

Testing

Build: I built cppcheck and testrunner in pr/, and cppcheck in base/.

Unit tests: ./testrunner (all tests): 5419 tests, 0 failed. This includes the new TestCondition cases.

Reproduced false positive:

struct T { int i; };
struct S { T t; int i; };
void modify();
int* gp;
void g(int&);
void f1() {
    S* p = new S{};
    gp = &p->t.i;          // escapes
    const int o = p->t.i;
    modify();
    if (p->t.i == o) {}
    delete p;
}
void f2() {
    S* p = new S{};
    g(p->t.i);             // may store the address
    const int o = p->t.i;
    modify();
    if (p->t.i == o) {}
    delete p;
}

Command: pr/cppcheck -q --enable=style t1.cpp

t1.cpp:11:16: style: Condition 'p->t.i==o' is always true [knownConditionTrueFalse]
t1.cpp:19:16: style: Condition 'p->t.i==o' is always true [knownConditionTrueFalse]

The same thing happens with S* p = new S[2]{}; g(p[0].t.i); ... if (p[0].t.i == o).

Control case: with S* p = get(); the PR stays quiet about p->t.i, while base warns. So the warnings above come from pointsToLocalAllocation() wrongly returning true.

Other cases checked:

  • Local object through a pointer (p = &s): the PR still warns, same as base.
  • Pointer to a global (int* p = &g): no warning.
  • Comparing an element of an array of pointers (a[0] == o): same result as base.

Performance: I timed two generated functions that do 2000–3000 reads through p-> of memory from new.

  • perf.cpp (3000 lines of if (p->a == i) p->b = ...): 1.24s on base, 1.26s with the PR.
  • perf2.cpp (2000 lines of int oN = p->a + N; g(oN); p->b = oN;): 3m12s on base, 3m46s with the PR, about 17% slower. Base is already very slow on this file.

The extra time comes from pointsToLocalAllocation(), which scans the whole scope again each time a p-> read is checked.


Automated review of 8fe6f71 by Claude. The pull request was built and tested locally.

Comment thread lib/vf_analyzers.cpp
return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isPointer()) ||
var->isStatic() || var->isReference() || var->isExtern();
}

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

isLocalDerefUse() looks only at the direct parent of the deref token. For a nested member access like p->t.i or p[0].t.i, that parent is another .. So these escapes are not detected:

  • g(p->t.i) where g takes int&
  • gp = &p->t.i
  • p->t.f()

As a result pointsToLocalAllocation() returns true for an allocation whose member address has escaped. Reproducer:

struct T { int i; };
struct S { T t; };
void modify(); void g(int&);
void f() {
    S* p = new S{};
    g(p->t.i);
    const int o = p->t.i;
    modify();
    if (p->t.i == o) {}   // still: Condition 'p->t.i==o' is always true
    delete p;
}

A possible fix: before applying these checks, walk up through . (and [ where the token is the left operand), so that the outermost member access is the one tested.

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