q3map2: add safe string copy functions

* Q_strncpyz()
* Q_strncat()
* Q_strcat()

Guard against buffer overruns, always zero terminate the result.
This commit is contained in:
Ben Noordhuis 2012-03-18 00:34:31 +01:00
parent 997811d430
commit c2be26a9bd
2 changed files with 37 additions and 0 deletions

View file

@ -46,6 +46,38 @@ vec_t Random( void ){
} }
char *Q_strncpyz( char *dst, const char *src, size_t len ) {
if ( len == 0 ) {
abort();
}
strncpy( dst, src, len );
dst[ len - 1 ] = '\0';
return dst;
}
char *Q_strcat( char *dst, size_t dlen, const char *src ) {
size_t n = strlen( dst );
if ( n > dlen ) {
abort(); /* buffer overflow */
}
return Q_strncpyz( dst + n, src, dlen - n );
}
char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
size_t n = strlen( dst );
if ( n > dlen ) {
abort(); /* buffer overflow */
}
return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
}
/* /*
ExitQ3Map() ExitQ3Map()

View file

@ -83,6 +83,8 @@
#include <stdlib.h> #include <stdlib.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/* ------------------------------------------------------------------------------- /* -------------------------------------------------------------------------------
@ -1446,6 +1448,9 @@ surfaceInfo_t;
/* main.c */ /* main.c */
vec_t Random( void ); vec_t Random( void );
char *Q_strncpyz( char *dst, const char *src, size_t len );
char *Q_strcat( char *dst, size_t dlen, const char *src );
char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen );
int BSPInfo( int count, char **fileNames ); int BSPInfo( int count, char **fileNames );
int ScaleBSPMain( int argc, char **argv ); int ScaleBSPMain( int argc, char **argv );
int ConvertMain( int argc, char **argv ); int ConvertMain( int argc, char **argv );