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

* [Bug c++/111531] Bound member function (-Wno-pmf-conversions) with multiple inheritance
  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 ` 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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-22  0:47 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |documentation

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Bound-member-functions.html


Reading this gives the impression that this situation could be better explained
and such that this is expected behavior.

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

* [Bug c++/111531] Bound member function with multiple inheritance documentation should be clearer
  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 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-22  1:12 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Bound member function       |Bound member function with
                   |(-Wno-pmf-conversions) with |multiple inheritance
                   |multiple inheritance        |documentation should be
                   |                            |clearer
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2023-09-22
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
"the PMF needs to store information about how to adjust the ‘this’ pointer,"

And then it says:
"you can extract the pointer to the function that would be called for a given
object/PMF pair and call it directly inside the inner loop, to save a bit of
time."

Meaning the specifically a Bound member function loses the information on how
to adjust the this pointer and just contains a pointer to the function rather
than anything else.

That is why I said this is just a documentation issue of explaining this in
more clearer langauge.

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

* [Bug c++/111531] Bound member function with multiple inheritance documentation should be clearer
  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
  3 siblings, 0 replies; 5+ messages in thread
From: paulhaile3 at gmail dot com @ 2023-09-22 12:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Paul Haile <paulhaile3 at gmail dot com> ---
Fair enough definitely could be intentional. However, In this example

renaming

typedef void (*b_fptr)(B *);

to

typedef void (*b_fptr)(A *);

gets rid of the error.

It seems restricting the binding such that the object must be the same type as
the class of the member function would solve this. Is there any reason to
allowing the object be any type?

E.g. it seems like this also compiles where the object is a completely
different type

--------
struct A {
void f() { 
}
};

struct C {};
typedef void (*c_fptr)(C *);

int main() {
C c;
c_fptr fp = (c_fptr)(&A::f);
fp(&c);
}
--------

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

* [Bug c++/111531] Bound member function with multiple inheritance documentation should be clearer
  2023-09-22  0:30 [Bug c++/111531] New: Bound member function (-Wno-pmf-conversions) with multiple inheritance paulhaile3 at gmail dot com
                   ` (2 preceding siblings ...)
  2023-09-22 12:16 ` paulhaile3 at gmail dot com
@ 2023-09-22 15:47 ` paulhaile3 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: paulhaile3 at gmail dot com @ 2023-09-22 15:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Paul Haile <paulhaile3 at gmail dot com> ---
The only time I could imagine allowing type mismatch would be in allowing the
function pointer to allow void * in type erased contexts.

e.g.
----
typedef void (*b_fptr)(void *);
----

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