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

clang-analyzer-alpha.cplusplus.DeleteWithNonVirtualDtor does not handle indirection through base pointer #117230

Open
tiagomacarios opened this issue Nov 21, 2024 · 3 comments

Comments

@tiagomacarios
Copy link
Contributor

The following code will trigger clang-analyzer-alpha.cplusplus.DeleteWithNonVirtualDtor
https://godbolt.org/z/7jPa8dr5W

#include <cstdio>

struct A {};

struct B : A {
    virtual ~B() { std::puts("B dtor"); }
};

struct C : B {
    ~C() { std::puts("C dtor"); }
};

int main() {
    C* c1 = new C{};
    C* c2 = nullptr;

    A** pp = (A**)&c2; // note: Casting from 'C' to 'A' here
    *pp = c1;

    delete c2; // warning: Destruction of a polymorphic object with no virtual destructor [clang-analyzer-alpha.cplusplus.DeleteWithNonVirtualDtor]
}
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

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

Author: Tiago (tiagomacarios)

The following code will trigger clang-analyzer-alpha.cplusplus.DeleteWithNonVirtualDtor https://godbolt.org/z/7jPa8dr5W ``` #include <cstdio>

struct A {};

struct B : A {
virtual ~B() { std::puts("B dtor"); }
};

struct C : B {
~C() { std::puts("C dtor"); }
};

int main() {
C* c1 = new C{};
C* c2 = nullptr;

A** pp = (A**)&amp;c2; // note: Casting from 'C' to 'A' here
*pp = c1;

delete c2; // warning: Destruction of a polymorphic object with no virtual destructor [clang-analyzer-alpha.cplusplus.DeleteWithNonVirtualDtor]

}

</details>

@steakhal
Copy link
Contributor

Yea, the checker should have checked if the pointer is null and ignore it if its null. I guess, there are other reasons too why this checker is alpha (aka. experimental).

Thanks for the report.

@YehezkelShB
Copy link

It isn't null, after *pp = c1, but A** pp = (A**)&c2; seems like UB to me (static_cast<A**>(&c2) doesn't compile here, so this is effectively a reinterpret_cast), thus I won't blame the analyzer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants