This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/13387] [3.3/3.4 regression] assignment to base class corrupts subclass


------- Additional Comments From bangerth at dealii dot org  2003-12-12 09:04 -------
Confirmed. The original testcase had a dubious address-of-cast construct 
(in the printf statement) but here is something cleaner: 
------------------------- 
struct Base { 
    Base() : i(0), c(0) {} 
    int i; 
    char c; 
}; 
 
struct Sub : Base { 
    Sub () : d(0) {} 
    char d; 
}; 
 
int main() { 
  Sub sub; 
  *static_cast<Base*>(&sub) = Base(); 
  return sub.d; 
} 
------------------------- 
The result should of course be zero, but I get 
 
g/x> /home/bangerth/bin/gcc-3.3.3-pre/bin/c++ x.cc 
g/x> ./a.out ; echo $? 
132 
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ x.cc 
g/x> ./a.out ; echo $? 
255 
 
Since 2.95.3 gives a result of zero, this is a regression. 
 
I believe that this has something to do with alignments etc. I don't 
know the rules, but if Sub::d is packed into the tail padding of Base, 
then what seems to happen is that the copy constructor of Base copies 
the elements of the base and does something random with the padding 
bytes. If I change the type of Base::i to integer to force the element 
of Sub to a 4-byte boundary, then the problem goes away. 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
           Keywords|                            |wrong-code
   Last reconfirmed|0000-00-00 00:00:00         |2003-12-12 09:04:51
               date|                            |
            Summary|assignment to base class    |[3.3/3.4 regression]
                   |corrupts subclass           |assignment to base class
                   |                            |corrupts subclass
   Target Milestone|---                         |3.3.3


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13387


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]