]> git.mxchange.org Git - flightgear.git/commitdiff
Fixed all type-casts violating the strict-aliasing rule.
authorThorsten Brehm <brehm@patagonia.southamerica>
Mon, 27 Sep 2010 21:48:20 +0000 (23:48 +0200)
committerThorsten Brehm <brehm@patagonia.southamerica>
Mon, 27 Sep 2010 21:48:20 +0000 (23:48 +0200)
=> Removes compiler warnings and optimization problems.

src/MultiPlayer/multiplaymgr.cxx
src/Network/generic.cxx

index 7574332b26d34064fb8932d778d6dcaf8ac13037..508d9631790a5c88f5a3a56e85908e55f2cb168f 100644 (file)
@@ -462,38 +462,38 @@ union FGMultiplayMgr::MsgBuf
 {
     MsgBuf()
     {
-        memset(&Msg, 0, sizeof(Msg));
+        memset(&Msg.Raw, 0, sizeof(Msg));
     }
 
     T_MsgHdr* msgHdr()
     {
-        return reinterpret_cast<T_MsgHdr*>(Msg);
+        return &Msg.Header;
     }
 
     const T_MsgHdr* msgHdr() const
     {
-        return reinterpret_cast<const T_MsgHdr*>(Msg);
+        return reinterpret_cast<const T_MsgHdr*>(&Msg.Header);
     }
 
     T_PositionMsg* posMsg()
     {
-        return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
+        return reinterpret_cast<T_PositionMsg*>(Msg.Raw + sizeof(T_MsgHdr));
     }
 
     const T_PositionMsg* posMsg() const
     {
-        return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
+        return reinterpret_cast<const T_PositionMsg*>(Msg.Raw + sizeof(T_MsgHdr));
     }
 
     xdr_data_t* properties()
     {
-        return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)
+        return reinterpret_cast<xdr_data_t*>(Msg.Raw + sizeof(T_MsgHdr)
                                              + sizeof(T_PositionMsg));
     }
 
     const xdr_data_t* properties() const
     {
-        return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)
+        return reinterpret_cast<const xdr_data_t*>(Msg.Raw + sizeof(T_MsgHdr)
                                                    + sizeof(T_PositionMsg));
     }
     /**
@@ -501,12 +501,12 @@ union FGMultiplayMgr::MsgBuf
      */
     xdr_data_t* propsEnd()
     {
-        return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);
+        return reinterpret_cast<xdr_data_t*>(Msg.Raw + MAX_PACKET_SIZE);
     };
 
     const xdr_data_t* propsEnd() const
     {
-        return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);
+        return reinterpret_cast<const xdr_data_t*>(Msg.Raw + MAX_PACKET_SIZE);
     };
     /**
      * The end of properties actually in the buffer. This assumes that
@@ -514,16 +514,20 @@ union FGMultiplayMgr::MsgBuf
      */
     xdr_data_t* propsRecvdEnd()
     {
-        return reinterpret_cast<xdr_data_t*>(Msg + msgHdr()->MsgLen);
+        return reinterpret_cast<xdr_data_t*>(Msg.Raw + Msg.Header.MsgLen);
     }
 
     const xdr_data_t* propsRecvdEnd() const
     {
-        return reinterpret_cast<const xdr_data_t*>(Msg + msgHdr()->MsgLen);
+        return reinterpret_cast<const xdr_data_t*>(Msg.Raw + Msg.Header.MsgLen);
     }
     
     xdr_data2_t double_val;
-    char Msg[MAX_PACKET_SIZE];
+    union
+    {
+      char Raw[MAX_PACKET_SIZE];
+      T_MsgHdr Header;
+    } Msg;
 };
 
 void
@@ -654,9 +658,9 @@ FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
     ++it;
   }
 escape:
-  unsigned msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg;
+  unsigned msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg.Raw;
   FillMsgHdr(msgBuf.msgHdr(), POS_DATA_ID, msgLen);
-  mSocket->sendto(msgBuf.Msg, msgLen, 0, &mServer);
+  mSocket->sendto(msgBuf.Msg.Raw, msgLen, 0, &mServer);
   SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
 } // FGMultiplayMgr::SendMyPosition()
 
@@ -731,7 +735,7 @@ FGMultiplayMgr::Update(void)
     //  packet waiting to be processed.
     //////////////////////////////////////////////////
     netAddress SenderAddress;
-    bytes = mSocket->recvfrom(msgBuf.Msg, sizeof(msgBuf.Msg), 0,
+    bytes = mSocket->recvfrom(msgBuf.Msg.Raw, sizeof(msgBuf.Msg.Raw), 0,
                               &SenderAddress);
     //////////////////////////////////////////////////
     //  no Data received
@@ -975,7 +979,7 @@ FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,
   
   char *chatStr = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
   const T_ChatMsg* ChatMsg
-      = reinterpret_cast<const T_ChatMsg *>(Msg.Msg + sizeof(T_MsgHdr));
+      = reinterpret_cast<const T_ChatMsg *>(Msg.Msg.Raw + sizeof(T_MsgHdr));
   strncpy(chatStr, ChatMsg->Text,
           MsgHdr->MsgLen - sizeof(T_MsgHdr));
   chatStr[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
index 423d2be189b28ffec2de641415dc99ab3b1bdcfb..9ab7d76999e13597ebf7336a16fe9a0bfd3b9504 100644 (file)
@@ -95,20 +95,20 @@ bool FGGeneric::gen_message_binary() {
 
         switch (_out_message[i].type) {
         case FG_INT:
+        {
             val = _out_message[i].offset +
                   _out_message[i].prop->getIntValue() * _out_message[i].factor;
-
-            if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
-                *((int32_t*)&buf[length]) = (int32_t)val;
-            } else {
-                *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)val);
+            int32_t intVal = val;
+            if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
+                intVal = (int32_t) sg_bswap_32((uint32_t)intVal);
             }
+            memcpy(&buf[length], &intVal, sizeof(int32_t));
             length += sizeof(int32_t);
             break;
+        }
 
         case FG_BOOL:
-            *((int8_t*)&buf[length])
-                      = _out_message[i].prop->getBoolValue() ? true : false;
+            buf[length] = (char) (_out_message[i].prop->getBoolValue() ? true : false);
             length += 1;
             break;
 
@@ -117,46 +117,48 @@ bool FGGeneric::gen_message_binary() {
             val = _out_message[i].offset +
                  _out_message[i].prop->getFloatValue() * _out_message[i].factor;
 
-            int fixed = (int)(val * 65536.0f); 
-            if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
-                *((int32_t*)&buf[length]) = (int32_t)fixed;
-            } else {
-                *((uint32_t*)&buf[length]) = sg_bswap_32((uint32_t)fixed);
+            int32_t fixed = (int)(val * 65536.0f);
+            if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
+                fixed = (int32_t) sg_bswap_32((uint32_t)fixed);
             } 
+            memcpy(&buf[length], &fixed, sizeof(int32_t));
             length += sizeof(int32_t);
             break;
         }
+
         case FG_FLOAT:
+        {
             val = _out_message[i].offset +
                  _out_message[i].prop->getFloatValue() * _out_message[i].factor;
+            u32 tmpun32;
+            tmpun32.floatVal = static_cast<float>(val);
 
-            if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
-                *((float*)&buf[length]) = val;
-            } else {
-                u32 tmpun32;
-                tmpun32.floatVal = static_cast<float>(val);
-                *((uint32_t*)&buf[length]) = sg_bswap_32(tmpun32.intVal);
+            if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
+                tmpun32.intVal = sg_bswap_32(tmpun32.intVal);
             }
+            memcpy(&buf[length], &tmpun32.intVal, sizeof(uint32_t));
             length += sizeof(uint32_t);
             break;
+        }
 
         case FG_DOUBLE:
+        {
             val = _out_message[i].offset +
                  _out_message[i].prop->getFloatValue() * _out_message[i].factor;
+            u64 tmpun64;
+            tmpun64.doubleVal = val;
 
-            if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
-                *((double*)&buf[length]) = val;
-            } else {
-                u64 tmpun64;
-                tmpun64.doubleVal = val;
-                *((uint64_t*)&buf[length]) = sg_bswap_64(tmpun64.longVal);
+            if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
+                tmpun64.longVal = sg_bswap_64(tmpun64.longVal);
             }
-            length += sizeof(int64_t);
+            memcpy(&buf[length], &tmpun64.longVal, sizeof(uint64_t));
+            length += sizeof(uint64_t);
             break;
+        }
 
         default: // SG_STRING
             const char *strdata = _out_message[i].prop->getStringValue();
-            int strlength = strlen(strdata);
+            int32_t strlength = strlen(strdata);
 
             if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) {
                 SG_LOG( SG_IO, SG_ALERT, "Generic protocol: "
@@ -165,11 +167,10 @@ bool FGGeneric::gen_message_binary() {
             /* Format for strings is 
              * [length as int, 4 bytes][ASCII data, length bytes]
              */
-            if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
-                *((int32_t*)&buf[length]) = strlength;
-            } else {
-                *((int32_t*)&buf[length]) = sg_bswap_32(strlength);
+            if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
+                strlength = sg_bswap_32(strlength);
             }
+            memcpy(&buf[length], &strlength, sizeof(int32_t));
             length += sizeof(int32_t);
             strncpy(&buf[length], strdata, strlength);
             length += strlength; 
@@ -191,11 +192,11 @@ bool FGGeneric::gen_message_binary() {
     }
 
     if (binary_footer_type != FOOTER_NONE) {
-        if (binary_byte_order == BYTE_ORDER_MATCHES_NETWORK_ORDER) {
-            *((int32_t*)&buf[length]) = binary_footer_value;
-        } else {
-            *((int32_t*)&buf[length]) = sg_bswap_32(binary_footer_value);
+        int32_t intValue = binary_footer_value;
+        if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) {
+            intValue = sg_bswap_32(binary_footer_value);
         }
+        memcpy(&buf[length], &intValue, sizeof(int32_t));
         length += sizeof(int32_t);
     }