[PATCH] fix KJS crash on some more strict platforms

Helge Deller deller at gmx.de
Tue Dec 16 23:41:34 GMT 2003


On Wednesday 17 December 2003 00:30, Maks Orlovich wrote:
> >
> > The compiler might align char[] and double in _this_ example differently,
> > e.g. char[] at a 2 byte boundary and double at 8 bytes.
> > Look at the manpage of gcc (e.g.
> > http://scv.bu.edu/SCV/Archive/linux-cluster/manpages/gcc.html and search
> > for the example in the description for "-fstrict-aliasing"): "In
> > particular, an object of one type is assumed never to reside at the same
> > address as an object of a different type, unless the types are almost the
> > same. For example, an "unsigned int" can alias an "int", but not a "void*"
> > or a "double""
> >
> > char[] and double are not "almost the same".
> 
> Ahem. Read a bit further. "A character type may alias any other type. "
> 
> And a bit further than that:
> Pay special attention to code like this: 
> 
> 
>         union a_union {
>           int i;
>           double d;
>         };
> 
>         int f() {
>           a_union t;
>           t.d = 3.0;
>           return t.i;
>         }
> 
> 
> 
>  The practice of reading from a different union member than the one most 
> recently written to (called ``type-punning'') is common. Even with 
> -fstrict-aliasing, type-punning is allowed, provided the memory is accessed 
> through the union type.
 

Ahem. Read a bit further.  :-) 

So, the code above will work as expected. However, this code might not: 
        int f() {
          a_union t;
          int* ip;
          t.d = 3.0;
          ip = &t.i;
          return *ip;
        }

And this is exactly what my proposed patch is about. We try to read a double
from the pointer which is &char[8] and this is not guaranteed to work:
const double NaN = *(const double*) NaN_Bytes;




More information about the kde-core-devel mailing list