Merge pull request #580 from tkoeppe/flexi

Use standard "offsetof" facility rather than manual code involving null pointer dereferences.
This commit is contained in:
Timothee "TTimo" Besset 2018-01-21 14:23:29 -06:00 committed by GitHub
commit e44968a80f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 5 deletions

View file

@ -20,6 +20,8 @@
*/
#include <stddef.h>
#include "cmdlib.h"
#include "mathlib.h"
#include "inout.h"
@ -467,7 +469,7 @@ winding_t *CopyWinding( winding_t *w ){
}
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 );
return c;
}

View file

@ -128,7 +128,7 @@ void FreeBrush( brush_t *b ){
}
/* 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;
/* free it */
@ -171,7 +171,7 @@ brush_t *CopyBrush( brush_t *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 );
memcpy( newBrush, brush, size );

View file

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

View file

@ -65,7 +65,7 @@ fixedWinding_t *NewFixedWinding( int 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 );
memset( w, 0, size );

View file

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