From: Thomas Debesse Date: Tue, 14 Jan 2020 08:42:37 +0000 (+0100) Subject: autoexpand: add an alternative way to autoexpand while zeroing the allocated memory X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=70a4ae59555b0670ff8c0f2f26fe93164559c40c;p=xonotic%2Fnetradiant.git autoexpand: add an alternative way to autoexpand while zeroing the allocated memory use it to allocate shader lump, without that, garbage from uninitialized memory may be written after the ending \0 of strings --- diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 6b8a77e3..63d49a8d 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -2570,28 +2570,43 @@ Q_EXTERN bspAdvertisement_t bspAds[ MAX_MAP_ADVERTISEMENTS ]; // Used for tex file support, Smokin'Guns globals Q_EXTERN qboolean compile_map; -#define AUTOEXPAND_BY_REALLOC( ptr, reqitem, allocated, def ) \ +#define _AUTOEXPAND_BY_REALLOC( ptr, reqitem, allocated, def, fillWithZeros ) \ do \ { \ + int prevAllocated = allocated; \ if ( reqitem >= allocated ) \ { \ if ( allocated == 0 ) { \ - allocated = def; } \ + allocated = def; \ + } \ while ( reqitem >= allocated && allocated ) \ + { \ allocated *= 2; \ + } \ if ( !allocated || allocated > 2147483647 / (int)sizeof( *ptr ) ) \ { \ Error( #ptr " over 2 GB" ); \ } \ ptr = realloc( ptr, sizeof( *ptr ) * allocated ); \ if ( !ptr ) { \ - Error( #ptr " out of memory" ); } \ + Error( #ptr " out of memory" ); \ + } \ + if ( fillWithZeros ) \ + { \ + memset( ptr + ( sizeof( *ptr ) * prevAllocated ), 0 , sizeof( *ptr ) * ( allocated - prevAllocated ) ); \ + } \ } \ } \ while ( 0 ) +#define AUTOEXPAND_BY_REALLOC( ptr, reqitem, allocated, def ) _AUTOEXPAND_BY_REALLOC( ptr, reqitem, allocated, def, qfalse ) + +#define AUTOEXPAND_BY_REALLOC0( ptr, reqitem, allocated, def ) _AUTOEXPAND_BY_REALLOC( ptr, reqitem, allocated, def, qtrue ) + #define AUTOEXPAND_BY_REALLOC_BSP( suffix, def ) AUTOEXPAND_BY_REALLOC( bsp##suffix, numBSP##suffix, allocatedBSP##suffix, def ) +#define AUTOEXPAND_BY_REALLOC0_BSP( suffix, def ) AUTOEXPAND_BY_REALLOC0( bsp##suffix, numBSP##suffix, allocatedBSP##suffix, def ) + #define Image_LinearFloatFromsRGBFloat( c ) ( ( ( c ) <= 0.04045f ) ? ( c ) * ( 1.0f / 12.92f ) : (float)pow( ( ( c ) + 0.055f ) * ( 1.0f / 1.055f ), 2.4f ) ) #define Image_sRGBFloatFromLinearFloat( c ) ( ( ( c ) < 0.0031308f ) ? ( c ) * 12.92f : 1.055f * (float)pow( ( c ), 1.0f / 2.4f ) - 0.055f ) diff --git a/tools/quake3/q3map2/writebsp.c b/tools/quake3/q3map2/writebsp.c index 3fbed1cc..e3b35df1 100644 --- a/tools/quake3/q3map2/writebsp.c +++ b/tools/quake3/q3map2/writebsp.c @@ -133,7 +133,7 @@ int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags ){ si = ShaderInfoForShader( shader ); /* emit a new shader */ - AUTOEXPAND_BY_REALLOC_BSP( Shaders, 1024 ); + AUTOEXPAND_BY_REALLOC0_BSP( Shaders, 1024 ); numBSPShaders++; strcpy( bspShaders[ i ].shader, shader );