diff --git a/tools/quake3/common/cmdlib.h b/tools/quake3/common/cmdlib.h index 60acf5b9..02af1745 100644 --- a/tools/quake3/common/cmdlib.h +++ b/tools/quake3/common/cmdlib.h @@ -89,6 +89,8 @@ void_ptr safe_calloc_info( size_t size, const char* info ); #define safe_calloc_info( size, info ) calloc( 1, size ) #endif /* SAFE_MALLOC */ +#define offsetof_array( TYPE, ARRAY_MEMBER, ARRAY_SIZE ) ( offsetof( TYPE, ARRAY_MEMBER[0] ) + sizeof( TYPE::ARRAY_MEMBER[0] ) * ARRAY_SIZE ) + static inline bool strEmpty( const char* string ){ return *string == '\0'; diff --git a/tools/quake3/common/polylib.cpp b/tools/quake3/common/polylib.cpp index 85c3eeb4..fe59dc7d 100644 --- a/tools/quake3/common/polylib.cpp +++ b/tools/quake3/common/polylib.cpp @@ -45,7 +45,7 @@ winding_t *AllocWinding( int points ){ if ( points >= MAX_POINTS_ON_WINDING ) { Error( "AllocWinding failed: MAX_POINTS_ON_WINDING exceeded" ); } - return safe_calloc( offsetof( winding_t, p[points] ) ); + return safe_calloc( offsetof_array( winding_t, p, points ) ); } /* @@ -57,7 +57,7 @@ winding_accu_t *AllocWindingAccu( int points ){ if ( points >= MAX_POINTS_ON_WINDING ) { Error( "AllocWindingAccu failed: MAX_POINTS_ON_WINDING exceeded" ); } - return safe_calloc( offsetof( winding_accu_t, p[points] ) ); + return safe_calloc( offsetof_array( winding_accu_t, p, points ) ); } /* @@ -362,7 +362,7 @@ winding_t *CopyWinding( const winding_t *w ){ if ( !w ) { Error( "CopyWinding: winding is NULL" ); } - return void_ptr( memcpy( AllocWinding( w->numpoints ), w, offsetof( winding_t, p[w->numpoints] ) ) ); + return void_ptr( memcpy( AllocWinding( w->numpoints ), w, offsetof_array( winding_t, p, w->numpoints ) ) ); } /* @@ -375,7 +375,7 @@ winding_accu_t *CopyWindingAccuIncreaseSizeAndFreeOld( winding_accu_t *w ){ Error( "CopyWindingAccuIncreaseSizeAndFreeOld: winding is NULL" ); } - winding_accu_t *c = void_ptr( memcpy( AllocWindingAccu( w->numpoints + 1 ), w, offsetof( winding_accu_t, p[w->numpoints] ) ) ); + winding_accu_t *c = void_ptr( memcpy( AllocWindingAccu( w->numpoints + 1 ), w, offsetof_array( winding_accu_t, p, w->numpoints ) ) ); FreeWindingAccu( w ); return c; } diff --git a/tools/quake3/q3map2/autopk3.cpp b/tools/quake3/q3map2/autopk3.cpp index c75556d0..74e610d5 100644 --- a/tools/quake3/q3map2/autopk3.cpp +++ b/tools/quake3/q3map2/autopk3.cpp @@ -39,7 +39,7 @@ struct StrList }; static inline StrList* StrList_allocate( size_t strNum ){ - StrList* ret = safe_calloc( offsetof( StrList, s[strNum] ) ); + StrList* ret = safe_calloc( offsetof_array( StrList, s, strNum ) ); ret->n = 0; ret->max = strNum; return ret; diff --git a/tools/quake3/q3map2/brush.cpp b/tools/quake3/q3map2/brush.cpp index 7b83e1f4..a0e35333 100644 --- a/tools/quake3/q3map2/brush.cpp +++ b/tools/quake3/q3map2/brush.cpp @@ -85,7 +85,7 @@ int CountBrushList( brush_t *brushes ){ */ brush_t *AllocBrush( int numSides ){ - return safe_calloc( offsetof( brush_t, sides[numSides] ) ); + return safe_calloc( offsetof_array( brush_t, sides, numSides ) ); } @@ -112,7 +112,7 @@ void FreeBrush( brush_t *b ){ } /* ydnar: overwrite it */ - memset( b, 0xFE, offsetof( brush_t, sides[b->numsides] ) ); + memset( b, 0xFE, offsetof_array( brush_t, sides, b->numsides ) ); *( (unsigned int*) b ) = 0xFEFEFEFE; /* free it */ @@ -148,7 +148,7 @@ void FreeBrushList( brush_t *brushes ){ brush_t *CopyBrush( const brush_t *brush ){ /* copy brush */ brush_t *newBrush = AllocBrush( brush->numsides ); - memcpy( newBrush, brush, offsetof( brush_t, sides[brush->numsides] ) ); + memcpy( newBrush, brush, offsetof_array( brush_t, sides, brush->numsides ) ); /* ydnar: nuke linked list */ newBrush->next = NULL; diff --git a/tools/quake3/q3map2/vis.cpp b/tools/quake3/q3map2/vis.cpp index f2d5eafe..314e8dc2 100644 --- a/tools/quake3/q3map2/vis.cpp +++ b/tools/quake3/q3map2/vis.cpp @@ -48,11 +48,11 @@ visPlane_t PlaneFromWinding( const fixedWinding_t *w ){ ydnar: altered this a bit to reconcile multiply-defined winding_t */ -fixedWinding_t *NewFixedWinding( int points ){ - if ( points > MAX_POINTS_ON_WINDING ) { - Error( "NewWinding: %i points", points ); +fixedWinding_t *NewFixedWinding( int numpoints ){ + if ( numpoints > MAX_POINTS_ON_WINDING ) { + Error( "NewWinding: %i points", numpoints ); } - return safe_calloc( offsetof( fixedWinding_t, points[points] ) ); + return safe_calloc( offsetof_array( fixedWinding_t, points, numpoints ) ); } diff --git a/tools/quake3/q3map2/visflow.cpp b/tools/quake3/q3map2/visflow.cpp index 2abc6a60..6673db12 100644 --- a/tools/quake3/q3map2/visflow.cpp +++ b/tools/quake3/q3map2/visflow.cpp @@ -1332,8 +1332,7 @@ void CreatePassages( int portalnum ){ /* ydnar: prefer correctness to stack overflow */ //% memcpy( &in, p->winding, (int)((fixedWinding_t *)0)->points[p->winding->numpoints] ); if ( p->winding->numpoints <= MAX_POINTS_ON_FIXED_WINDING ) { - memcpy( &in, p->winding, (size_t)&( ( (fixedWinding_t *)0 )->points[p->winding->numpoints] ) ); -// memcpy( &in, p->winding, offsetof( fixedWinding_t, points[p->winding->numpoints] ) ); + memcpy( &in, p->winding, offsetof_array( fixedWinding_t, points, p->winding->numpoints ) ); } else{ memcpy( &in, p->winding, sizeof( fixedWinding_t ) );