Use standard "offsetof" facility rather than manual code involving null pointer dereferences.

This commit is contained in:
Thomas Köppe 2018-01-21 01:54:24 +00:00
parent 3c49dd161d
commit ec0907f7ca
5 changed files with 8 additions and 5 deletions

View file

@ -20,6 +20,8 @@
*/ */
#include <stddef.h>
#include "cmdlib.h" #include "cmdlib.h"
#include "mathlib.h" #include "mathlib.h"
#include "inout.h" #include "inout.h"
@ -467,7 +469,7 @@ winding_t *CopyWinding( winding_t *w ){
} }
c = AllocWinding( w->numpoints ); c = AllocWinding( w->numpoints );
size = (int)( (size_t)( (winding_t *)0 )->p[w->numpoints] ); size = offsetof( winding_t, p ) + sizeof( *w->p ) * w->numpoints;
memcpy( c, w, size ); memcpy( c, w, size );
return c; return c;
} }

View file

@ -128,7 +128,7 @@ void FreeBrush( brush_t *b ){
} }
/* ydnar: overwrite it */ /* ydnar: overwrite it */
memset( b, 0xFE, (size_t)&( ( (brush_t*) 0 )->sides[ b->numsides ] ) ); memset( b, 0xFE, offsetof( brush_t, sides ) + sizeof( *b->sides ) * b->numsides );
*( (unsigned int*) b ) = 0xFEFEFEFE; *( (unsigned int*) b ) = 0xFEFEFEFE;
/* free it */ /* free it */
@ -171,7 +171,7 @@ brush_t *CopyBrush( brush_t *brush ){
/* copy brush */ /* copy brush */
size = (size_t)&( ( (brush_t*) 0 )->sides[ brush->numsides ] ); size = offsetof( brush_t, sides ) + sizeof( *brush->sides ) * brush->numsides;
newBrush = AllocBrush( brush->numsides ); newBrush = AllocBrush( brush->numsides );
memcpy( newBrush, brush, size ); memcpy( newBrush, brush, size );

View file

@ -91,6 +91,7 @@ extern int unz_GAME_QL;
#include "vfs.h" #include "vfs.h"
#include "png.h" #include "png.h"
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))

View file

@ -65,7 +65,7 @@ fixedWinding_t *NewFixedWinding( int points ){
Error( "NewWinding: %i points", points ); Error( "NewWinding: %i points", points );
} }
size = (int)( (size_t)( (fixedWinding_t *)0 )->points[points] ); size = offsetof( fixedWinding_t, points ) + sizeof( *w->points ) * points;
w = safe_malloc( size ); w = safe_malloc( size );
memset( w, 0, size ); memset( w, 0, size );

View file

@ -1423,7 +1423,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( fixedWinding_t, points ) + sizeof( *p->winding->points ) * p->winding->numpoints );
} }
else{ else{
memcpy( &in, p->winding, sizeof( fixedWinding_t ) ); memcpy( &in, p->winding, sizeof( fixedWinding_t ) );