From 73663e30dcaf17893df8f34156a58481bec27984 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 24 Dec 2016 14:01:21 +0100 Subject: [PATCH] Changed: Use a global C64BitsParts union to avoid strict aliasing warnings --- code/nel/include/nel/misc/common.h | 19 ++++++++ code/nel/src/gui/interface_property.cpp | 20 +++------ code/ryzom/client/src/character_cl.cpp | 13 ++++-- code/ryzom/client/src/commands.cpp | 26 ++++------- code/ryzom/client/src/entities.cpp | 12 ++--- .../interface_expr_user_fct_game.cpp | 11 +---- code/ryzom/client/src/r2/editor.cpp | 11 ++--- code/ryzom/client/src/user_entity.cpp | 12 ++--- .../common/src/game_share/persistent_data.h | 1 + .../src/game_share/persistent_data_inline.h | 44 +++++++------------ 10 files changed, 71 insertions(+), 98 deletions(-) diff --git a/code/nel/include/nel/misc/common.h b/code/nel/include/nel/misc/common.h index c7c1369c5..4c2b38cca 100644 --- a/code/nel/include/nel/misc/common.h +++ b/code/nel/include/nel/misc/common.h @@ -714,6 +714,25 @@ inline T iavoid0(T x) return x; } +// Helper to convert in memory between types of different sizes +union C64BitsParts +{ + // unsigned + uint64 u64[1]; + uint32 u32[2]; + uint16 u16[4]; + uint8 u8[8]; + + // signed + sint64 i64[1]; + sint32 i32[2]; + sint16 i16[4]; + sint8 i8[8]; + + // floats + double d[1]; + float f[2]; +}; } // NLMISC diff --git a/code/nel/src/gui/interface_property.cpp b/code/nel/src/gui/interface_property.cpp index ab375ae5e..5b11c9b74 100644 --- a/code/nel/src/gui/interface_property.cpp +++ b/code/nel/src/gui/interface_property.cpp @@ -20,6 +20,7 @@ #include "nel/gui/interface_property.h" #include "nel/gui/interface_common.h" #include "nel/gui/db_manager.h" +#include "nel/misc/common.h" using namespace NLMISC; using namespace std; @@ -30,13 +31,6 @@ using namespace std; namespace NLGUI { - // helper to convert double <> sint64 - union C64BitsParts - { - sint64 i64; - double d; - }; - bool CInterfaceProperty::link( CCDBNodeLeaf *dbNode ) { _VolatileValue = dbNode; @@ -79,15 +73,15 @@ namespace NLGUI void CInterfaceProperty::setDouble(double value) { C64BitsParts parts; - parts.d = value; - setSInt64(parts.i64); + parts.d[0] = value; + setSInt64(parts.i64[0]); } double CInterfaceProperty::getDouble() const { C64BitsParts parts; - parts.i64 = getSInt64(); - return parts.d; + parts.i64[0] = getSInt64(); + return parts.d[0]; } // ***************** @@ -129,8 +123,8 @@ namespace NLGUI { _VolatileValue = NLGUI::CDBManager::getInstance()->getDbProp(id); C64BitsParts buf; - fromString(ptr, buf.d); - _VolatileValue->setValue64(buf.i64); + fromString(ptr, buf.d[0]); + _VolatileValue->setValue64(buf.i64[0]); } else { diff --git a/code/ryzom/client/src/character_cl.cpp b/code/ryzom/client/src/character_cl.cpp index e1eee00b0..228e3dbde 100644 --- a/code/ryzom/client/src/character_cl.cpp +++ b/code/ryzom/client/src/character_cl.cpp @@ -1309,8 +1309,9 @@ void CCharacterCL::updateVisualPropertyMode(const NLMISC::TGameCycle &gameCycle, nlwarning("CH::updtVPMode:%d: Cannot find the property 'PROPERTY_ORIENTATION(%d)'.", _Slot, CLFECOMMON::PROPERTY_ORIENTATION); return; } - const sint64 &ori = nodeOri->getValue64(); - float angleZ = *(float *)(&ori); + C64BitsParts parts; + parts.i64[0] = nodeOri->getValue64(); + float angleZ = parts.f[0]; // server forces the entity orientation even if it cannot turn front(CVector((float)cos(angleZ), (float)sin(angleZ), 0.f), true, true, true); dir(front(), false, false); @@ -5228,7 +5229,9 @@ bool CCharacterCL::applyStage(CStage &stage) pair resultTeta = stage.property(PROPERTY_ORIENTATION); if(resultTeta.first) { - float angleZ = *(float *)(&resultTeta.second); + C64BitsParts parts; + parts.i64[0] = resultTeta.second; + float angleZ = parts.f[0]; // server forces the entity orientation even if it cannot turn front(CVector((float)cos(angleZ), (float)sin(angleZ), 0.f), true, true, true); @@ -5242,7 +5245,9 @@ bool CCharacterCL::applyStage(CStage &stage) if(resultMode.first) { // Get the mode from stage. - uint8 mo = *(uint8 *)(&resultMode.second); + C64BitsParts parts; + parts.i64[0] = resultMode.second; + uint8 mo = parts.u8[0]; // If the mode wanted is not the same, change the mode wanted. if(mo != _ModeWanted) { diff --git a/code/ryzom/client/src/commands.cpp b/code/ryzom/client/src/commands.cpp index 35389633c..70cbfc229 100644 --- a/code/ryzom/client/src/commands.cpp +++ b/code/ryzom/client/src/commands.cpp @@ -2041,15 +2041,10 @@ NLMISC_COMMAND(entity, "Create an entity on the user or just remove it if Form n node = NLGUI::CDBManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", slot)+":P"+toString("%d", CLFECOMMON::PROPERTY_ORIENTATION), false); if(node) { - union C64BitsRot - { - sint64 i64; - float f; - }; - - C64BitsRot rot; - rot.f = (float)atan2(UserEntity->front().y, UserEntity->front().x); - node->setValue64(rot.i64); + C64BitsParts rot; + rot.f[0] = (float)atan2(UserEntity->front().y, UserEntity->front().x); + rot.f[1] = 0.f; // to be sure 64 bits value is initialized + node->setValue64(rot.i64[0]); } // Set Mode node = NLGUI::CDBManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", slot)+":P"+toString("%d", CLFECOMMON::PROPERTY_MODE), false); @@ -2847,16 +2842,11 @@ NLMISC_COMMAND(orient, "Orient an entity", "Slot: [1-254] orient(degree) [dt(tic // Write the position in the DB. float fRot= (float)(rot*Pi/180.f); - union C64BitsRot - { - sint64 i64; - float f; - }; + C64BitsParts r; + r.f[0] = fRot; + r.f[1] = 0.f; // to be sure 64 bits value is initialized - C64BitsRot r; - r.f = fRot; - - IngameDbMngr.setProp("Entities:E" + toString(slot) + ":P"+toString(CLFECOMMON::PROPERTY_ORIENTATION), r.i64); + IngameDbMngr.setProp("Entities:E" + toString(slot) + ":P"+toString(CLFECOMMON::PROPERTY_ORIENTATION), r.u32[0]); // Update the position. EntitiesMngr.updateVisualProperty(NetMngr.getCurrentServerTick()+dt, slot, CLFECOMMON::PROPERTY_ORIENTATION); } diff --git a/code/ryzom/client/src/entities.cpp b/code/ryzom/client/src/entities.cpp index 7e682a588..382f2f7e1 100644 --- a/code/ryzom/client/src/entities.cpp +++ b/code/ryzom/client/src/entities.cpp @@ -2584,16 +2584,10 @@ void CEntityManager::logPropertyChange(CLFECOMMON::TCLEntityId who, const CStage // Orientation else if(propLoged[i]==CLFECOMMON::PROPERTY_ORIENTATION) { - union C64BitsRot - { - sint64 i64; - float f; - }; + C64BitsParts rot; + rot.i64[0] = value; - C64BitsRot rot; - rot.i64 = value; - - valStr= toString("%d", sint32(rot.f*180/Pi)); + valStr= toString(sint32(rot.f[0]*180/Pi)); } 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 ac75afc82..bb8e65ee2 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,19 +146,12 @@ static DECLARE_INTERFACE_USER_FCT(getCompassText) return false; } - // helper - union C64BitsParts - { - sint64 i64; - double d; - }; - //get the direction // sint64 in the databae. C64BitsParts angle; - angle.i64 = args[0].getInteger(); + angle.i64[0] = args[0].getInteger(); // cast as double now. - sint direction =(sint) floor( 0.5 + ( 8.0 * (angle.d + NLMISC::Pi)/(NLMISC::Pi) ) ); + sint direction =(sint) floor( 0.5 + ( 8.0 * (angle.d[0] + NLMISC::Pi)/(NLMISC::Pi) ) ); direction = ((direction%16)+16)%16; static const string txts[]= { diff --git a/code/ryzom/client/src/r2/editor.cpp b/code/ryzom/client/src/r2/editor.cpp index 72d3a66e8..7ef8ab26e 100644 --- a/code/ryzom/client/src/r2/editor.cpp +++ b/code/ryzom/client/src/r2/editor.cpp @@ -4893,13 +4893,10 @@ CEntityCL *CEditor::createEntity(uint slot, const NLMISC::CSheetId &sheetId, con node = NLGUI::CDBManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", slot)+":P"+toString("%d", CLFECOMMON::PROPERTY_ORIENTATION), false); if(node) { - union - { - uint64 heading64; - float headingFloat; - }; - headingFloat = heading; - node->setValue64(heading64); + C64BitsParts parts; + parts.f[0] = heading; + parts.f[1] = 0.f; + node->setValue64(parts.i64[0]); } // Set Mode node = NLGUI::CDBManager::getInstance()->getDbProp("SERVER:Entities:E"+toString("%d", slot)+":P"+toString("%d", CLFECOMMON::PROPERTY_MODE), false); diff --git a/code/ryzom/client/src/user_entity.cpp b/code/ryzom/client/src/user_entity.cpp index 2b200d2e6..3fd761262 100644 --- a/code/ryzom/client/src/user_entity.cpp +++ b/code/ryzom/client/src/user_entity.cpp @@ -789,18 +789,12 @@ bool CUserEntity::mode(MBEHAV::EMode m) case MBEHAV::COMBAT: case MBEHAV::COMBAT_FLOAT: { - union C64BitsRot - { - sint64 i64; - float f; - }; - - C64BitsRot rot; + C64BitsParts rot; // Compute the angle const string propName = toString("SERVER:Entities:E%d:P%d", _Slot, CLFECOMMON::PROPERTY_ORIENTATION); - rot.i64 = NLGUI::CDBManager::getInstance()->getDbProp(propName)->getValue64(); - _TargetAngle = rot.f; + rot.i64[0] = NLGUI::CDBManager::getInstance()->getDbProp(propName)->getValue64(); + _TargetAngle = rot.f[0]; // Initialize controls for the combat. UserControls.startCombat(); diff --git a/code/ryzom/common/src/game_share/persistent_data.h b/code/ryzom/common/src/game_share/persistent_data.h index 45147a0ad..3d4910160 100644 --- a/code/ryzom/common/src/game_share/persistent_data.h +++ b/code/ryzom/common/src/game_share/persistent_data.h @@ -37,6 +37,7 @@ #include "nel/misc/entity_id.h" #include "nel/misc/sheet_id.h" #include "nel/misc/sstring.h" +#include "nel/misc/common.h" #include "utils.h" #include 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 9c7b46522..8541404dc 100644 --- a/code/ryzom/common/src/game_share/persistent_data_inline.h +++ b/code/ryzom/common/src/game_share/persistent_data_inline.h @@ -18,20 +18,6 @@ // 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 @@ -110,8 +96,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 - C64BitParts valueInBits; - valueInBits.s64 = val; + NLMISC::C64BitsParts valueInBits; + valueInBits.i64[0] = val; // make sure the token is valid #ifdef NL_DEBUG @@ -120,9 +106,9 @@ inline void CPersistentDataRecord::push(TToken token,sint64 val) // store the token and value to the relavent data buffers _TokenTable.push_back((token<<3)+CArg::EXTEND_TOKEN); - _ArgTable.push_back(valueInBits.i32_1); + _ArgTable.push_back(valueInBits.u32[0]); _TokenTable.push_back((token<<3)+CArg::SINT_TOKEN); - _ArgTable.push_back(valueInBits.i32_2); + _ArgTable.push_back(valueInBits.u32[0]); } inline void CPersistentDataRecord::push(TToken token,uint8 val) @@ -164,8 +150,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 - C64BitParts valueInBits; - valueInBits.u64 = val; + NLMISC::C64BitsParts valueInBits; + valueInBits.u64[0] = val; // make sure the token is valid #ifdef NL_DEBUG @@ -174,15 +160,15 @@ inline void CPersistentDataRecord::push(TToken token,uint64 val) // store the token and value to the relavent data buffers _TokenTable.push_back((token<<3)+CArg::EXTEND_TOKEN); - _ArgTable.push_back(valueInBits.i32_1); + _ArgTable.push_back(valueInBits.u32[0]); _TokenTable.push_back((token<<3)+CArg::UINT_TOKEN); - _ArgTable.push_back(valueInBits.i32_2); + _ArgTable.push_back(valueInBits.u32[1]); } inline void CPersistentDataRecord::push(TToken token,float val) { - C64BitParts valueInBits; - valueInBits.f = val; + NLMISC::C64BitsParts valueInBits; + valueInBits.f[0] = val; // make sure the token is valid #ifdef NL_DEBUG @@ -191,14 +177,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(valueInBits.i32_1); + _ArgTable.push_back(valueInBits.u32[0]); } 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 - C64BitParts valueInBits; - valueInBits.d = val; + NLMISC::C64BitsParts valueInBits; + valueInBits.d[0] = val; // make sure the token is valid #ifdef NL_DEBUG @@ -207,9 +193,9 @@ inline void CPersistentDataRecord::push(TToken token,double val) // store the token and value to the relavent data buffers _TokenTable.push_back((token<<3)+CArg::EXTEND_TOKEN); - _ArgTable.push_back(valueInBits.i32_1); + _ArgTable.push_back(valueInBits.u32[0]); _TokenTable.push_back((token<<3)+CArg::FLOAT_TOKEN); - _ArgTable.push_back(valueInBits.i32_2); + _ArgTable.push_back(valueInBits.u32[1]); } inline void CPersistentDataRecord::push(TToken token,const std::string& val)