public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "aeiken at motortech dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/106924] New: Nested class: virtual function returns wrong pointer of covariant type
Date: Tue, 13 Sep 2022 10:07:51 +0000	[thread overview]
Message-ID: <bug-106924-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 106924
           Summary: Nested class: virtual function returns wrong pointer
                    of covariant type
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aeiken at motortech dot de
  Target Milestone: ---

Created attachment 53570
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53570&action=edit
test.ii file generated with g++ -save-temps test.cpp

The following was found with gcc-arm-none-eabi-10.3-2021.07, tests with other
versions for linux (Ubuntu 20.04.3 LTS) did also not work (e.g. g++ 9.4.0 on
Ubuntu).

Take a look at the example code in test.cpp:
A class (ABFabNested) is nested inside a class (class DerivedAB with multiple
inheritances (class BaseA and BaseB)).
Because of the class layout, the adresses of DerivedAB and BaseB are different.
The nested class itself is derived from a Base class (FabForB) which has a
virtual function(getPtr), which return a pointer to class BaseA.
The nested class overrides this virtual function, but returns a pointer to
class DerivedAB. This is allowed, because DerivedAB and BaseB are covariants.

If you have an instance of a pointer to FabForB, which points to an instance of
ABFabNested, getPtr returns the address of DerivedAB, not BaseA.
If you implement the same class (here ABFab) outside of DerivedAB (not nested)
and repeat the step before, getPtr returns the address of BaseA, which is what
I expected.

test.cpp was compiled with the 9.4.0 version of the g++ without any parameters:

g++ a.out

Output of a.out:
TEST fab:        addr e89f6030 base e89f6040
TEST             addr e89f6040 
TEST fab nested: addr e89f6010 base e89f6020
TEST nested      addr e89f6010

g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The test was repeated with other g++ versions (also newer version) with the
same result.


test.cpp:
#include <stdio.h>
  class BaseA
  {
  public:
    virtual BaseA* getPtr(){return this;}
    virtual ~BaseA(){}
  };
  class BaseB
  {
  public:
    int i = 0;
    virtual int geti(){return i;};
    virtual ~BaseB(){}
  };
  class FabForB
  {
  public:
    virtual BaseA* getPtr()=0;
    virtual ~FabForB(){}
  };
  class DerivedAB: public BaseB, public BaseA
  {
  public:
    virtual DerivedAB* getPtr(){printf("TEST D this %x\n", this);return this;}
    virtual int geti(){return 5;};

    class ABFabNested: public FabForB
    {
    public:
      virtual DerivedAB* getPtr()
      {
        static DerivedAB staticD;
        BaseA* basePtr = &staticD;
        printf("TEST fab nested: addr %x base %x\n", &staticD, basePtr);
        return &staticD;
      }
      virtual int geti(){return 5;};
    };
  };
  class ABFab: public FabForB
  {
  public:
    virtual DerivedAB* getPtr()
    {
      static DerivedAB staticD;
      BaseA* basePtr = &staticD;
      printf("TEST fab:        addr %x base %x\n", &staticD, basePtr);
      return &staticD;
    }
    virtual int geti(){return 5;};
  };
int main()
{
  ABFab fab;
  DerivedAB::ABFabNested fabNested;

  FabForB* fabPtr = &fab;
  FabForB* fabNestedPtr = &fabNested;
  printf("TEST             addr %x \n", fabPtr->getPtr());
  printf("TEST nested      addr %x \n", fabNestedPtr->getPtr());  
  return 0;
}

             reply	other threads:[~2022-09-13 10:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13 10:07 aeiken at motortech dot de [this message]
2022-09-14 10:10 ` [Bug c++/106924] " redi at gcc dot gnu.org
2022-09-14 10:19 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-106924-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).