public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: David Decotigny <David.Decotigny@irisa.fr> To: gcc-gnats@gcc.gnu.org Cc: ddecotig@irisa.fr Subject: c++/8242: Invalid class size evaluation with long long and inheritance Date: Wed, 16 Oct 2002 09:06:00 -0000 [thread overview] Message-ID: <200210161556.RAA28651@blutch.irisa.fr> (raw) >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 <string.h> // only for memset #include <iostream> // 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:
reply other threads:[~2002-10-16 16:06 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=200210161556.RAA28651@blutch.irisa.fr \ --to=david.decotigny@irisa.fr \ --cc=ddecotig@irisa.fr \ --cc=gcc-gnats@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).