Ver Fonte

2009-05-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Fixed g++-4.4 compiler warning: dereferencing type-punned pointer
	will break strict-aliasing rules
	* src/PeerConnection.cc
	* test/DHTRoutingTableSerializerTest.cc
Tatsuhiro Tsujikawa há 16 anos atrás
pai
commit
a933438401
3 ficheiros alterados com 18 adições e 3 exclusões
  1. 7 0
      ChangeLog
  2. 3 1
      src/PeerConnection.cc
  3. 8 2
      test/DHTRoutingTableSerializerTest.cc

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+2009-05-24  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Fixed g++-4.4 compiler warning: dereferencing type-punned pointer
+	will break strict-aliasing rules
+	* src/PeerConnection.cc
+	* test/DHTRoutingTableSerializerTest.cc
+
 2009-05-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Removed PKG_CONFIG variable in sqlite3.m4, which conflicts the

+ 3 - 1
src/PeerConnection.cc

@@ -97,7 +97,9 @@ bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength) {
       // still 4-lenbufLength bytes to go
       return false;
     }
-    uint32_t payloadLength = ntohl(*(reinterpret_cast<uint32_t*>(lenbuf)));
+    uint32_t payloadLength;
+    memcpy(&payloadLength, lenbuf, sizeof(payloadLength));
+    payloadLength = ntohl(payloadLength);
     if(payloadLength > MAX_PAYLOAD_LEN) {
       throw DL_ABORT_EX(StringFormat(EX_TOO_LONG_PAYLOAD, payloadLength).str());
     }

+ 8 - 2
test/DHTRoutingTableSerializerTest.cc

@@ -70,7 +70,10 @@ void DHTRoutingTableSerializerTest::testSerialize()
 
   // time
   ss.read(buf, 8);
-  time_t time = ntoh64(*reinterpret_cast<uint64_t*>(buf));
+  time_t time;
+  uint64_t timebuf;
+  memcpy(&timebuf, buf, sizeof(timebuf));
+  time = ntoh64(timebuf);
   std::cerr << time << std::endl;
 
   // localnode
@@ -86,7 +89,10 @@ void DHTRoutingTableSerializerTest::testSerialize()
 
   // number of nodes saved
   ss.read(buf, 4);
-  uint32_t numNodes = ntohl(*reinterpret_cast<uint32_t*>(buf));
+  uint32_t numNodes;
+  memcpy(&numNodes, buf, sizeof(numNodes));
+  numNodes = ntohl(numNodes);
+
   CPPUNIT_ASSERT_EQUAL((uint32_t)3, numNodes);
   // 4bytes reserved
   ss.read(buf, 4);