Skip to content

Fix #14747 FP nullPointer for ternary with previous null pointer check - #9078

Open
APX403 wants to merge 1 commit into
cppcheck-opensource:mainfrom
APX403:fix-14747-ternary-null-check
Open

APX403 wants to merge 1 commit into
cppcheck-opensource:mainfrom
APX403:fix-14747-ternary-null-check

Conversation

@APX403

@APX403 APX403 commented Nov 27, 2026

Copy link
Copy Markdown

d2 is 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 passes NULL (#14747):

#include <stddef.h>
void f(double *d1, double *d2)
{
    if (!d1 && !d2) {
        return;
    }
    double d3 = d1 ? *d1 : *d2;
    (void)d3;
}
void g(void)
{
    double d = 0;
    f(&d, NULL);
}
t14747.c:7:29: warning: Possible null pointer dereference: d2 [nullPointer]

The values of both arguments of f(&d, NULL) are followed together, so the tracked value of d1 is lifetime[Address]=(d). That value takes the place of the d1 != 0 that 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 null d2 gets to *d2. With an unknown pointer instead of &d there 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 : 0 still warns about d2.

I don't have a Trac account, so I can't assign the ticket to myself.

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 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

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 cppcheck and testrunner in pr/ and cppcheck in 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 : *d2 and (d1 == NULL) ? *d2 : *d1.
    • Arguments &s->x, &static_d and &global are fixed too.
    • Real bugs are still reported: d1 ? *d2 : 0 and !d1 ? 0 : *d2. A new true positive appears for d1 && 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.

Comment thread lib/programmemory.cpp
{
if (!expr->valueType() || expr->valueType()->pointer == 0)
return false;
return values.size() == 1 && values.front().isLifetimeValue() &&

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 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?

@dmcppcheck

Copy link
Copy Markdown
Contributor

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/
packages: 1000
Differing warnings: 29 (+8 -21)
AI review: 20 improvement, 3 neutral, 4 regression, 1 unclear (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9078/2026-11-26_23-17-33-9790581c538b-review.txt)
Timing: +0.0% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9078/2026-11-26_23-17-33-9790581c538b_timing.html)

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.

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