From 2bbc328212ee779b96aa3cdf96f2a054a1226214 Mon Sep 17 00:00:00 2001 From: Christian Lavoie Date: Tue, 8 Feb 2011 23:05:47 +0000 Subject: [PATCH] Patch from Chris Morgan, fixing gcc warnings. THRIFT-1057: casts in TBinaryProtocol.tcc causing "dereferencing type-punned pointer will break strict-aliasing rules" warnings from gcc git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1068672 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/protocol/TBinaryProtocol.tcc | 43 ++++++++++++++---------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/cpp/src/protocol/TBinaryProtocol.tcc b/lib/cpp/src/protocol/TBinaryProtocol.tcc index 3d9334838..721e897bd 100644 --- a/lib/cpp/src/protocol/TBinaryProtocol.tcc +++ b/lib/cpp/src/protocol/TBinaryProtocol.tcc @@ -354,28 +354,34 @@ uint32_t TBinaryProtocolT::readByte(int8_t& byte) { template uint32_t TBinaryProtocolT::readI16(int16_t& i16) { - uint8_t b[2]; - this->trans_->readAll(b, 2); - i16 = *(int16_t*)b; - i16 = (int16_t)ntohs(i16); + union bytes { + uint8_t b[2]; + int16_t all; + } theBytes; + this->trans_->readAll(theBytes.b, 2); + i16 = (int16_t)ntohs(theBytes.all); return 2; } template uint32_t TBinaryProtocolT::readI32(int32_t& i32) { - uint8_t b[4]; - this->trans_->readAll(b, 4); - i32 = *(int32_t*)b; - i32 = (int32_t)ntohl(i32); + union bytes { + uint8_t b[4]; + int32_t all; + } theBytes; + this->trans_->readAll(theBytes.b, 4); + i32 = (int32_t)ntohl(theBytes.all); return 4; } template uint32_t TBinaryProtocolT::readI64(int64_t& i64) { - uint8_t b[8]; - this->trans_->readAll(b, 8); - i64 = *(int64_t*)b; - i64 = (int64_t)ntohll(i64); + union bytes { + uint8_t b[8]; + int64_t all; + } theBytes; + this->trans_->readAll(theBytes.b, 8); + i64 = (int64_t)ntohll(theBytes.all); return 8; } @@ -384,12 +390,13 @@ uint32_t TBinaryProtocolT::readDouble(double& dub) { BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); BOOST_STATIC_ASSERT(std::numeric_limits::is_iec559); - uint64_t bits; - uint8_t b[8]; - this->trans_->readAll(b, 8); - bits = *(uint64_t*)b; - bits = ntohll(bits); - dub = bitwise_cast(bits); + union bytes { + uint8_t b[8]; + uint64_t all; + } theBytes; + this->trans_->readAll(theBytes.b, 8); + theBytes.all = ntohll(theBytes.all); + dub = bitwise_cast(theBytes.all); return 8; }