prevent filename overflow in model .skin loader

strip model extension during filename construction
add variadic template operator to StringOutputStream for inline strings creation
This commit is contained in:
Garux 2021-01-20 11:59:16 +03:00
parent cfa502df6e
commit 3b39a5754d
3 changed files with 13 additions and 7 deletions

View File

@ -112,6 +112,14 @@ std::size_t write( const char* buffer, std::size_t length ){
return length;
}
template<typename ... Args>
StringOutputStream& operator()( const Args& ... args ){
clear();
using expander = int[];
(void)expander{ 0, ( (void)( *this << args ), 0 ) ... };
return *this;
}
iterator begin(){
return m_string.begin();
}

View File

@ -224,7 +224,6 @@ void InsertModel( const char *name, int skin, int frame, m4x4_t transform, remap
byte *color;
picoIndex_t *indexes;
skinfile_t *sf, *sf2;
char skinfilename[ MAX_QPATH ];
char *skinfilecontent;
int skinfilesize;
char *skinfileptr, *skinfilenextptr;
@ -246,14 +245,12 @@ void InsertModel( const char *name, int skin, int frame, m4x4_t transform, remap
}
/* load skin file */
snprintf( skinfilename, sizeof( skinfilename ), "%s_%d.skin", name, skin );
skinfilename[sizeof( skinfilename ) - 1] = 0;
skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
auto skinfilename = StringOutputStream(99)( StringRange( name, path_get_filename_base_end( name ) ), '_', skin, ".skin" );
skinfilesize = vfsLoadFile( skinfilename.c_str(), (void**) &skinfilecontent, 0 );
if ( skinfilesize < 0 && skin != 0 ) {
/* fallback to skin 0 if invalid */
snprintf( skinfilename, sizeof( skinfilename ), "%s_0.skin", name );
skinfilename[sizeof( skinfilename ) - 1] = 0;
skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
skinfilename( StringRange( name, path_get_filename_base_end( name ) ), "_0.skin" );
skinfilesize = vfsLoadFile( skinfilename.c_str(), (void**) &skinfilecontent, 0 );
if ( skinfilesize >= 0 ) {
Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );
}

View File

@ -85,6 +85,7 @@
#include "md4.h"
#include "stringfixedsize.h"
#include "stream/stringstream.h"
#include <stddef.h>
#include <stdlib.h>