From e78bb0c916736f95b6a9c6a340faf6d32229da03 Mon Sep 17 00:00:00 2001 From: kervala Date: Tue, 29 Nov 2016 20:32:33 +0100 Subject: [PATCH] Fixed: Strict aliasing warnings --- .../interface_expr_user_fct_game.cpp | 13 ++++-- .../ryzom/common/src/game_share/inventories.h | 11 +++-- .../src/game_share/persistent_data_inline.h | 40 +++++++++++-------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/code/ryzom/client/src/interface_v3/interface_expr_user_fct_game.cpp b/code/ryzom/client/src/interface_v3/interface_expr_user_fct_game.cpp index 5fe4cc569..ac75afc82 100644 --- a/code/ryzom/client/src/interface_v3/interface_expr_user_fct_game.cpp +++ b/code/ryzom/client/src/interface_v3/interface_expr_user_fct_game.cpp @@ -146,12 +146,19 @@ static DECLARE_INTERFACE_USER_FCT(getCompassText) return false; } + // helper + union C64BitsParts + { + sint64 i64; + double d; + }; + //get the direction // sint64 in the databae. - sint64 angleInt= args[0].getInteger(); + C64BitsParts angle; + angle.i64 = args[0].getInteger(); // cast as double now. - double angle= (double&)angleInt; - sint direction =(sint) floor( 0.5 + ( 8.0 * (angle + NLMISC::Pi)/(NLMISC::Pi) ) ); + sint direction =(sint) floor( 0.5 + ( 8.0 * (angle.d + NLMISC::Pi)/(NLMISC::Pi) ) ); direction = ((direction%16)+16)%16; static const string txts[]= { diff --git a/code/ryzom/common/src/game_share/inventories.h b/code/ryzom/common/src/game_share/inventories.h index 3d2bbae90..ef093bd2b 100644 --- a/code/ryzom/common/src/game_share/inventories.h +++ b/code/ryzom/common/src/game_share/inventories.h @@ -412,13 +412,18 @@ private: struct COneProp { - TItemPropId ItemPropId; + union + { + TItemPropId ItemPropId; + uint32 ItemPropIdUint32; + }; + sint32 ItemPropValue; void serial( NLMISC::CBitMemStream& bms ) { - bms.serial( (uint32&)ItemPropId, NbBitsForItemPropId ); - bms.serial( (uint32&)ItemPropValue, CItemSlot::DataBitSize[ItemPropId] ); + bms.serial((uint32&)ItemPropIdUint32, NbBitsForItemPropId); + bms.serial((uint32&)ItemPropValue, CItemSlot::DataBitSize[ItemPropId]); } }; diff --git a/code/ryzom/common/src/game_share/persistent_data_inline.h b/code/ryzom/common/src/game_share/persistent_data_inline.h index a2d575eef..9c7b46522 100644 --- a/code/ryzom/common/src/game_share/persistent_data_inline.h +++ b/code/ryzom/common/src/game_share/persistent_data_inline.h @@ -18,6 +18,20 @@ // inlines CPersistentDataRecord //----------------------------------------------------------------------------- +union C64BitParts +{ + struct + { + uint32 i32_1; + uint32 i32_2; + }; + + sint64 s64; + uint64 u64; + double d; + float f; +}; + inline void CPersistentDataRecord::addString(const std::string& name,uint16 &result) { // check whether the value of 'result' is already correct @@ -96,11 +110,8 @@ inline void CPersistentDataRecord::push(TToken token,sint32 val) inline void CPersistentDataRecord::push(TToken token,sint64 val) { // create a union for splitting the i64 value into 2 32bit parts and map the union onto the input value - struct C64BitParts - { - uint32 i32_1; - uint32 i32_2; - } &valueInBits= *(C64BitParts*)&val; + C64BitParts valueInBits; + valueInBits.s64 = val; // make sure the token is valid #ifdef NL_DEBUG @@ -153,11 +164,8 @@ inline void CPersistentDataRecord::push(TToken token,uint32 val) inline void CPersistentDataRecord::push(TToken token,uint64 val) { // create a union for splitting the i64 value into 2 32bit parts and map the union onto the input value - struct C64BitParts - { - uint32 i32_1; - uint32 i32_2; - } &valueInBits= *(C64BitParts*)&val; + C64BitParts valueInBits; + valueInBits.u64 = val; // make sure the token is valid #ifdef NL_DEBUG @@ -173,6 +181,9 @@ inline void CPersistentDataRecord::push(TToken token,uint64 val) inline void CPersistentDataRecord::push(TToken token,float val) { + C64BitParts valueInBits; + valueInBits.f = val; + // make sure the token is valid #ifdef NL_DEBUG BOMB_IF( ((token<<3)>>3)!= token, "Invalid token - Insufficient numeric precision", return); @@ -180,17 +191,14 @@ inline void CPersistentDataRecord::push(TToken token,float val) // store the token and value to the relavent data buffers _TokenTable.push_back((token<<3)+CArg::FLOAT_TOKEN); - _ArgTable.push_back(*(sint32*)&val); + _ArgTable.push_back(valueInBits.i32_1); } inline void CPersistentDataRecord::push(TToken token,double val) { // create a union for splitting the i64 value into 2 32bit parts and map the union onto the input value - struct C64BitParts - { - uint32 i32_1; - uint32 i32_2; - } &valueInBits= *(C64BitParts*)&val; + C64BitParts valueInBits; + valueInBits.d = val; // make sure the token is valid #ifdef NL_DEBUG