Skip to content

Commit

Permalink
q3map2: add safe string copy functions
Browse files Browse the repository at this point in the history
* Q_strncpyz()
* Q_strncat()
* Q_strcat()

Guard against buffer overruns, always zero terminate the result.
  • Loading branch information
bnoordhuis committed Mar 18, 2012
1 parent 997811d commit c2be26a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tools/quake3/q3map2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions tools/quake3/q3map2/q3map2.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@

#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))


/* -------------------------------------------------------------------------------
Expand Down Expand Up @@ -1446,6 +1448,9 @@ surfaceInfo_t;

/* main.c */
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 ScaleBSPMain( int argc, char **argv );
int ConvertMain( int argc, char **argv );
Expand Down

0 comments on commit c2be26a

Please sign in to comment.