From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24769 invoked by alias); 16 Oct 2002 16:06:06 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 24705 invoked by uid 71); 16 Oct 2002 16:06:04 -0000 Resent-Date: 16 Oct 2002 16:06:04 -0000 Resent-Message-ID: <20021016160604.24704.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, David Decotigny Received: (qmail 19962 invoked from network); 16 Oct 2002 15:56:34 -0000 Received: from unknown (HELO sky.irisa.fr) (131.254.60.147) by sources.redhat.com with SMTP; 16 Oct 2002 15:56:34 -0000 Received: from blutch.irisa.fr (blutch.irisa.fr [131.254.13.41]) by sky.irisa.fr (8.11.4/8.11.4) with ESMTP id g9GFuOr06627; Wed, 16 Oct 2002 17:56:24 +0200 (MET DST) Message-Id: <200210161556.RAA28651@blutch.irisa.fr> Date: Wed, 16 Oct 2002 09:06:00 -0000 From: David Decotigny To: gcc-gnats@gcc.gnu.org Cc: ddecotig@irisa.fr X-Send-Pr-Version: 3.113 Subject: c++/8242: Invalid class size evaluation with long long and inheritance X-SW-Source: 2002-10/txt/msg00612.txt.bz2 List-Id: >Number: 8242 >Category: c++ >Synopsis: Invalid struct size evaluation with long long and inheritance (g++-3.x) >Confidential: no >Severity: critical >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Wed Oct 16 09:06:03 PDT 2002 >Closed-Date: >Last-Modified: >Originator: David.Decotigny@irisa.fr >Release: 3.1 >Organization: IRISA, Campus de Beaulieu, 35042 Rennes Cedex, FRANCE >Environment: System: SunOS blutch 5.7 Generic_106541-19 sun4u sparc SUNW,Ultra-5_10 Architecture: sun4 host: sparc-sun-solaris2.7 build: sparc-sun-solaris2.7 target: sparc-sun-solaris2.7 configured with: ../gcc/gcc-3.1/configure --prefix /usr/local/gcc-3.1 --enable-shared --enable-languages=c++,f77 >Description: When you have a class Ancestor (struct in the example below, but it doesn't matter) that contains at least a 64 bits attribute (even in a much larger union, or with any __attribute__ ((aligned(xxx))) ), then a class that inherits it and defines additional fields will have its sizeof() wrong as soon as the size of the data /after/ the long long is not multiple of 64 bits... >How-To-Repeat: Compile the following source (any option) with g++-3.0, g++-3.0.1, g++-3.0.3, g++-3.0.4, g++-3.1 (maybe others) on a Solaris 2.7 host (g++ configured *without* the --enable-long-long option), and run it. You will notice that Child and Ancestor have the same sizeof(), and hence the memset in zero_ancestor(), which normally does not touch the Child::foo field, will overwrite it : =====> With g++-3.1 (Wrong) : [blutch] ~ >make clean all && ./a rm -f *.o g++-3.1 -Wall -c cpp_main.cc -o cpp_main.o g++-3.1 -Wall -o a cpp_main.o sizeof(struct Ancestor) = 16 sizeof(struct Child) = 16 #1 c.foo = 42 #2 c.foo = 0 =====> With g++-2.95.3 (Ok) : [blutch] ~ >make clean all && ./a 1060 rm -f *.o g++-2.95.3 -Wall -c cpp_main.cc -o cpp_main.o g++-2.95.3 -Wall -o a cpp_main.o sizeof(struct Ancestor) = 16 sizeof(struct Child) = 24 #1 c.foo = 42 #2 c.foo = 42 Here is the source: #include // only for memset #include // only for std::cout struct Ancestor { // Or class if you prefer long long i64; // Or any array of 64bits integers, or even an union // with at least a long long member !!! int not64; // Or char, short..., or whatever non multiple // of 64 bits (int not64[3] will fail for example...) }; struct Child : public Ancestor { // Or class if you prefer int foo; // or anything else }; void zero_ancestor(struct Ancestor * a) { // Both will fail... // *((int*)(((int)a)+sizeof(struct Ancestor)-sizeof(int))) = 0; memset(a, 0x0, sizeof(struct Ancestor)); } int main() { Child c; std::cout << "sizeof(struct Ancestor) = " << sizeof(struct Ancestor) << std::endl; std::cout << "sizeof(struct Child) = " << sizeof(struct Child) << std::endl; c.foo = 42; std::cout << "#1 c.foo = " << c.foo << std::endl; zero_ancestor(& c); std::cout << "#2 c.foo = " << c.foo << std::endl; return 0; } >Fix: Non-gcc fix : make sure that what is /after/ the long long fields in Ancestor are of size multiple of 64bits... This is ugly and painful to check. >Release-Note: >Audit-Trail: >Unformatted: