[PATCH] fix KJS crash on some more strict platforms
Helge Deller
deller at gmx.de
Tue Dec 16 21:08:26 GMT 2003
Some platforms are pretty strict about the alignment for some types
of variables.
Code like this (taken from kdelibs/kjs/internal.cpp):
const unsigned char NaN_Bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
....
double x = * (double*) NaN_Bytes;
e.g. gives the compiler warning message:
/home/cvs/kde20/kdelibs/kjs/internal.cpp:75: warning: cast from `const unsigned
char*' to `const double*' increases required alignment of target type
and in this case KJS breaks with a SIGSEG fault if NaN_Bytes[] wasn't aligned by the
compiler to a 8 byte boundary. I my case the compiler (gcc on the HP PARISC platform)
aligned the NaN_Bytes arry by default to 4 bytes (which is normally OK) but
which of course by pure luck sometimes worked and sometimes just crashed.
The attached patch fixes this bug by using gcc's __aligned__ attribute if the sources
are built with g++. I assume other architectures (e.g. SPARC?) with other
compilers (e.g. aCC on HP/UX?) might want a similiar patch too, just to be on
the safe side. Adding a warning to the #else part in this case would IMHO be
a good idea (Opinions?).
I would like to fix this bug now, so I would like to ask if it's Ok to submit this
version of the patch to HEAD and backport it to KDE 3.1 ?
Helge
-------------- next part --------------
Index: internal.cpp
===================================================================
RCS file: /home/kde/kdelibs/kjs/internal.cpp,v
retrieving revision 1.170
diff -u -p -r1.170 internal.cpp
--- internal.cpp 17 Nov 2003 00:48:40 -0000 1.170
+++ internal.cpp 16 Dec 2003 20:54:05 -0000
@@ -54,16 +54,22 @@ extern int kjsyyparse();
using namespace KJS;
+#if defined(__GNUC__)
+#define KJS_DOUBLE_ALIGN __attribute__((__aligned__(sizeof(double))))
+#else
+#define KJS_DOUBLE_ALIGN
+#endif
+
namespace KJS {
#ifdef WORDS_BIGENDIAN
- const unsigned char NaN_Bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
- const unsigned char Inf_Bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 };
+ const unsigned char KJS_DOUBLE_ALIGN NaN_Bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
+ const unsigned char KJS_DOUBLE_ALIGN Inf_Bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } ;
#elif defined(arm)
- const unsigned char NaN_Bytes[] = { 0, 0, 0xf8, 0x7f, 0, 0, 0, 0 };
- const unsigned char Inf_Bytes[] = { 0, 0, 0xf0, 0x7f, 0, 0, 0, 0 };
+ const unsigned char KJS_DOUBLE_ALIGN NaN_Bytes[] = { 0, 0, 0xf8, 0x7f, 0, 0, 0, 0 } ;
+ const unsigned char KJS_DOUBLE_ALIGN Inf_Bytes[] = { 0, 0, 0xf0, 0x7f, 0, 0, 0, 0 } ;
#else
- const unsigned char NaN_Bytes[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f };
- const unsigned char Inf_Bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
+ const unsigned char KJS_DOUBLE_ALIGN NaN_Bytes[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f } ;
+ const unsigned char KJS_DOUBLE_ALIGN Inf_Bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } ;
#endif
const double NaN = *(const double*) NaN_Bytes;
More information about the kde-core-devel
mailing list