Bug 28907 - Wrong pointer cast with multiple inheritance
Summary: Wrong pointer cast with multiple inheritance
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 13.1
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-02-18 16:49 UTC by Simon Marchi
Modified: 2022-04-18 15:46 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Marchi 2022-02-18 16:49:44 UTC
$ cat test.cpp                                                       
struct Base1 {
  int b1 = 0x1111;
};

struct Base2 {
  int b2 = 0x2222;
};

struct Derived : public Base1, Base2 {
  int d = 0x3333;
};

int main() {
  Derived d;
  return 0;
}
$ g++ test.cpp -g3 -O0                                               
$ DEBUGINFOD_URLS= ./gdb -nx -q --data-directory=data-directory a.out -ex "b 15" -ex r
Reading symbols from a.out...
Breakpoint 1 at 0x1165: file test.cpp, line 15.
Starting program: /home/simark/build/binutils-gdb/gdb/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/../lib/libthread_db.so.1".

Breakpoint 1, main () at test.cpp:15
15        return 0;
(gdb) p &d
$1 = (Derived *) 0x7fffffffdcfc
(gdb) p (Base2 *) &d
$2 = (Base2 *) 0x7fffffffdd00
(gdb) p (Derived *) (Base2 *) &d
$3 = (Derived *) 0x7fffffffdcf8


Pretty sure that $1 and $3 should be equal.

Might be related to https://sourceware.org/bugzilla/show_bug.cgi?id=20285.  Not quite the same, but it might be the same root cause.
Comment 1 Tom Tromey 2022-04-02 16:14:40 UTC
Sending a patch.
Comment 2 Sourceware Commits 2022-04-18 15:45:55 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2390419d1cb72882110538e01e5586372df19657

commit 2390419d1cb72882110538e01e5586372df19657
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Apr 2 09:54:40 2022 -0600

    Fix C++ cast of derived class to base class
    
    PR c++/28907 points out that casting from a derived class to a base
    class fails in some situations.  The problem turned out to be a
    missing use of value_embedded_offset.  One peculiarity here is that,
    if you managed to construct a pointer-to-derived with an embedded
    offset of 0, the cast would work -- for example, one of the two new
    tests here passes without the patch.
    
    This embedded offset stuff is an endless source of bugs.  I wonder if
    it's possible to get rid of it somehow.
    
    Regression tested on x86-64 Fedora 34.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28907
Comment 3 Tom Tromey 2022-04-18 15:46:48 UTC
Fixed.