From 8a01d3f9b735667bdb8b1243e89d79d7d547bc07 Mon Sep 17 00:00:00 2001
From: Thomas Debesse <dev@illwieckz.net>
Date: Thu, 23 Jan 2020 22:27:12 +0100
Subject: [PATCH] q3map2: use safe_malloc0 when safe_malloc is followed by a
 memset to 0 with the same size

---
 tools/quake3/common/bspfile.c          | 23 ++++-----------
 tools/quake3/common/cmdlib.c           |  3 +-
 tools/quake3/common/imagelib.c         |  8 ++---
 tools/quake3/common/polylib.c          |  6 ++--
 tools/quake3/q3map2/brush.c            |  9 ++----
 tools/quake3/q3map2/bsp.c              |  3 +-
 tools/quake3/q3map2/bspfile_abstract.c | 15 +++-------
 tools/quake3/q3map2/bspfile_ibsp.c     | 12 +++-----
 tools/quake3/q3map2/bspfile_rbsp.c     |  3 +-
 tools/quake3/q3map2/convert_bsp.c      |  3 +-
 tools/quake3/q3map2/decals.c           |  3 +-
 tools/quake3/q3map2/facebsp.c          |  3 +-
 tools/quake3/q3map2/light.c            | 15 ++++------
 tools/quake3/q3map2/light_bounce.c     |  6 ++--
 tools/quake3/q3map2/light_ydnar.c      |  9 ++----
 tools/quake3/q3map2/lightmaps_ydnar.c  | 41 ++++++++++----------------
 tools/quake3/q3map2/map.c              |  3 +-
 tools/quake3/q3map2/model.c            |  6 ++--
 tools/quake3/q3map2/patch.c            |  6 ++--
 tools/quake3/q3map2/portals.c          |  3 +-
 tools/quake3/q3map2/shaders.c          | 12 +++-----
 tools/quake3/q3map2/surface.c          | 12 +++-----
 tools/quake3/q3map2/surface_foliage.c  |  3 +-
 tools/quake3/q3map2/surface_meta.c     |  7 ++---
 tools/quake3/q3map2/vis.c              | 15 ++++------
 tools/quake3/q3map2/visflow.c          | 12 +++-----
 26 files changed, 81 insertions(+), 160 deletions(-)

diff --git a/tools/quake3/common/bspfile.c b/tools/quake3/common/bspfile.c
index 4fc45bd6..c65da46d 100644
--- a/tools/quake3/common/bspfile.c
+++ b/tools/quake3/common/bspfile.c
@@ -96,9 +96,7 @@ void SetLightBytes( int n ){
 		return;
 	}
 
-	lightBytes = safe_malloc_info( numLightBytes, "SetLightBytes" );
-
-	memset( lightBytes, 0, numLightBytes );
+	lightBytes = safe_malloc0_info( numLightBytes, "SetLightBytes" );
 }
 
 void SetGridPoints( int n ){
@@ -112,9 +110,7 @@ void SetGridPoints( int n ){
 		return;
 	}
 
-	gridData = safe_malloc_info( numGridPoints * 8, "SetGridPoints" );
-
-	memset( gridData, 0, numGridPoints * 8 );
+	gridData = safe_malloc0_info( numGridPoints * 8, "SetGridPoints" );
 }
 
 void IncDrawVerts(){
@@ -152,9 +148,7 @@ void SetDrawVerts( int n ){
 	numDrawVerts = n;
 	numDrawVertsBuffer = numDrawVerts;
 
-	drawVerts = safe_malloc_info( sizeof( drawVert_t ) * numDrawVertsBuffer, "IncDrawVerts" );
-
-	memset( drawVerts, 0, n * sizeof( drawVert_t ) );
+	drawVerts = safe_malloc0_info( sizeof( drawVert_t ) * numDrawVertsBuffer, "IncDrawVerts" );
 }
 
 void SetDrawSurfacesBuffer(){
@@ -164,9 +158,7 @@ void SetDrawSurfacesBuffer(){
 
 	numDrawSurfacesBuffer = MAX_MAP_DRAW_SURFS;
 
-	drawSurfaces = safe_malloc_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
-
-	memset( drawSurfaces, 0, MAX_MAP_DRAW_SURFS * sizeof( drawVert_t ) );
+	drawSurfaces = safe_malloc0_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
 }
 
 void SetDrawSurfaces( int n ){
@@ -177,9 +169,7 @@ void SetDrawSurfaces( int n ){
 	numDrawSurfaces = n;
 	numDrawSurfacesBuffer = numDrawSurfaces;
 
-	drawSurfaces = safe_malloc_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
-
-	memset( drawSurfaces, 0, n * sizeof( drawVert_t ) );
+	drawSurfaces = safe_malloc0_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
 }
 
 void BspFilesCleanup(){
@@ -522,8 +512,7 @@ void StripTrailing( char *e ) {
 epair_t *ParseEpair( void ) {
 	epair_t *e;
 
-	e = safe_malloc( sizeof( epair_t ) );
-	memset( e, 0, sizeof( epair_t ) );
+	e = safe_malloc0( sizeof( epair_t ) );
 
 	if ( strlen( token ) >= MAX_KEY - 1 ) {
 		Error( "ParseEpar: token too long" );
diff --git a/tools/quake3/common/cmdlib.c b/tools/quake3/common/cmdlib.c
index 43bf0a35..abbc0d68 100644
--- a/tools/quake3/common/cmdlib.c
+++ b/tools/quake3/common/cmdlib.c
@@ -675,8 +675,7 @@ int    LoadFileBlock( const char *filename, void **bufferptr ){
 	if ( nBlock > 0 ) {
 		nAllocSize += MEM_BLOCKSIZE - nBlock;
 	}
-	buffer = safe_malloc( nAllocSize + 1 );
-	memset( buffer, 0, nAllocSize + 1 );
+	buffer = safe_malloc0( nAllocSize + 1 );
 	SafeRead( f, buffer, length );
 	fclose( f );
 
diff --git a/tools/quake3/common/imagelib.c b/tools/quake3/common/imagelib.c
index 5bc5ff71..661b9ed3 100644
--- a/tools/quake3/common/imagelib.c
+++ b/tools/quake3/common/imagelib.c
@@ -256,8 +256,7 @@ void LoadLBM( const char *filename, byte **picture, byte **palette ){
 			break;
 
 		case CMAPID:
-			cmapbuffer = safe_malloc( 768 );
-			memset( cmapbuffer, 0, 768 );
+			cmapbuffer = safe_malloc0( 768 );
 			memcpy( cmapbuffer, LBM_P, chunklength );
 			break;
 
@@ -572,8 +571,7 @@ void WritePCXfile( const char *filename, byte *data,
 	pcx_t   *pcx;
 	byte        *pack;
 
-	pcx = safe_malloc( width * height * 2 + 1000 );
-	memset( pcx, 0, sizeof( *pcx ) );
+	pcx = safe_malloc0( width * height * 2 + 1000 );
 
 	pcx->manufacturer = 0x0a;   // PCX id
 	pcx->version = 5;           // 256 color
@@ -1145,6 +1143,8 @@ void WriteTGA( const char *filename, byte *data, int width, int height ) {
 	FILE    *f;
 
 	buffer = safe_malloc( width * height * 4 + 18 );
+	/* we may also use safe_malloc0 on the whole instead,
+	 * this would just be a bit slower */
 	memset( buffer, 0, 18 );
 	buffer[2] = 2;      // uncompressed type
 	buffer[12] = width & 255;
diff --git a/tools/quake3/common/polylib.c b/tools/quake3/common/polylib.c
index bef8fbd4..21afcbcc 100644
--- a/tools/quake3/common/polylib.c
+++ b/tools/quake3/common/polylib.c
@@ -66,8 +66,7 @@ winding_t   *AllocWinding( int points ){
 		}
 	}
 	s = sizeof( *w ) + ( points ? sizeof( w->p[0] ) * ( points - 1 ) : 0 );
-	w = safe_malloc( s );
-	memset( w, 0, s );
+	w = safe_malloc0( s );
 	return w;
 }
 
@@ -94,8 +93,7 @@ winding_accu_t *AllocWindingAccu( int points ){
 		}
 	}
 	s = sizeof( *w ) + ( points ? sizeof( w->p[0] ) * ( points - 1 ) : 0 );
-	w = safe_malloc( s );
-	memset( w, 0, s );
+	w = safe_malloc0( s );
 	return w;
 }
 
diff --git a/tools/quake3/q3map2/brush.c b/tools/quake3/q3map2/brush.c
index c6400cbc..db04b880 100644
--- a/tools/quake3/q3map2/brush.c
+++ b/tools/quake3/q3map2/brush.c
@@ -98,8 +98,7 @@ brush_t *AllocBrush( int numSides ){
 		Error( "AllocBrush called with numsides = %d", numSides );
 	}
 	c = (size_t)&( ( (brush_t*) 0 )->sides[ numSides ] );
-	bb = safe_malloc( c );
-	memset( bb, 0, c );
+	bb = safe_malloc0( c );
 	if ( numthreads == 1 ) {
 		numActiveBrushes++;
 	}
@@ -829,8 +828,7 @@ void FilterStructuralBrushesIntoTree( entity_t *e, tree_t *tree ) {
 tree_t *AllocTree( void ){
 	tree_t  *tree;
 
-	tree = safe_malloc( sizeof( *tree ) );
-	memset( tree, 0, sizeof( *tree ) );
+	tree = safe_malloc0( sizeof( *tree ) );
 	ClearBounds( tree->mins, tree->maxs );
 
 	return tree;
@@ -844,8 +842,7 @@ tree_t *AllocTree( void ){
 node_t *AllocNode( void ){
 	node_t  *node;
 
-	node = safe_malloc( sizeof( *node ) );
-	memset( node, 0, sizeof( *node ) );
+	node = safe_malloc0( sizeof( *node ) );
 
 	return node;
 }
diff --git a/tools/quake3/q3map2/bsp.c b/tools/quake3/q3map2/bsp.c
index b3c28002..432de5d5 100644
--- a/tools/quake3/q3map2/bsp.c
+++ b/tools/quake3/q3map2/bsp.c
@@ -719,8 +719,7 @@ int BSPMain( int argc, char **argv ){
 	Sys_Printf( "--- BSP ---\n" );
 
 	SetDrawSurfacesBuffer();
-	mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
-	memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
+	mapDrawSurfs = safe_malloc0( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
 	numMapDrawSurfs = 0;
 
 	tempSource[ 0 ] = '\0';
diff --git a/tools/quake3/q3map2/bspfile_abstract.c b/tools/quake3/q3map2/bspfile_abstract.c
index 62beb16a..80b1028b 100644
--- a/tools/quake3/q3map2/bspfile_abstract.c
+++ b/tools/quake3/q3map2/bspfile_abstract.c
@@ -89,9 +89,7 @@ void SetDrawVerts( int n ){
 	numBSPDrawVerts = n;
 	numBSPDrawVertsBuffer = numBSPDrawVerts;
 
-	bspDrawVerts = safe_malloc_info( sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer, "IncDrawVerts" );
-
-	memset( bspDrawVerts, 0, n * sizeof( bspDrawVert_t ) );
+	bspDrawVerts = safe_malloc0_info( sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer, "IncDrawVerts" );
 }
 
 int numBSPDrawSurfacesBuffer = 0;
@@ -102,9 +100,7 @@ void SetDrawSurfacesBuffer(){
 
 	numBSPDrawSurfacesBuffer = MAX_MAP_DRAW_SURFS;
 
-	bspDrawSurfaces = safe_malloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
-
-	memset( bspDrawSurfaces, 0, MAX_MAP_DRAW_SURFS * sizeof( bspDrawSurface_t ) );
+	bspDrawSurfaces = safe_malloc0_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
 }
 
 void SetDrawSurfaces( int n ){
@@ -115,9 +111,7 @@ void SetDrawSurfaces( int n ){
 	numBSPDrawSurfaces = n;
 	numBSPDrawSurfacesBuffer = numBSPDrawSurfaces;
 
-	bspDrawSurfaces = safe_malloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
-
-	memset( bspDrawSurfaces, 0, n * sizeof( bspDrawSurface_t ) );
+	bspDrawSurfaces = safe_malloc0_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
 }
 
 void BSPFilesCleanup(){
@@ -500,8 +494,7 @@ epair_t *ParseEPair( void ){
 
 
 	/* allocate and clear new epair */
-	e = safe_malloc( sizeof( epair_t ) );
-	memset( e, 0, sizeof( epair_t ) );
+	e = safe_malloc0( sizeof( epair_t ) );
 
 	/* handle key */
 	if ( strlen( token ) >= ( MAX_KEY - 1 ) ) {
diff --git a/tools/quake3/q3map2/bspfile_ibsp.c b/tools/quake3/q3map2/bspfile_ibsp.c
index f610abc8..1c4c994a 100644
--- a/tools/quake3/q3map2/bspfile_ibsp.c
+++ b/tools/quake3/q3map2/bspfile_ibsp.c
@@ -120,8 +120,7 @@ static void AddBrushSidesLump( FILE *file, ibspHeader_t *header ){
 
 	/* allocate output buffer */
 	size = numBSPBrushSides * sizeof( *buffer );
-	buffer = safe_malloc( size );
-	memset( buffer, 0, size );
+	buffer = safe_malloc0( size );
 
 	/* convert */
 	in = bspBrushSides;
@@ -232,8 +231,7 @@ static void AddDrawSurfacesLump( FILE *file, ibspHeader_t *header ){
 
 	/* allocate output buffer */
 	size = numBSPDrawSurfaces * sizeof( *buffer );
-	buffer = safe_malloc( size );
-	memset( buffer, 0, size );
+	buffer = safe_malloc0( size );
 
 	/* convert */
 	in = bspDrawSurfaces;
@@ -330,8 +328,7 @@ static void AddDrawVertsLump( FILE *file, ibspHeader_t *header ){
 
 	/* allocate output buffer */
 	size = numBSPDrawVerts * sizeof( *buffer );
-	buffer = safe_malloc( size );
-	memset( buffer, 0, size );
+	buffer = safe_malloc0( size );
 
 	/* convert */
 	in = bspDrawVerts;
@@ -385,8 +382,7 @@ static void CopyLightGridLumps( ibspHeader_t *header ){
 	numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTGRID, sizeof( *in ) );
 
 	/* allocate buffer */
-	bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
-	memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
+	bspGridPoints = safe_malloc0( numBSPGridPoints * sizeof( *bspGridPoints ) );
 
 	/* copy */
 	in = GetLump( (bspHeader_t*) header, LUMP_LIGHTGRID );
diff --git a/tools/quake3/q3map2/bspfile_rbsp.c b/tools/quake3/q3map2/bspfile_rbsp.c
index b1ff0304..88ebcbf4 100644
--- a/tools/quake3/q3map2/bspfile_rbsp.c
+++ b/tools/quake3/q3map2/bspfile_rbsp.c
@@ -96,8 +96,7 @@ static void CopyLightGridLumps( rbspHeader_t *header ){
 	numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTARRAY, sizeof( *inArray ) );
 
 	/* allocate buffer */
-	bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
-	memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
+	bspGridPoints = safe_malloc0( numBSPGridPoints * sizeof( *bspGridPoints ) );
 
 	/* copy */
 	inArray = GetLump( (bspHeader_t*) header, LUMP_LIGHTARRAY );
diff --git a/tools/quake3/q3map2/convert_bsp.c b/tools/quake3/q3map2/convert_bsp.c
index 87579d26..9a0e0551 100644
--- a/tools/quake3/q3map2/convert_bsp.c
+++ b/tools/quake3/q3map2/convert_bsp.c
@@ -49,8 +49,7 @@ void PseudoCompileBSP( qboolean need_tree, const char *BSPFilePath, const char *
 	int i;
 
 	SetDrawSurfacesBuffer();
-	mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
-	memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
+	mapDrawSurfs = safe_malloc0( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
 	numMapDrawSurfs = 0;
 
 	BeginBSPFile();
diff --git a/tools/quake3/q3map2/decals.c b/tools/quake3/q3map2/decals.c
index f7471c96..dfa8dace 100644
--- a/tools/quake3/q3map2/decals.c
+++ b/tools/quake3/q3map2/decals.c
@@ -620,8 +620,7 @@ static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds,
 	ds2->lightmapScale = ds->lightmapScale;
 	ds2->shadeAngleDegrees = ds->shadeAngleDegrees;
 	ds2->numVerts = w->numpoints;
-	ds2->verts = safe_malloc( ds2->numVerts * sizeof( *ds2->verts ) );
-	memset( ds2->verts, 0, ds2->numVerts * sizeof( *ds2->verts ) );
+	ds2->verts = safe_malloc0( ds2->numVerts * sizeof( *ds2->verts ) );
 
 	/* set vertexes */
 	for ( i = 0; i < ds2->numVerts; i++ )
diff --git a/tools/quake3/q3map2/facebsp.c b/tools/quake3/q3map2/facebsp.c
index e5390ede..48cf798d 100644
--- a/tools/quake3/q3map2/facebsp.c
+++ b/tools/quake3/q3map2/facebsp.c
@@ -49,8 +49,7 @@ int c_faceLeafs;
 face_t  *AllocBspFace( void ) {
 	face_t  *f;
 
-	f = safe_malloc( sizeof( *f ) );
-	memset( f, 0, sizeof( *f ) );
+	f = safe_malloc0( sizeof( *f ) );
 
 	return f;
 }
diff --git a/tools/quake3/q3map2/light.c b/tools/quake3/q3map2/light.c
index 45546f7c..e43906e0 100644
--- a/tools/quake3/q3map2/light.c
+++ b/tools/quake3/q3map2/light.c
@@ -107,8 +107,7 @@ static void CreateSunLight( sun_t *sun ){
 
 		/* create a light */
 		numSunLights++;
-		light = safe_malloc( sizeof( *light ) );
-		memset( light, 0, sizeof( *light ) );
+		light = safe_malloc0( sizeof( *light ) );
 		light->next = lights;
 		lights = light;
 
@@ -254,8 +253,7 @@ void CreateEntityLights( void ){
 
 		/* create a light */
 		numPointLights++;
-		light = safe_malloc( sizeof( *light ) );
-		memset( light, 0, sizeof( *light ) );
+		light = safe_malloc0( sizeof( *light ) );
 		light->next = lights;
 		lights = light;
 
@@ -592,8 +590,7 @@ void CreateSurfaceLights( void ){
 			VectorScale( origin, 0.5f, origin );
 
 			/* create a light */
-			light = safe_malloc( sizeof( *light ) );
-			memset( light, 0, sizeof( *light ) );
+			light = safe_malloc0( sizeof( *light ) );
 			light->next = lights;
 			lights = light;
 
@@ -1857,14 +1854,12 @@ void SetupGrid( void ){
 	numBSPGridPoints = numRawGridPoints;
 
 	/* allocate lightgrid */
-	rawGridPoints = safe_malloc( numRawGridPoints * sizeof( *rawGridPoints ) );
-	memset( rawGridPoints, 0, numRawGridPoints * sizeof( *rawGridPoints ) );
+	rawGridPoints = safe_malloc0( numRawGridPoints * sizeof( *rawGridPoints ) );
 
 	if ( bspGridPoints != NULL ) {
 		free( bspGridPoints );
 	}
-	bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
-	memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
+	bspGridPoints = safe_malloc0( numBSPGridPoints * sizeof( *bspGridPoints ) );
 
 	/* clear lightgrid */
 	for ( i = 0; i < numRawGridPoints; i++ )
diff --git a/tools/quake3/q3map2/light_bounce.c b/tools/quake3/q3map2/light_bounce.c
index 9fe00ef6..02fd8ebd 100644
--- a/tools/quake3/q3map2/light_bounce.c
+++ b/tools/quake3/q3map2/light_bounce.c
@@ -532,8 +532,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw
 	}
 
 	/* create a light */
-	light = safe_malloc( sizeof( *light ) );
-	memset( light, 0, sizeof( *light ) );
+	light = safe_malloc0( sizeof( *light ) );
 
 	/* attach it */
 	ThreadLock();
@@ -579,8 +578,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw
 		/* optionally create a point splashsplash light for first pass */
 		if ( original && si->backsplashFraction > 0 ) {
 			/* allocate a new point light */
-			splash = safe_malloc( sizeof( *splash ) );
-			memset( splash, 0, sizeof( *splash ) );
+			splash = safe_malloc0( sizeof( *splash ) );
 			splash->next = lights;
 			lights = splash;
 
diff --git a/tools/quake3/q3map2/light_ydnar.c b/tools/quake3/q3map2/light_ydnar.c
index c75dac2e..19d1b31a 100644
--- a/tools/quake3/q3map2/light_ydnar.c
+++ b/tools/quake3/q3map2/light_ydnar.c
@@ -162,13 +162,11 @@ void SmoothNormals( void ){
 
 
 	/* allocate shade angle table */
-	shadeAngles = safe_malloc( numBSPDrawVerts * sizeof( float ) );
-	memset( shadeAngles, 0, numBSPDrawVerts * sizeof( float ) );
+	shadeAngles = safe_malloc0( numBSPDrawVerts * sizeof( float ) );
 
 	/* allocate smoothed table */
 	cs = ( numBSPDrawVerts / 8 ) + 1;
-	smoothed = safe_malloc( cs );
-	memset( smoothed, 0, cs );
+	smoothed = safe_malloc0( cs );
 
 	/* set default shade angle */
 	defaultShadeAngle = DEG2RAD( shadeAngleDegrees );
@@ -2392,8 +2390,7 @@ void IlluminateRawLightmap( int rawLightmapNum ){
 			if ( lm->superLuxels[ lightmapNum ] == NULL ) {
 				/* allocate sampling lightmap storage */
 				size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
-				lm->superLuxels[ lightmapNum ] = safe_malloc( size );
-				memset( lm->superLuxels[ lightmapNum ], 0, size );
+				lm->superLuxels[ lightmapNum ] = safe_malloc0( size );
 			}
 
 			/* set style */
diff --git a/tools/quake3/q3map2/lightmaps_ydnar.c b/tools/quake3/q3map2/lightmaps_ydnar.c
index f578daad..f6203f53 100644
--- a/tools/quake3/q3map2/lightmaps_ydnar.c
+++ b/tools/quake3/q3map2/lightmaps_ydnar.c
@@ -67,6 +67,8 @@ void WriteTGA24( char *filename, byte *data, int width, int height, qboolean fli
 
 	/* allocate a buffer and set it up */
 	buffer = safe_malloc( width * height * 3 + 18 );
+	/* we may also use safe_malloc0 on the whole instead,
+	 * this would just be a bit slower */
 	memset( buffer, 0, 18 );
 	buffer[ 2 ] = 2;
 	buffer[ 12 ] = width & 255;
@@ -809,8 +811,7 @@ qboolean AddSurfaceToRawLightmap( int num, rawLightmap_t *lm ){
 	/* for planar surfaces, create lightmap vectors for st->xyz conversion */
 	if ( VectorLength( ds->lightmapVecs[ 2 ] ) || 1 ) {  /* ydnar: can't remember what exactly i was thinking here... */
 		/* allocate space for the vectors */
-		lm->vecs = safe_malloc( 3 * sizeof( vec3_t ) );
-		memset( lm->vecs, 0, 3 * sizeof( vec3_t ) );
+		lm->vecs = safe_malloc0( 3 * sizeof( vec3_t ) );
 		VectorCopy( ds->lightmapVecs[ 2 ], lm->vecs[ 2 ] );
 
 		/* project stepped lightmap blocks and subtract to get planevecs */
@@ -980,18 +981,15 @@ void SetupSurfaceLightmaps( void ){
 	/* allocate a list of surface clusters */
 	numSurfaceClusters = 0;
 	maxSurfaceClusters = numBSPLeafSurfaces;
-	surfaceClusters = safe_malloc( maxSurfaceClusters * sizeof( *surfaceClusters ) );
-	memset( surfaceClusters, 0, maxSurfaceClusters * sizeof( *surfaceClusters ) );
+	surfaceClusters = safe_malloc0( maxSurfaceClusters * sizeof( *surfaceClusters ) );
 
 	/* allocate a list for per-surface info */
-	surfaceInfos = safe_malloc( numBSPDrawSurfaces * sizeof( *surfaceInfos ) );
-	memset( surfaceInfos, 0, numBSPDrawSurfaces * sizeof( *surfaceInfos ) );
+	surfaceInfos = safe_malloc0( numBSPDrawSurfaces * sizeof( *surfaceInfos ) );
 	for ( i = 0; i < numBSPDrawSurfaces; i++ )
 		surfaceInfos[ i ].childSurfaceNum = -1;
 
 	/* allocate a list of surface indexes to be sorted */
-	sortSurfaces = safe_malloc( numBSPDrawSurfaces * sizeof( int ) );
-	memset( sortSurfaces, 0, numBSPDrawSurfaces * sizeof( int ) );
+	sortSurfaces = safe_malloc0( numBSPDrawSurfaces * sizeof( int ) );
 
 	/* walk each model in the bsp */
 	for ( i = 0; i < numBSPModels; i++ )
@@ -1111,14 +1109,12 @@ void SetupSurfaceLightmaps( void ){
 
 	/* allocate a list of surfaces that would go into raw lightmaps */
 	numLightSurfaces = 0;
-	lightSurfaces = safe_malloc( numSurfsLightmapped * sizeof( int ) );
-	memset( lightSurfaces, 0, numSurfsLightmapped * sizeof( int ) );
+	lightSurfaces = safe_malloc0( numSurfsLightmapped * sizeof( int ) );
 
 	/* allocate a list of raw lightmaps */
 	numRawSuperLuxels = 0;
 	numRawLightmaps = 0;
-	rawLightmaps = safe_malloc( numSurfsLightmapped * sizeof( *rawLightmaps ) );
-	memset( rawLightmaps, 0, numSurfsLightmapped * sizeof( *rawLightmaps ) );
+	rawLightmaps = safe_malloc0( numSurfsLightmapped * sizeof( *rawLightmaps ) );
 
 	/* walk the list of sorted surfaces */
 	for ( i = 0; i < numBSPDrawSurfaces; i++ )
@@ -1203,10 +1199,8 @@ void SetupSurfaceLightmaps( void ){
 	/* allocate vertex luxel storage */
 	for ( k = 0; k < MAX_LIGHTMAPS; k++ )
 	{
-		vertexLuxels[ k ] = safe_malloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
-		memset( vertexLuxels[ k ], 0, numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
-		radVertexLuxels[ k ] = safe_malloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
-		memset( radVertexLuxels[ k ], 0, numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
+		vertexLuxels[ k ] = safe_malloc0( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
+		radVertexLuxels[ k ] = safe_malloc0( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
 	}
 
 	/* emit some stats */
@@ -1957,13 +1951,10 @@ static void SetupOutLightmap( rawLightmap_t *lm, outLightmap_t *olm ){
 	olm->numShaders = 0;
 
 	/* allocate buffers */
-	olm->lightBits = safe_malloc( ( olm->customWidth * olm->customHeight / 8 ) + 8 );
-	memset( olm->lightBits, 0, ( olm->customWidth * olm->customHeight / 8 ) + 8 );
-	olm->bspLightBytes = safe_malloc( olm->customWidth * olm->customHeight * 3 );
-	memset( olm->bspLightBytes, 0, olm->customWidth * olm->customHeight * 3 );
+	olm->lightBits = safe_malloc0( ( olm->customWidth * olm->customHeight / 8 ) + 8 );
+	olm->bspLightBytes = safe_malloc0( olm->customWidth * olm->customHeight * 3 );
 	if ( deluxemap ) {
-		olm->bspDirBytes = safe_malloc( olm->customWidth * olm->customHeight * 3 );
-		memset( olm->bspDirBytes, 0, olm->customWidth * olm->customHeight * 3 );
+		olm->bspDirBytes = safe_malloc0( olm->customWidth * olm->customHeight * 3 );
 	}
 }
 
@@ -2526,8 +2517,7 @@ void StoreSurfaceLightmaps( qboolean fastLightmapSearch, qboolean storeForReal )
 			/* allocate bsp luxel storage */
 			if ( lm->bspLuxels[ lightmapNum ] == NULL ) {
 				size = lm->w * lm->h * BSP_LUXEL_SIZE * sizeof( float );
-				lm->bspLuxels[ lightmapNum ] = safe_malloc( size );
-				memset( lm->bspLuxels[ lightmapNum ], 0, size );
+				lm->bspLuxels[ lightmapNum ] = safe_malloc0( size );
 			}
 
 			/* allocate radiosity lightmap storage */
@@ -3126,8 +3116,7 @@ void StoreSurfaceLightmaps( qboolean fastLightmapSearch, qboolean storeForReal )
 		else
 		{
 			numBSPLightBytes = ( numBSPLightmaps * game->lightmapSize * game->lightmapSize * 3 );
-			bspLightBytes = safe_malloc( numBSPLightBytes );
-			memset( bspLightBytes, 0, numBSPLightBytes );
+			bspLightBytes = safe_malloc0( numBSPLightBytes );
 		}
 
 		/* walk the list of output lightmaps */
diff --git a/tools/quake3/q3map2/map.c b/tools/quake3/q3map2/map.c
index 22fc166f..ff6eb98f 100644
--- a/tools/quake3/q3map2/map.c
+++ b/tools/quake3/q3map2/map.c
@@ -1523,8 +1523,7 @@ void LoadEntityIndexMap( entity_t *e ){
 	}
 
 	/* create a new index map */
-	im = safe_malloc( sizeof( *im ) );
-	memset( im, 0, sizeof( *im ) );
+	im = safe_malloc0( sizeof( *im ) );
 
 	/* set it up */
 	im->w = w;
diff --git a/tools/quake3/q3map2/model.c b/tools/quake3/q3map2/model.c
index c8558a3f..effaf17d 100644
--- a/tools/quake3/q3map2/model.c
+++ b/tools/quake3/q3map2/model.c
@@ -441,12 +441,10 @@ void InsertModel( const char *name, int skin, int frame, m4x4_t transform, remap
 
 		/* set particulars */
 		ds->numVerts = PicoGetSurfaceNumVertexes( surface );
-		ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
-		memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
+		ds->verts = safe_malloc0( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
 
 		ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
-		ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
-		memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
+		ds->indexes = safe_malloc0( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
 
 		/* copy vertexes */
 		for ( i = 0; i < ds->numVerts; i++ )
diff --git a/tools/quake3/q3map2/patch.c b/tools/quake3/q3map2/patch.c
index 82ff09a7..95d478f6 100644
--- a/tools/quake3/q3map2/patch.c
+++ b/tools/quake3/q3map2/patch.c
@@ -348,8 +348,7 @@ void ParsePatch( qboolean onlyLights ){
 	}
 
 	/* allocate patch mesh */
-	pm = safe_malloc( sizeof( *pm ) );
-	memset( pm, 0, sizeof( *pm ) );
+	pm = safe_malloc0( sizeof( *pm ) );
 
 	/* ydnar: add entity/brush numbering */
 	pm->entityNum = mapEnt->mapEntityNum;
@@ -445,8 +444,7 @@ void PatchMapDrawSurfs( entity_t *e ){
 	if ( !patchCount ) {
 		return;
 	}
-	bordering = safe_malloc( patchCount * patchCount );
-	memset( bordering, 0, patchCount * patchCount );
+	bordering = safe_malloc0( patchCount * patchCount );
 
 	// build the bordering matrix
 	for ( k = 0 ; k < patchCount ; k++ ) {
diff --git a/tools/quake3/q3map2/portals.c b/tools/quake3/q3map2/portals.c
index 676fa315..27ae8c31 100644
--- a/tools/quake3/q3map2/portals.c
+++ b/tools/quake3/q3map2/portals.c
@@ -62,8 +62,7 @@ portal_t *AllocPortal( void ){
 		c_peak_portals = c_active_portals;
 	}
 
-	p = safe_malloc( sizeof( portal_t ) );
-	memset( p, 0, sizeof( portal_t ) );
+	p = safe_malloc0( sizeof( portal_t ) );
 
 	return p;
 }
diff --git a/tools/quake3/q3map2/shaders.c b/tools/quake3/q3map2/shaders.c
index 97d98fb1..b3697e4e 100644
--- a/tools/quake3/q3map2/shaders.c
+++ b/tools/quake3/q3map2/shaders.c
@@ -1280,8 +1280,7 @@ static void ParseShaderFile( const char *filename ){
 				}
 
 				/* allocate sun */
-				sun = safe_malloc( sizeof( *sun ) );
-				memset( sun, 0, sizeof( *sun ) );
+				sun = safe_malloc0( sizeof( *sun ) );
 
 				/* set style */
 				sun->style = si->lightStyle;
@@ -1381,8 +1380,7 @@ static void ParseShaderFile( const char *filename ){
 					surfaceModel_t  *model;
 
 					/* allocate new model and attach it */
-					model = safe_malloc( sizeof( *model ) );
-					memset( model, 0, sizeof( *model ) );
+					model = safe_malloc0( sizeof( *model ) );
 					model->next = si->surfaceModel;
 					si->surfaceModel = model;
 
@@ -1415,8 +1413,7 @@ static void ParseShaderFile( const char *filename ){
 
 
 					/* allocate new foliage struct and attach it */
-					foliage = safe_malloc( sizeof( *foliage ) );
-					memset( foliage, 0, sizeof( *foliage ) );
+					foliage = safe_malloc0( sizeof( *foliage ) );
 					foliage->next = si->foliage;
 					si->foliage = foliage;
 
@@ -1746,8 +1743,7 @@ static void ParseShaderFile( const char *filename ){
 					alpha = ( !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) ) ? 1 : 0;
 
 					/* allocate new colormod */
-					cm = safe_malloc( sizeof( *cm ) );
-					memset( cm, 0, sizeof( *cm ) );
+					cm = safe_malloc0( sizeof( *cm ) );
 
 					/* attach to shader */
 					if ( si->colorMod == NULL ) {
diff --git a/tools/quake3/q3map2/surface.c b/tools/quake3/q3map2/surface.c
index a51db8c1..4f69a2f1 100644
--- a/tools/quake3/q3map2/surface.c
+++ b/tools/quake3/q3map2/surface.c
@@ -961,8 +961,7 @@ mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, windin
 	ds->sampleSize = b->lightmapSampleSize;
 	ds->lightmapScale = b->lightmapScale;
 	ds->numVerts = w->numpoints;
-	ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
-	memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
+	ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) );
 
 	/* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
 	ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY );
@@ -3032,8 +3031,7 @@ static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si ){
 			VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] );
 			ds->fogNum = -1;
 			ds->numVerts = w->numpoints;
-			ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
-			memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
+			ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) );
 
 			/* walk the winding */
 			for ( i = 0; i < ds->numVerts; i++ )
@@ -3124,11 +3122,9 @@ void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader ){
 	ds->shaderInfo = si;
 	ds->fogNum = -1;
 	ds->numVerts = 8;
-	ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
-	memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
+	ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) );
 	ds->numIndexes = 36;
-	ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
-	memset( ds->indexes, 0, ds->numIndexes * sizeof( *ds->indexes ) );
+	ds->indexes = safe_malloc0( ds->numIndexes * sizeof( *ds->indexes ) );
 
 	/* set verts */
 	VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
diff --git a/tools/quake3/q3map2/surface_foliage.c b/tools/quake3/q3map2/surface_foliage.c
index 042c547a..da8e71f7 100644
--- a/tools/quake3/q3map2/surface_foliage.c
+++ b/tools/quake3/q3map2/surface_foliage.c
@@ -301,8 +301,7 @@ void Foliage( mapDrawSurface_t *src ){
 			ds->fogNum = src->fogNum;
 
 			/* add a drawvert for every instance */
-			verts = safe_malloc( ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
-			memset( verts, 0, ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
+			verts = safe_malloc0( ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
 			memcpy( verts, ds->verts, ds->numVerts * sizeof( *verts ) );
 			free( ds->verts );
 			ds->verts = verts;
diff --git a/tools/quake3/q3map2/surface_meta.c b/tools/quake3/q3map2/surface_meta.c
index 145e1961..2cd6f037 100644
--- a/tools/quake3/q3map2/surface_meta.c
+++ b/tools/quake3/q3map2/surface_meta.c
@@ -656,6 +656,7 @@ void FanFaceSurface( mapDrawSurface_t *ds ){
 
 	/* add a new vertex at the beginning of the surface */
 	verts = safe_malloc( ( ds->numVerts + 1 ) * sizeof( bspDrawVert_t ) );
+	/* beware to only zero the new vertexi at the beginning, nor more! */
 	memset( verts, 0, sizeof( bspDrawVert_t ) );
 	memcpy( &verts[ 1 ], ds->verts, ds->numVerts * sizeof( bspDrawVert_t ) );
 	free( ds->verts );
@@ -1201,13 +1202,11 @@ void SmoothMetaTriangles( void ){
 	Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" );
 
 	/* allocate shade angle table */
-	shadeAngles = safe_malloc( numMetaVerts * sizeof( float ) );
-	memset( shadeAngles, 0, numMetaVerts * sizeof( float ) );
+	shadeAngles = safe_malloc0( numMetaVerts * sizeof( float ) );
 
 	/* allocate smoothed table */
 	cs = ( numMetaVerts / 8 ) + 1;
-	smoothed = safe_malloc( cs );
-	memset( smoothed, 0, cs );
+	smoothed = safe_malloc0( cs );
 
 	/* set default shade angle */
 	defaultShadeAngle = DEG2RAD( npDegrees );
diff --git a/tools/quake3/q3map2/vis.c b/tools/quake3/q3map2/vis.c
index bd33c496..ef05a3dc 100644
--- a/tools/quake3/q3map2/vis.c
+++ b/tools/quake3/q3map2/vis.c
@@ -66,8 +66,7 @@ fixedWinding_t *NewFixedWinding( int points ){
 	}
 
 	size = (int)( (size_t)( (fixedWinding_t *)0 )->points[points] );
-	w = safe_malloc( size );
-	memset( w, 0, size );
+	w = safe_malloc0( size );
 
 	return w;
 }
@@ -936,11 +935,9 @@ void LoadPortals( char *name ){
 	portallongs = portalbytes / sizeof( long );
 
 	// each file portal is split into two memory portals
-	portals = safe_malloc( 2 * numportals * sizeof( vportal_t ) );
-	memset( portals, 0, 2 * numportals * sizeof( vportal_t ) );
+	portals = safe_malloc0( 2 * numportals * sizeof( vportal_t ) );
 
-	leafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
-	memset( leafs, 0, portalclusters * sizeof( leaf_t ) );
+	leafs = safe_malloc0( portalclusters * sizeof( leaf_t ) );
 
 	for ( i = 0; i < portalclusters; i++ )
 		leafs[i].merged = -1;
@@ -1036,11 +1033,9 @@ void LoadPortals( char *name ){
 
 	}
 
-	faces = safe_malloc( 2 * numfaces * sizeof( vportal_t ) );
-	memset( faces, 0, 2 * numfaces * sizeof( vportal_t ) );
+	faces = safe_malloc0( 2 * numfaces * sizeof( vportal_t ) );
 
-	faceleafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
-	memset( faceleafs, 0, portalclusters * sizeof( leaf_t ) );
+	faceleafs = safe_malloc0( portalclusters * sizeof( leaf_t ) );
 
 	for ( i = 0, p = faces; i < numfaces; i++ )
 	{
diff --git a/tools/quake3/q3map2/visflow.c b/tools/quake3/q3map2/visflow.c
index 5a7ffd92..3366e5df 100644
--- a/tools/quake3/q3map2/visflow.c
+++ b/tools/quake3/q3map2/visflow.c
@@ -1364,8 +1364,7 @@ void CreatePassages( int portalnum ){
 			continue;
 		}
 
-		passage = (passage_t *) safe_malloc( sizeof( passage_t ) + portalbytes );
-		memset( passage, 0, sizeof( passage_t ) + portalbytes );
+		passage = (passage_t *) safe_malloc0( sizeof( passage_t ) + portalbytes );
 		numseperators = AddSeperators( portal->winding, target->winding, qfalse, seperators, MAX_SEPERATORS * 2 );
 		numseperators += AddSeperators( target->winding, portal->winding, qtrue, &seperators[numseperators], MAX_SEPERATORS * 2 - numseperators );
 
@@ -1585,14 +1584,11 @@ void BasePortalVis( int portalnum ){
 		return;
 	}
 
-	p->portalfront = safe_malloc( portalbytes );
-	memset( p->portalfront, 0, portalbytes );
+	p->portalfront = safe_malloc0( portalbytes );
 
-	p->portalflood = safe_malloc( portalbytes );
-	memset( p->portalflood, 0, portalbytes );
+	p->portalflood = safe_malloc0( portalbytes );
 
-	p->portalvis = safe_malloc( portalbytes );
-	memset( p->portalvis, 0, portalbytes );
+	p->portalvis = safe_malloc0( portalbytes );
 
 	for ( j = 0, tp = portals ; j < numportals * 2 ; j++, tp++ )
 	{
-- 
2.39.5