From b0e62198ba5dc31425d9a1aa42ffe897a349deac Mon Sep 17 00:00:00 2001 From: Garux Date: Thu, 21 Jan 2021 18:41:16 +0300 Subject: [PATCH] use safe strings more --- tools/quake3/q3map2/bspfile_abstract.cpp | 2 +- tools/quake3/q3map2/convert_map.cpp | 2 +- tools/quake3/q3map2/q3map2.h | 2 ++ tools/quake3/q3map2/shaders.cpp | 14 ++++---------- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tools/quake3/q3map2/bspfile_abstract.cpp b/tools/quake3/q3map2/bspfile_abstract.cpp index 50b3481f..3c6fec0e 100644 --- a/tools/quake3/q3map2/bspfile_abstract.cpp +++ b/tools/quake3/q3map2/bspfile_abstract.cpp @@ -164,7 +164,7 @@ void SwapBSPFile( void ){ { if ( doingBSP ){ si = ShaderInfoForShader( bspShaders[ i ].shader ); - if ( si->remapShader && si->remapShader[ 0 ] ) { + if ( !strEmptyOrNull( si->remapShader ) ) { strcpy( bspShaders[ i ].shader, si->remapShader ); } } diff --git a/tools/quake3/q3map2/convert_map.cpp b/tools/quake3/q3map2/convert_map.cpp index 96bf23d6..178b70d1 100644 --- a/tools/quake3/q3map2/convert_map.cpp +++ b/tools/quake3/q3map2/convert_map.cpp @@ -835,7 +835,7 @@ for ( i = 0; i < brush->numSides; i++ ) static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin ){ int x, y; bspShader_t *shader; - char *texture; + const char *texture; bspDrawVert_t *dv; vec3_t xyz; diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index b5bc9957..bc434b02 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -116,6 +116,8 @@ #define Q3MAP2_EXPERIMENTAL_SNAP_PLANE_FIX 1 /* general */ +// actual shader name length limit depends on game engine and name use manner (plain texture/custom shader) +// now checking it for strlen() < MAX_QPATH (so it's null terminated), though this check may be not enough/too much, depending on the use case #define MAX_QPATH 64 #define MAX_IMAGES 512 diff --git a/tools/quake3/q3map2/shaders.cpp b/tools/quake3/q3map2/shaders.cpp index ad34e876..0cd7c843 100644 --- a/tools/quake3/q3map2/shaders.cpp +++ b/tools/quake3/q3map2/shaders.cpp @@ -829,7 +829,6 @@ shaderInfo_t *ShaderInfoForShader( const char *shaderName ){ int i; int deprecationDepth; shaderInfo_t *si; - char shader[ MAX_QPATH ]; /* dummy check */ if ( strEmptyOrNull( shaderName ) ) { @@ -838,11 +837,7 @@ shaderInfo_t *ShaderInfoForShader( const char *shaderName ){ } /* strip off extension */ - // actual shader name length limit depends on game engine and name use manner (plain texture/custom shader) - // so this check may be not enough/too much, depending on the use case - if( strcpyQ( shader, shaderName, MAX_QPATH ) >= MAX_QPATH ) - Error( "Shader name too long: %s", shaderName ); - StripExtension( shader ); + auto shader = String64()( PathExtensionless( shaderName ) ); /* search for it */ deprecationDepth = 0; @@ -853,12 +848,11 @@ shaderInfo_t *ShaderInfoForShader( const char *shaderName ){ /* check if shader is deprecated */ if ( deprecationDepth < MAX_SHADER_DEPRECATION_DEPTH && !strEmptyOrNull( si->deprecateShader ) ) { /* override name */ - strcpy( shader, si->deprecateShader ); - StripExtension( shader ); + shader( PathExtensionless( si->deprecateShader ) ); /* increase deprecation depth */ deprecationDepth++; if ( deprecationDepth == MAX_SHADER_DEPRECATION_DEPTH ) { - Sys_Warning( "Max deprecation depth of %i is reached on shader '%s'\n", MAX_SHADER_DEPRECATION_DEPTH, shader ); + Sys_Warning( "Max deprecation depth of %i is reached on shader '%s'\n", MAX_SHADER_DEPRECATION_DEPTH, shader.c_str() ); } /* search again from beginning */ i = -1; @@ -878,7 +872,7 @@ shaderInfo_t *ShaderInfoForShader( const char *shaderName ){ /* allocate a default shader */ si = AllocShaderInfo(); - si->shader << shader; + si->shader = shader; LoadShaderImages( si ); FinishShader( si );