[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#458133: some patches



I've looked a bit at qt's source code and it is impressive how people
can write code C++ that would already be ugly and hackish in C.
(and that for almost every kind of ugly and hackish code in C I can
think of and I only looked at things near reinterpet_cast).

I'm currently building qt4-x11 on my hppa (which is extremly slow), but
the qmake calls so far were not erroring out (kernel switched to
traping) yet with the following patch (It's nor really nice nor
portable, but as its purpose is writing 8 byte strings into a double
and its for qt, which does the assumption that this tricks works
everywhere else, that does not matter):

diff -r -u qt4-x11-4.3.4/src/corelib/global/qnumeric_p.h qt4-x11-4.3.4.patched/src/corelib/global/qnumeric_p.h
--- qt4-x11-4.3.4/src/corelib/global/qnumeric_p.h	2008-02-26 18:27:52.000000000 +0100
+++ qt4-x11-4.3.4.patched/src/corelib/global/qnumeric_p.h	2008-03-11 15:38:36.000000000 +0100
@@ -62,11 +62,17 @@
 static const unsigned char qt_armfpa_inf_bytes[] = { 0, 0, 0xf0, 0x7f, 0, 0, 0, 0 };
 static inline double qt_inf()
 {
+    union { double d; unsigned char bytes[8]; } val;
+
 #ifdef QT_ARMFPA
-    return *reinterpret_cast<const double *>(qt_armfpa_inf_bytes);
+    qMemCopy(val.bytes, qt_armfpa_inf_bytes, 8);
 #else
-    return *reinterpret_cast<const double *>(QSysInfo::ByteOrder == QSysInfo::BigEndian ? qt_be_inf_bytes : qt_le_inf_bytes);
+    if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
+        qMemCopy(val.bytes, qt_be_inf_bytes, 8);
+      else
+        qMemCopy(val.bytes, qt_le_inf_bytes, 8);
 #endif
+    return val.d;
 }
 
 // Signaling NAN
@@ -75,11 +81,17 @@
 static const unsigned char qt_armfpa_snan_bytes[] = { 0, 0, 0xf8, 0x7f, 0, 0, 0, 0 };
 static inline double qt_snan()
 {
+    union { double d; unsigned char bytes[8]; } val;
+
 #ifdef QT_ARMFPA
-    return *reinterpret_cast<const double *>(qt_armfpa_snan_bytes);
+    qMemCopy(val.bytes, qt_armfpa_snan_bytes, 8);
 #else
-    return *reinterpret_cast<const double *>(QSysInfo::ByteOrder == QSysInfo::BigEndian ? qt_be_snan_bytes : qt_le_snan_bytes);
+    if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
+        qMemCopy(val.bytes, qt_be_snan_bytes, 8);
+      else
+        qMemCopy(val.bytes, qt_le_snan_bytes, 8);
 #endif
+    return val.d;
 }
 
 // Quiet NAN
@@ -88,11 +100,17 @@
 static const unsigned char qt_armfpa_qnan_bytes[] = { 0, 0, 0xf8, 0xff, 0, 0, 0, 0 };
 static inline double qt_qnan()
 {
+    union { double d; unsigned char bytes[8]; } val;
+
 #ifdef QT_ARMFPA
-    return *reinterpret_cast<const double *>(qt_armfpa_qnan_bytes);
+    qMemCopy(val.bytes, qt_armfpa_qnan_bytes, 8);
 #else
-    return *reinterpret_cast<const double *>(QSysInfo::ByteOrder == QSysInfo::BigEndian ? qt_be_qnan_bytes : qt_le_qnan_bytes);
+    if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
+        qMemCopy(val.bytes, qt_be_qnan_bytes, 8);
+      else
+        qMemCopy(val.bytes, qt_le_qnan_bytes, 8);
 #endif
+    return val.d;
 }
 


The following cannot cause a problem in this form, but I had to write a
fix for it anyway, because it is alphabetically the first reinterpret_cast
in src/ and is so broken, that even the possibility of someone copying it
elsewhere is reason enough to fix it:

 static inline bool qt_is_inf(double d)
diff -r -u qt4-x11-4.3.4/src/3rdparty/sha1/sha1.cpp qt4-x11-4.3.4.patched/src/3rdparty/sha1/sha1.cpp
--- qt4-x11-4.3.4/src/3rdparty/sha1/sha1.cpp	2008-02-26 18:27:50.000000000 +0100
+++ qt4-x11-4.3.4.patched/src/3rdparty/sha1/sha1.cpp	2008-03-11 16:02:53.000000000 +0100
@@ -149,10 +149,10 @@
     quint32 d = state->h3;
     quint32 e = state->h4;
 
-    quint8 chunkBuffer[64];
-    memcpy(chunkBuffer, buffer, 64);
+    Sha1Chunk chunkBuffer;
+    memcpy(chunkBuffer.bytes, buffer, 64);
 
-    Sha1Chunk *chunk = reinterpret_cast<Sha1Chunk*>(&chunkBuffer);
+    Sha1Chunk *chunk = &chunkBuffer;
 
     for (int i = 0; i < 16; ++i)
         chunk->words[i] = qFromBigEndian(chunk->words[i]);
@@ -188,7 +188,7 @@
     // Wipe variables
 #ifdef SHA1_WIPE_VARIABLES
     a = b = c = d = e = 0;
-    memset(chunkBuffer, 0, 64);
+    memset(chunkBuffer.bytes, 0, 64);
 #endif
 }


Also note that here someone tried hard to write unaligned code but
instead ended up writing code that is properly aligned but does not
work (ev is of type struct inotify_event*):

--- qt4-x11-4.3.4/src/corelib/io/qfilesystemwatcher_inotify.cpp
@@ -315,6 +315,7 @@
             id = -id;
             path = idToPath.value(id);
             if (path.isEmpty()) {
+		// Oops?
                 ev += sizeof(inotify_event) + ev->len;
                 continue;
             }
@@ -338,6 +339,7 @@
                 emit fileChanged(path, false);
         }
 
+	// Oops?
         ev += sizeof(inotify_event) + ev->len;
     }
 }

Hochachtungsvoll,
	Bernhard R. Link



Reply to: