https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98206

            Bug ID: 98206
           Summary: UBSan: Casting from multiple inheritance base to
                    derived class triggers undefined behavior sanitizer
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rbock at eudoxos dot de
  Target Milestone: ---

The following code incorrectly triggers undefined behavior sanitizer:

// -------------------------------------
#include <iostream>
#include <string_view>

template<typename Derived>
struct Base1
{
    char c1;
};

template<typename Derived>
struct Base2
{
    char c2;
    auto& get2() const
    {
        return static_cast<const Derived&>(*this);
    }
};

struct X : public Base1<X>, public Base2<X>
{
    X(std::string_view d) : data{d} {}

    std::string_view data;
};


int main()
{
    auto x = X{"cheesecake"};

    std::cout << x.get2().data << std::endl;
}
// -------------------------------------


See also discussion here:
https://stackoverflow.com/a/65178320/2173029

Reply via email to