[PATCH] fix KJS crash on some more strict platforms

Helge Deller deller at gmx.de
Thu Dec 18 21:20:52 GMT 2003


On Thursday 18 December 2003 21:43, Friedrich W. H.  Kossebau wrote:
> So something like this (in khexedit) is to be doomed, too?
> 
>     QString buf;
>     void *P8Bit, *P16Bit, *P32Bit, *P64Bit;
>     unsigned char Data[8];
>     if(
> #ifdef WORDS_BIGENDIAN
>         !
> #endif
>          mCheckIntelFormat->isChecked() )
>     {
>       // take it as it is
>       memcpy( Data, state.data, 8 );
>       P8Bit = P16Bit = P32Bit = P64Bit = Data;
>     }
>     else
>     {
>       // reverse order
>       for( int i=0,j=7; i<8; ++i,--j )
>         Data[i] = state.data[j];
> 
>       P8Bit =  &Data[7];
>       P16Bit = &Data[6];
>       P32Bit = &Data[4];
>       P64Bit = Data;

This code is at least not very portable.
At the first glance it should work, _as_ _long_ as you ensure 
that "Data" is align on a 8 byte (because of P64Bit) boundary. 
Right now it's aligned anywhere, where the compiler puts it.

Something like:
typedef union { unsigned char b[8]; double d; } aligned_t;
aligned_t Data;
...
       P8Bit =  &Data.b[7];
       P16Bit = &Data.b[6];
       P32Bit = &Data.b[4];
       P64Bit = Data.b[0];

should be safer.

FYI: I tested khexedit right now on my PA (bigendian) machine and it 
worked correctly. 
But maybe it was just pure luck that the compiler aligned it correctly :-)

Helge




More information about the kde-core-devel mailing list