public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/8242: Invalid class size evaluation with long long and inheritance
@ 2002-10-16  9:06 David Decotigny
  0 siblings, 0 replies; only message in thread
From: David Decotigny @ 2002-10-16  9:06 UTC (permalink / raw)
  To: gcc-gnats; +Cc: ddecotig


>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:


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-10-16 16:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-16  9:06 c++/8242: Invalid class size evaluation with long long and inheritance David Decotigny

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