Skip to content

False positive from Sanitizer: undefined-behaviorΒ #59060

@Fedr

Description

@Fedr

In the program:

struct A {
    constexpr A();
    virtual void f() {}
};

struct B : A {};

constexpr A::A() { 
    (void)static_cast<B*>(this);
}

int main() {
    B b;
}

compiled with -fsanitize=undefined flag, Clang reports:

/app/example.cpp:9:11: runtime error: downcast of address 0x7ffca2ddc908 which does not point to an object of type 'B'
0x7ffca2ddc908: note: object is of type 'A'
 fc 7f 00 00  a8 4c df bc 66 55 00 00  00 00 00 00 00 00 00 00  83 e0 7c 61 10 7f 00 00  b0 ca c9 61
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for 'A'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /app/example.cpp:9:11 in 

Online demo: https://godbolt.org/z/axEPzczs8

But it does not look right, since the pointer after the cast is not used, and it can be used after finishing of B constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions