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"