Repository navigation
Conversation
When the arguments of a call such as f(&d, NULL) are followed together, the tracked value of the first pointer is the address of d. The program memory had no truth value for an address, so after "if (!d1 && !d2) return;" the condition of "d1 ? *d1 : *d2" stayed unknown and the null value of d2 reached *d2. A pointer whose tracked value is the address of an object is now true when used as a bool.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix only works when the argument is written literally as &obj. When the address is first stored in a pointer variable (double *p = &d; f(p, NULL);), the same false positive from #14747 is still reported. That argument has more values (lifetime[Address]=(d), symbolic=(&d) and a token value &), and the null d2 is apparently followed together with one of the values that isObjectAddress does not recognise. I found no regressions: the full testrunner passes. Other ways of writing the condition (!d1 ? … : …, d1 != NULL, d1 == NULL), plus arrays, globals, statics and &s->x, all behave correctly. Real bugs (d1 ? *d2 : 0) are still reported.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | FP of #14747 remains when the address is passed through a pointer variable | AI review | lib/programmemory.cpp:1711 |
🆕 New |
@APX403, please fix, resolve or reply to the open and new comments: #1.
Testing
- I built
cppcheckandtestrunnerin pr/ andcppcheckin base/. Everything built. ./testrunner TestNullPointer TestProgramMemory TestValueFlow TestCondition TestUninitVar TestAutoVariables TestStl TestBufferOverrun: 838 tests, 0 failed. The full./testrunner -q: 5428 tests, 0 failed.- Compared with base:
- The PR's example gives no warning any more (base gives one).
- These conditions are also fixed:
!d1 ? *d2 : *d1,(d1 != NULL) ? *d1 : *d2and(d1 == NULL) ? *d2 : *d1. - Arguments
&s->x,&static_dand&globalare fixed too. - Real bugs are still reported:
d1 ? *d2 : 0and!d1 ? 0 : *d2. A new true positive appears ford1 && 1 ? *d2 : 0, which base missed. - The only side effect is in the error path: the note "Assuming condition is false" on
if (!d1 && !d2)is gone, because the condition is now known.
- The false positive remains when the address goes through a pointer variable:
#include <stddef.h>
void f(double *d1, double *d2)
{
if (!d1 && !d2)
return;
double d3 = d1 ? *d1 : *d2;
(void)d3;
}
void h(void){ double d = 0; double *p = &d; f(p, NULL); }$ pr/cppcheck -q --enable=warning --template='{line}:{column} {id} {message}' h.c
6:29 nullPointer Possible null pointer dereference: d2
Base gives the same output. --debug shows that p at the call has the values {lifetime[Address]=(d),symbolic=(&d),&,!0}, so d1 carries {lifetime[Address]=(d)@1,symbolic=(&d)@1,&@1}. With f(&d, NULL) it carries only lifetime[Address]=(d)@1.
Automated review of 9790581 by Claude. The pull request was built and tested locally.
| { | ||
| if (!expr->valueType() || expr->valueType()->pointer == 0) | ||
| return false; | ||
| return values.size() == 1 && values.front().isLifetimeValue() && |
There was a problem hiding this comment.
AI review
This only matches a lone lifetime[Address] tracked value. If the address is first stored in a variable, the #14747 false positive is still reported:
void f(double *d1, double *d2) {
if (!d1 && !d2) return;
double d3 = d1 ? *d1 : *d2;
(void)d3;
}
void h(void){ double d = 0; double *p = &d; f(p, NULL); }6:29 nullPointer Possible null pointer dereference: d2 (base gives the same).
Here d1 carries {lifetime[Address]=(d)@1, symbolic=(&d)@1, &@1}. The null d2 is apparently followed together with the symbolic or token value, and nothing makes that value true as a bool. Should this case be handled as well, or at least get a TODO test?
|
Test results for commit 9790581 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9078/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
d2is reported as a possible null pointer dereference in the:branch of a ternary, although the early return rules that branch out for the call that passesNULL(#14747):The values of both arguments of
f(&d, NULL)are followed together, so the tracked value ofd1islifetime[Address]=(d). That value takes the place of thed1 != 0that the false condition leaves in the program memory, and the executor has no truth value for an address, so the condition of the ternary stays unknown. Since 32ab9d1 (#14164) the forward analysis goes on into both branches of a?whose condition is unknown (forwardanalyzer.cpp#L223), which is how the nulld2gets to*d2. With an unknown pointer instead of&dthere is no warning.A pointer whose tracked value is the address of an object is now true when it is used as a bool, the way the int constraints that exclude zero already are a few lines below (programmemory.cpp#L2131-L2147). The new test also checks that
d1 ? *d2 : 0still warns aboutd2.I don't have a Trac account, so I can't assign the ticket to myself.