public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111531] New: Bound member function (-Wno-pmf-conversions) with multiple inheritance
@ 2023-09-22  0:30 paulhaile3 at gmail dot com
  2023-09-22  0:47 ` [Bug c++/111531] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: paulhaile3 at gmail dot com @ 2023-09-22  0:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111531
           Summary: Bound member function (-Wno-pmf-conversions) with
                    multiple inheritance
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: paulhaile3 at gmail dot com
  Target Milestone: ---

I noticed a bug with bound pointer to member functions that I would like to
report. When using multiple inheritance, the this pointer is incorrect and
results in undefined behavior. See the example below, which is modified from
https://github.com/llvm/llvm-project/issues/22495. The last set of addresses
are incorrect - e.g. this resolves to the address of struct B, instead of A,
which means accessing the data member this->x is undefined.

"""
#include <cstdio>

struct A {
int x;
void f() { 
    printf("A-in-B [A::f]: %p\n", this);
    printf("address of x: %p, value of x: %d\n", &this->x, this->x);
}
};
struct X { char y; };
struct B : X, A {};

typedef void (B::*b_mp)();
typedef void (*b_fptr)(B *);

int main() {

B b;
b.x = 100;
b_mp mp = &A::f;
printf("B: [main]: %p\n", &b);
printf("address of x: %p, value of x: %d\n", &b.x, b.x);
printf("A-in-B [main]: %p\n", (A *)&b);
(b.*mp)();
b_fptr fp = (b_fptr)(&A::f);
fp(&b);
}
"""

Output:
B: [main]: 0x7fff4772e958
address of x: 0x7fff4772e95c, value of x: 100
A-in-B [main]: 0x7fff4772e95c
A-in-B [A::f]: 0x7fff4772e95c
address of x: 0x7fff4772e95c, value of x: 100
A-in-B [A::f]: 0x7fff4772e958
address of x: 0x7fff4772e958, value of x: 0
Compiled with x86-64 gcc 13.2 with flags "-O3 --std=c++20 -Wno-pmf-conversions"

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

end of thread, other threads:[~2023-09-22 15:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-22  0:30 [Bug c++/111531] New: Bound member function (-Wno-pmf-conversions) with multiple inheritance paulhaile3 at gmail dot com
2023-09-22  0:47 ` [Bug c++/111531] " pinskia at gcc dot gnu.org
2023-09-22  1:12 ` [Bug c++/111531] Bound member function with multiple inheritance documentation should be clearer pinskia at gcc dot gnu.org
2023-09-22 12:16 ` paulhaile3 at gmail dot com
2023-09-22 15:47 ` paulhaile3 at gmail dot com

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).