public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101872] New: static_cast succeeds in CRTP with incorrect type
@ 2021-08-12  8:11 vpervouchine at gmail dot com
  2021-08-12 15:17 ` [Bug c++/101872] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: vpervouchine at gmail dot com @ 2021-08-12  8:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101872
           Summary: static_cast succeeds in CRTP with incorrect type
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vpervouchine at gmail dot com
  Target Milestone: ---

Created attachment 51296
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51296&action=edit
Preprocessor output. See the actual code from line 11011 onwards

static_cast of *this to an incorrect "Derived" class inside a CRTP base
succeeds when Derived itself is a variadic template, leading to invalid
addressing of member variables of the derived class.
Since the preprocessed file doesn't include comments, please see them in the
source file here: https://godbolt.org/z/oKv1xeK9G

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/101872] static_cast succeeds in CRTP with incorrect type
  2021-08-12  8:11 [Bug c++/101872] New: static_cast succeeds in CRTP with incorrect type vpervouchine at gmail dot com
@ 2021-08-12 15:17 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-12 15:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Original testcase (since we won't know if godbolt will be around in 10-20 years
...):

#include <tuple>

template <template <typename> typename ContainerImpl, typename Policy>
struct BaseContainer {
    using Impl = ContainerImpl<Policy>; // this isn't our actual Impl (missing
Handlers template args...)...

    Impl & impl() { return static_cast<Impl&>(*this); } // this shouldn't
compile

    int start() {
        return impl().on_start(); // ... so impl() casts this to incorrect
value
    }
};

template <typename Policy, typename ...Handlers>
struct BaseUiContainer : BaseContainer<BaseUiContainer, Policy> {

    int on_start() {
        return m_x * 2; // ... and because of the wrong cast this->m_x is
pointing elsewhere
    }

private:
    std::tuple<Handlers...> m_handlers; // comment this out to get the correct
output of 4
    int m_x = 2;
};

struct Handler {
    char a[128];
};

template <typename Policy>
struct MyUiContainer : BaseUiContainer<Policy, Handler> {
};

int main() {
    struct P {};
    MyUiContainer<P> container;
    return container.start();
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-12 15:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-12  8:11 [Bug c++/101872] New: static_cast succeeds in CRTP with incorrect type vpervouchine at gmail dot com
2021-08-12 15:17 ` [Bug c++/101872] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).