Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect "Called C++ object pointer is null" report -- when user code NULL-checks the dynamically casted value #134454

Open
vakatov opened this issue Apr 4, 2025 · 1 comment
Labels
clang:static analyzer false-positive Warning fires when it should not

Comments

@vakatov
Copy link

vakatov commented Apr 4, 2025

It looks that when the Static Analyzer sees a code branch which checks for a NULL dynamically casted value, then it (mistakenly) decides that the original pointer therefore is liable to be NULL as well:

class A { public: virtual void f(void) const; };
class B : public A { public: virtual void f(void) const; };

void g(const A* a)
{
    const B* b = dynamic_cast<const B*>(a);
    if (b) {
        b->f();
        return;
    }
    a->f();
}

$ /usr/local/llvm/20.1.1/bin/clang++ --analyze fp.cpp
fp.cpp:11:5: warning: Called C++ object pointer is null [core.CallAndMessage]
   11 |     a->f();
      |     ^~~~~~
1 warning generated.
@firewave firewave added clang:static analyzer false-positive Warning fires when it should not and removed new issue labels Apr 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/issue-subscribers-clang-static-analyzer

Author: Denis Vakatov, NCBI (vakatov)

It looks that when the Static Analyzer sees a code branch which checks for a NULL **dynamically casted** value, then it (mistakenly) decides that the original pointer therefore is liable to be NULL as well:
class A { public: virtual void f(void) const; };
class B : public A { public: virtual void f(void) const; };

void g(const A* a)
{
    const B* b = dynamic_cast&lt;const B*&gt;(a);
    if (b) {
        b-&gt;f();
        return;
    }
    a-&gt;f();
}

$ /usr/local/llvm/20.1.1/bin/clang++ --analyze fp.cpp
fp.cpp:11:5: warning: Called C++ object pointer is null [core.CallAndMessage]
   11 |     a-&gt;f();
      |     ^~~~~~
1 warning generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

3 participants