fix new gcc build error: ‘N’ is not a constant expression, while using offsetof

This commit is contained in:
Garux 2021-06-22 20:49:42 +03:00
parent 7796044913
commit 4697d7940c
6 changed files with 15 additions and 14 deletions

View File

@ -89,6 +89,8 @@ void_ptr safe_calloc_info( size_t size, const char* info );
#define safe_calloc_info( size, info ) calloc( 1, size ) #define safe_calloc_info( size, info ) calloc( 1, size )
#endif /* SAFE_MALLOC */ #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 ){ static inline bool strEmpty( const char* string ){
return *string == '\0'; return *string == '\0';

View File

@ -45,7 +45,7 @@ winding_t *AllocWinding( int points ){
if ( points >= MAX_POINTS_ON_WINDING ) { if ( points >= MAX_POINTS_ON_WINDING ) {
Error( "AllocWinding failed: MAX_POINTS_ON_WINDING exceeded" ); 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 ) { if ( points >= MAX_POINTS_ON_WINDING ) {
Error( "AllocWindingAccu failed: MAX_POINTS_ON_WINDING exceeded" ); 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 ) { if ( !w ) {
Error( "CopyWinding: winding is NULL" ); 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" ); 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 ); FreeWindingAccu( w );
return c; return c;
} }

View File

@ -39,7 +39,7 @@ struct StrList
}; };
static inline StrList* StrList_allocate( size_t strNum ){ 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->n = 0;
ret->max = strNum; ret->max = strNum;
return ret; return ret;

View File

@ -85,7 +85,7 @@ int CountBrushList( brush_t *brushes ){
*/ */
brush_t *AllocBrush( int numSides ){ 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 */ /* 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; *( (unsigned int*) b ) = 0xFEFEFEFE;
/* free it */ /* free it */
@ -148,7 +148,7 @@ void FreeBrushList( brush_t *brushes ){
brush_t *CopyBrush( const brush_t *brush ){ brush_t *CopyBrush( const brush_t *brush ){
/* copy brush */ /* copy brush */
brush_t *newBrush = AllocBrush( brush->numsides ); 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 */ /* ydnar: nuke linked list */
newBrush->next = NULL; newBrush->next = NULL;

View File

@ -48,11 +48,11 @@ visPlane_t PlaneFromWinding( const fixedWinding_t *w ){
ydnar: altered this a bit to reconcile multiply-defined winding_t ydnar: altered this a bit to reconcile multiply-defined winding_t
*/ */
fixedWinding_t *NewFixedWinding( int points ){ fixedWinding_t *NewFixedWinding( int numpoints ){
if ( points > MAX_POINTS_ON_WINDING ) { if ( numpoints > MAX_POINTS_ON_WINDING ) {
Error( "NewWinding: %i points", points ); Error( "NewWinding: %i points", numpoints );
} }
return safe_calloc( offsetof( fixedWinding_t, points[points] ) ); return safe_calloc( offsetof_array( fixedWinding_t, points, numpoints ) );
} }

View File

@ -1332,8 +1332,7 @@ void CreatePassages( int portalnum ){
/* ydnar: prefer correctness to stack overflow */ /* ydnar: prefer correctness to stack overflow */
//% memcpy( &in, p->winding, (int)((fixedWinding_t *)0)->points[p->winding->numpoints] ); //% memcpy( &in, p->winding, (int)((fixedWinding_t *)0)->points[p->winding->numpoints] );
if ( p->winding->numpoints <= MAX_POINTS_ON_FIXED_WINDING ) { 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_array( fixedWinding_t, points, p->winding->numpoints ) );
// memcpy( &in, p->winding, offsetof( fixedWinding_t, points[p->winding->numpoints] ) );
} }
else{ else{
memcpy( &in, p->winding, sizeof( fixedWinding_t ) ); memcpy( &in, p->winding, sizeof( fixedWinding_t ) );