]> git.rm.cloudns.org Git - xonotic/netradiant.git/commitdiff
autoexpand: add an alternative way to autoexpand while zeroing the allocated memory
authorThomas Debesse <dev@illwieckz.net>
Tue, 14 Jan 2020 08:42:37 +0000 (09:42 +0100)
committerThomas Debesse <dev@illwieckz.net>
Thu, 23 Jan 2020 18:21:39 +0000 (19:21 +0100)
use it to allocate shader lump, without that, garbage from uninitialized memory may be written
after the ending \0 of strings

tools/quake3/q3map2/q3map2.h
tools/quake3/q3map2/writebsp.c

index 6b8a77e3e6cae45fa478881cd64e599f2e08890a..63d49a8d168f6f69429a3f68206d721785d4ee6a 100644 (file)
@@ -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 )
 
index 3fbed1cc64dfb25a1f4a705baf50e9e22aa1d53c..e3b35df1ce0fe96ca6b6a83995a1cc757fcf387d 100644 (file)
@@ -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 );