Skip to content

Commit 619923e

Browse files
committed
Plugins (gforce): Fix type punning warnings.
1 parent 70902aa commit 619923e

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

libvisual-plugins/plugins/actor/gforce/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ SET(GFORCE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/plugins/actor/gforce)
22
SET(GFORCE_DATA_DIR ${LV_PLUGIN_DATA_DIR}/actor/actor_gforce)
33

44
SET(GFORCE_COMPILE_DEFS UNIX_X _REENTRANT)
5-
SET(GFORCE_COMPILE_OPTIONS -fno-strict-aliasing -Wno-unknown-warning-option -Wno-sometimes-uninitialized)
5+
SET(GFORCE_COMPILE_OPTIONS -Werror)
66

77
# Required on GCC but not Clang for some reason.
88
IF(NOT MINGW)

libvisual-plugins/plugins/actor/gforce/Common/GeneralTools/Headers/XFloatList.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
// by Andrew O'Meara
55

66
#include "XPtrList.h"
7+
#include <string.h>
78

89
class XLongList;
910

11+
inline float void_ptr_to_float(const void* value)
12+
{
13+
float result;
14+
memcpy( &result, &value, sizeof( result ) );
15+
return result;
16+
}
17+
18+
inline void* float_to_void_ptr(float value)
19+
{
20+
void* result = 0;
21+
memcpy( &result, &value, sizeof( value ) );
22+
return result;
23+
}
1024

1125
class XFloatList {
1226

1327
public:
1428
XFloatList( ListOrderingT inOrdering = cOrderNotImportant ); // See XPtrList.h for ListOrderingT choices
1529

1630
// See XPtrList.h for description of functions.
17-
virtual long Add( float inNum ) { return mList.Add( *((void**) &inNum) ); }
31+
virtual long Add( float inNum ) { return mList.Add( float_to_void_ptr( inNum ) ); }
1832
virtual void Add( const XFloatList& inList ) { mList.Add( inList.mList ); }
1933
virtual bool RemoveElement( long inIndex ) { return mList.RemoveElement( inIndex ); }
2034
virtual void RemoveAll() { mList.RemoveAll(); }
21-
virtual float Fetch( long inIndex ) { long t = (long) mList.Fetch( inIndex ); return *((float*) &t);}
35+
virtual float Fetch( long inIndex ) { return void_ptr_to_float( mList.Fetch( inIndex ) ); }
2236
virtual bool Fetch( long inIndex, float* ioPtrDest ) const { return mList.Fetch( inIndex, (void**)ioPtrDest ); }
2337
virtual long Count() const { return mList.Count(); }
2438

@@ -36,7 +50,7 @@ class XFloatList {
3650
// Smoothes all the floats in this list
3751
void GaussSmooth( float inSigma );
3852

39-
float operator[] ( const long inIndex ) { long t = (long) mList.Fetch( inIndex ); return *((float*) &t); }
53+
float operator[] ( const long inIndex ) { return void_ptr_to_float(mList.Fetch( inIndex )); }
4054

4155
// Generic utility fcn to gauss-smooth a 1D curve.
4256
static void GaussSmooth( float inSigma, long inN, float inSrceDest[] );

libvisual-plugins/plugins/actor/gforce/Common/GeneralTools/XFloatList.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void XFloatList::FindMeans( long inNumMeans, float outMeans[], float inSigmaScal
6464

6565
// If this a local max. (Note: this could/should be improved for neighbors that are equal)
6666
if ( ( cen > left && cen >= right ) ) {
67-
sepCandidates.Put( i, *((void**) &cen) );
67+
sepCandidates.Put( i, float_to_void_ptr( cen ) );
6868
}
6969
}
7070

@@ -284,7 +284,7 @@ void XFloatList::Rank( XLongList& outRank, long inNumToRank ) const {
284284

285285

286286
int XFloatList::sQSFloatComparitor( const void* inA, const void* inB ) {
287-
float diff = *((float*) inB) - *((float*) inA);
287+
float diff = void_ptr_to_float(inB) - void_ptr_to_float(inA);
288288
if ( diff > 0.0 )
289289
return 1;
290290
else if ( diff < 0.0 )
@@ -296,7 +296,7 @@ int XFloatList::sQSFloatComparitor( const void* inA, const void* inB ) {
296296

297297

298298
int XFloatList::sFloatComparitor( const void* inA, const void* inB ) {
299-
float diff = *((float*) &inB) - *((float*) &inA);
299+
float diff = void_ptr_to_float(inA) - void_ptr_to_float(inB);
300300
if ( diff > 0.0 )
301301
return 1;
302302
else if ( diff < 0.0 )

libvisual-plugins/plugins/actor/gforce/Common/io/CEgIStream.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "CEgIStream.h"
2-
3-
//#include <string.h>
2+
#include <stdint.h>
3+
#include <string.h>
44

55

66
UtilStr CEgIStream::sTemp;
@@ -32,8 +32,20 @@ long CEgIStream::GetLong() {
3232

3333

3434
float CEgIStream::GetFloat() {
35-
long v = GetLong();
36-
return *( (float*) &v );
35+
uint32_t c, n = GetByte();
36+
37+
c = GetByte();
38+
n = n | ( c << 8 );
39+
40+
c = GetByte();
41+
n = n | ( c << 16 );
42+
43+
c = GetByte();
44+
n = n | ( c << 24 );
45+
46+
float result;
47+
memcpy(&result, &n, sizeof(result));
48+
return result;
3749
}
3850

3951

libvisual-plugins/plugins/actor/gforce/Common/math/ExprVirtualMachine.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <libvisual/libvisual.h>
2222
#include <limits>
2323
#include <stdlib.h>
24-
24+
#include <string.h>
2525

2626
#define __addInst( opcode, data16 ) long op = (opcode) | (data16); \
2727
mProgram.Append( &op, sizeof(long) );
@@ -85,7 +85,7 @@ ExprUserFcn ExprVirtualMachine::sZeroFcn = { 0, { 0 } };
8585
case cABS: r = fabs( r ); break; \
8686
case cSIN: r = sin( r ); break; \
8787
case cCOS: r = cos( r ); break; \
88-
case cSEED: i = *((long*) &r); \
88+
case cSEED: i = pun_float_to_long(r); \
8989
size = i % 31; \
9090
srand( ( i << size ) | ( i >> ( 32 - size ) ) ); break; \
9191
case cTAN: r = tan( r ); break; \
@@ -114,6 +114,13 @@ ExprUserFcn ExprVirtualMachine::sZeroFcn = { 0, { 0 } };
114114
case '%': { long tt = r2; r1 = (tt != 0) ? (( (long) r1 ) % tt) : 0.0; break; } \
115115
}
116116

117+
inline long pun_float_to_long(float x)
118+
{
119+
long result = 0;
120+
memcpy(&result, &x, sizeof(x));
121+
return result;
122+
}
123+
117124
ExprVirtualMachine::ExprVirtualMachine() {
118125
mPCStart = 0;
119126
mPCEnd = 0;

0 commit comments

Comments
 (0)