replace-str.c
snippets · 4.5 KB · 2010-03-30
Replace-str: replacements for two BSD string functions (modified by Samson).
Download « Back to the snippets list
// * *********************************************************************************** //
// * This is my little modification to 2 functions samson retrieved from i believe BSD * //
// * These functions replace strcat and strcpy functions, and prevent string overflows * //
// * They are exact copies of BSD functions, with just one modification other then its * //
// * name, which is something i added to allow for better tracking of overflows. * //
// * This will now wiznet when an overflow was caught, and log it. This will allow * //
// * you to expand variables when needed. Aswell as stop for future bugs. * //
// * Warning, this system requires 2 of my other snippets released. * //
// * *********************************************************************************** //
// * This requires wiznet_printf, and nlogf, which is just a wrapper for log_string * //
// * My recommendation is to install this snippet, replace your strcpy's strcats and * //
// * be VERY careful when doing so, it will save your mud big-time. * //
// * *********************************************************************************** //
// * Credit to samson for doing all the work on getting these. * //
// * Enjoy :) -- Dazzle of Sandstorm: The Mages Sanctuary * //
First off.
Put these in merc.h (or proto.h if you got it)
If merc.h, put it with the prototypes, if proto.h, put it in string.c
size_t __mudstrlcpy( char * dst, const char * src, size_t siz, const char *dafile, const char *dafunction, int line );
size_t __mudstrlcat( char * dst, const char * src, size_t siz, const char *dafile, const char *dafunction, int line );
#define mudstrlcpy( dst, src, siz) __mudstrlcpy(dst, src, siz, __FILE__, __FUNCTION__, __LINE__)
#define mudstrlcat( dst, src, siz) __mudstrlcat(dst, src, siz, __FILE__, __FUNCTION__, __LINE__)
Now put these in string.c :)
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*
* Renamed so it can play itself system independent.
* Samson 10-12-03
*
* Updated for wiznet notifycation of overflows stopped by Darien
* Feb 18 2005
*/
size_t __mudstrlcpy( char * dst, const char * src, size_t siz, const char *dafile, const char *dafunction, int line )
{
register char * d = dst;
register char * s = (char *)src;
register size_t n = siz;
size_t asslen = strlen(src);
/* Copy as many bytes as will fit */
if( n != 0 && --n != 0 )
{
do
{
if( ( *d++ = *s++ ) == 0 )
break;
}
while( --n != 0 );
}
/* Not enough room in dst, add NUL and traverse rest of src */
if( n == 0 )
{
if( siz != 0 )
*d = '\0'; /* NUL-terminate dst */
while( *s++ )
;
}
if(player_list)
{
if(asslen > siz)
{
wiznet_printf(NULL, NULL, WIZ_MEMORY, 0, IMMORTAL, "mudstrlcpy caught overflow: File: %s, Function: %s, Line %d", dafile, dafunction, line);
nlogf("mudstrlcpy caught overflow: File: %s, Function: %s, Line %d", dafile, dafunction, line);
}
}
return( s - src - 1 ); /* count does not include NUL */
}
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(initial dst) + strlen(src); if retval >= siz,
* truncation occurred.
*
* Renamed so it can play itself system independent.
* Samson 10-12-03
*
* Updated for wiznet notifycation of overflows stopped by Darien
* Feb 18 2005
*/
size_t __mudstrlcat( char * dst, const char * src, size_t siz, const char *dafile, const char *dafunction, int line )
{
register char * d = dst;
register char * s = (char *)src;
register size_t n = siz;
size_t dlen;
size_t asslen = strlen(src);
/* Find the end of dst and adjust bytes left but don't go past end */
while( n-- != 0 && *d != '\0' )
d++;
dlen = d - dst;
n = siz - dlen;
if( n == 0 )
return( dlen + strlen(s) );
while( *s != '\0' )
{
if( n != 1 )
{
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
if(player_list)
{
if(asslen > siz)
{
wiznet_printf(NULL, NULL, WIZ_MEMORY, 0, IMMORTAL, "mudstrlcat caught overflow: File: %s, Function: %s, Line %d", dafile, dafunction, line);
nlogf("mudstrlcat caught overflow: File: %s, Function: %s, Line %d", dafile, dafunction, line);
}
}
return( dlen + ( s - src ) ); /* count does not include NUL */
}