Use only safe string functions
authorSam Lantinga <slouken@libsdl.org>
Sun, 19 Feb 2006 23:46:34 +0000
changeset 1379c0a74f199ecf
parent 1378 dc0e13e7e1ae
child 1380 d94b080ff6ce
Use only safe string functions
configure.in
include/SDL_config.h.in
include/SDL_stdinc.h
src/SDL_error.c
src/audio/SDL_audio.c
src/audio/SDL_audiodev.c
src/audio/alsa/SDL_alsa_audio.c
src/audio/arts/SDL_artsaudio.c
src/audio/esd/SDL_esdaudio.c
src/cdrom/aix/SDL_syscdrom.c
src/cdrom/bsdi/SDL_syscdrom.c
src/cdrom/freebsd/SDL_syscdrom.c
src/cdrom/linux/SDL_syscdrom.c
src/cdrom/openbsd/SDL_syscdrom.c
src/cdrom/os2/SDL_syscdrom.c
src/cdrom/osf/SDL_syscdrom.c
src/cdrom/qnx/SDL_syscdrom.c
src/cdrom/win32/SDL_syscdrom.c
src/joystick/SDL_joystick.c
src/joystick/linux/SDL_sysjoystick.c
src/joystick/macos/SDL_sysjoystick.c
src/joystick/os2/SDL_sysjoystick.c
src/loadso/dlopen/SDL_sysloadso.c
src/loadso/macos/SDL_sysloadso.c
src/loadso/macosx/SDL_sysloadso.c
src/loadso/win32/SDL_sysloadso.c
src/main/win32/SDL_win32_main.c
src/stdlib/SDL_getenv.c
src/stdlib/SDL_string.c
src/video/SDL_video.c
src/video/Xext/Xv/Xv.c
src/video/Xext/extensions/extutil.h
src/video/aalib/SDL_aaevents.c
src/video/ataricommon/SDL_atarigl.c
src/video/cybergfx/SDL_amigaevents.c
src/video/directfb/SDL_DirectFB_events.c
src/video/fbcon/SDL_fbevents.c
src/video/ggi/SDL_ggievents.c
src/video/maccommon/SDL_macevents.c
src/video/macdsp/SDL_dspvideo.c
src/video/nanox/SDL_nxevents.c
src/video/photon/SDL_ph_events.c
src/video/photon/SDL_ph_gl.c
src/video/ps2gs/SDL_gsevents.c
src/video/riscos/SDL_riscosFullScreenVideo.c
src/video/riscos/SDL_riscosevents.c
src/video/riscos/SDL_riscostask.c
src/video/riscos/SDL_wimpvideo.c
src/video/svga/SDL_svgaevents.c
src/video/vgl/SDL_vglevents.c
src/video/wincommon/SDL_sysevents.c
src/video/wincommon/SDL_wingl.c
src/video/windib/SDL_dibevents.c
src/video/windx5/SDL_dx5events.c
src/video/wscons/SDL_wsconsevents.c
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11gl.c
src/video/x11/SDL_x11modes.c
src/video/x11/SDL_x11mouse.c
src/video/x11/SDL_x11video.c
     1.1 --- a/configure.in	Sun Feb 19 23:38:57 2006 +0000
     1.2 +++ b/configure.in	Sun Feb 19 23:46:34 2006 +0000
     1.3 @@ -119,7 +119,7 @@
     1.4      if test x$ac_cv_func_strtod = xyes; then
     1.5          AC_DEFINE(HAVE_STRTOD)
     1.6      fi
     1.7 -    AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strcpy strncpy strcat strncat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol _i64toa _ui64toa strtoll atoi atof strcmp strncmp stricmp strcasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep)
     1.8 +    AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol _i64toa _ui64toa strtoll atoi atof strcmp strncmp stricmp strcasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep)
     1.9  
    1.10      AC_CHECK_LIB(m, pow, [BUILD_LIBS="$BUILD_LIBS -lm"])
    1.11  fi
     2.1 --- a/include/SDL_config.h.in	Sun Feb 19 23:38:57 2006 +0000
     2.2 +++ b/include/SDL_config.h.in	Sun Feb 19 23:46:34 2006 +0000
     2.3 @@ -87,10 +87,8 @@
     2.4  #undef HAVE_MEMMOVE
     2.5  #undef HAVE_MEMCMP
     2.6  #undef HAVE_STRLEN
     2.7 -#undef HAVE_STRCPY
     2.8 -#undef HAVE_STRNCPY
     2.9 -#undef HAVE_STRCAT
    2.10 -#undef HAVE_STRNCAT
    2.11 +#undef HAVE_STRLCPY
    2.12 +#undef HAVE_STRLCAT
    2.13  #undef HAVE_STRDUP
    2.14  #undef HAVE__STRREV
    2.15  #undef HAVE__STRUPR
     3.1 --- a/include/SDL_stdinc.h	Sun Feb 19 23:38:57 2006 +0000
     3.2 +++ b/include/SDL_stdinc.h	Sun Feb 19 23:46:34 2006 +0000
     3.3 @@ -206,6 +206,9 @@
     3.4  #define SDL_abs(X)	((X) < 0 ? -(X) : (X))
     3.5  #endif
     3.6  
     3.7 +#define SDL_min(x, y)	(((x) < (y)) ? (x) : (y))
     3.8 +#define SDL_max(x, y)	(((x) > (y)) ? (x) : (y))
     3.9 +
    3.10  #if HAVE_CTYPE_H
    3.11  #define SDL_isdigit(X)  isdigit(X)
    3.12  #define SDL_isspace(X)  isspace(X)
    3.13 @@ -355,28 +358,16 @@
    3.14  extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string);
    3.15  #endif
    3.16  
    3.17 -#if HAVE_STRCPY
    3.18 -#define SDL_strcpy     strcpy
    3.19 +#if HAVE_STRLCPY
    3.20 +#define SDL_strlcpy     strlcpy
    3.21  #else
    3.22 -extern DECLSPEC char * SDLCALL SDL_strcpy(char *dst, const char *src);
    3.23 +extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen);
    3.24  #endif
    3.25  
    3.26 -#if HAVE_STRNCPY
    3.27 -#define SDL_strncpy     strncpy
    3.28 +#if HAVE_STRLCAT
    3.29 +#define SDL_strlcat    strlcat
    3.30  #else
    3.31 -extern DECLSPEC char * SDLCALL SDL_strncpy(char *dst, const char *src, size_t maxlen);
    3.32 -#endif
    3.33 -
    3.34 -#if HAVE_STRCAT
    3.35 -#define SDL_strcat     strcat
    3.36 -#else
    3.37 -#define SDL_strcat(dst, src)    (SDL_strcpy(dst+SDL_strlen(dst), src), dst)
    3.38 -#endif
    3.39 -
    3.40 -#if HAVE_STRNCAT
    3.41 -#define SDL_strncat    strncat
    3.42 -#else
    3.43 -#define SDL_strncat(dst, src, n) (SDL_strncpy(dst+SDL_strlen(dst), src, n), dst)
    3.44 +extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
    3.45  #endif
    3.46  
    3.47  #if HAVE_STRDUP
     4.1 --- a/src/SDL_error.c	Sun Feb 19 23:38:57 2006 +0000
     4.2 +++ b/src/SDL_error.c	Sun Feb 19 23:46:34 2006 +0000
     4.3 @@ -59,8 +59,7 @@
     4.4  	/* Copy in the key, mark error as valid */
     4.5  	error = SDL_GetErrBuf();
     4.6  	error->error = 1;
     4.7 -	SDL_strncpy((char *)error->key, fmt, sizeof(error->key));
     4.8 -	error->key[sizeof(error->key)-1] = '\0';
     4.9 +	SDL_strlcpy((char *)error->key, fmt, sizeof(error->key));
    4.10  
    4.11  	va_start(ap, fmt);
    4.12  	error->argc = 0;
    4.13 @@ -94,8 +93,7 @@
    4.14  				  char *str = va_arg(ap, char *);
    4.15  				  if (str == NULL)
    4.16  				      str = "(null)";
    4.17 -				  SDL_strncpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
    4.18 -				  error->args[index].buf[ERR_MAX_STRLEN-1] = 0;
    4.19 +				  SDL_strlcpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
    4.20  				  error->argc++;
    4.21  				}
    4.22  				break;
    4.23 @@ -251,8 +249,7 @@
    4.24  	/* Allocate the UNICODE buffer */
    4.25  	errstr16 = (Uint16 *)SDL_malloc(maxlen * (sizeof *errstr16));
    4.26  	if ( ! errstr16 ) {
    4.27 -		SDL_strncpy((char *)errstr, "Out of memory", maxlen);
    4.28 -		errstr[maxlen-1] = '\0';
    4.29 +		SDL_strlcpy((char *)errstr, "Out of memory", maxlen);
    4.30  		return(errstr);
    4.31  	}
    4.32  
     5.1 --- a/src/audio/SDL_audio.c	Sun Feb 19 23:38:57 2006 +0000
     5.2 +++ b/src/audio/SDL_audio.c	Sun Feb 19 23:46:34 2006 +0000
     5.3 @@ -373,8 +373,7 @@
     5.4  char *SDL_AudioDriverName(char *namebuf, int maxlen)
     5.5  {
     5.6  	if ( current_audio != NULL ) {
     5.7 -		SDL_strncpy(namebuf, current_audio->name, maxlen-1);
     5.8 -		namebuf[maxlen-1] = '\0';
     5.9 +		SDL_strlcpy(namebuf, current_audio->name, maxlen);
    5.10  		return(namebuf);
    5.11  	}
    5.12  	return(NULL);
     6.1 --- a/src/audio/SDL_audiodev.c	Sun Feb 19 23:38:57 2006 +0000
     6.2 +++ b/src/audio/SDL_audiodev.c	Sun Feb 19 23:46:34 2006 +0000
     6.3 @@ -89,7 +89,7 @@
     6.4  		audiodev = audiopath;
     6.5  	}
     6.6  	if ( path != NULL ) {
     6.7 -		SDL_strncpy(path, audiodev, maxlen);
     6.8 +		SDL_strlcpy(path, audiodev, maxlen);
     6.9  		path[maxlen-1] = '\0';
    6.10  	}
    6.11  	return(audio_fd);
    6.12 @@ -136,7 +136,7 @@
    6.13  	}
    6.14  	audio_fd = open(audiodev, flags, 0);
    6.15  	if ( path != NULL ) {
    6.16 -		SDL_strncpy(path, audiodev, maxlen);
    6.17 +		SDL_strlcpy(path, audiodev, maxlen);
    6.18  		path[maxlen-1] = '\0';
    6.19  	}
    6.20  	return audio_fd;
    6.21 @@ -166,8 +166,7 @@
    6.22  	    audio_fd = open(audiopath, flags, 0);
    6.23  	    if ( audio_fd > 0 ) {
    6.24  		if ( path != NULL ) {
    6.25 -		    SDL_strncpy( path, audiopath, maxlen );
    6.26 -		    path[maxlen-1] = '\0';
    6.27 +		    SDL_strlcpy( path, audiopath, maxlen );
    6.28  		}
    6.29  	        return audio_fd;
    6.30  	    }
     7.1 --- a/src/audio/alsa/SDL_alsa_audio.c	Sun Feb 19 23:38:57 2006 +0000
     7.2 +++ b/src/audio/alsa/SDL_alsa_audio.c	Sun Feb 19 23:46:34 2006 +0000
     7.3 @@ -124,7 +124,7 @@
     7.4  	if (alsa_handle) {
     7.5  		alsa_loaded = 1;
     7.6  		retval = 0;
     7.7 -		for (i = 0; i < SDL_TABLESIZE(alsa_functions); i++) {
     7.8 +		for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
     7.9  /*			*alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/
    7.10  #if HAVE_DLVSYM
    7.11  			*alsa_functions[i].func = dlvsym(alsa_handle,alsa_functions[i].name,"ALSA_0.9");
     8.1 --- a/src/audio/arts/SDL_artsaudio.c	Sun Feb 19 23:38:57 2006 +0000
     8.2 +++ b/src/audio/arts/SDL_artsaudio.c	Sun Feb 19 23:46:34 2006 +0000
     8.3 @@ -90,7 +90,7 @@
     8.4  	if ( arts_handle ) {
     8.5  		arts_loaded = 1;
     8.6  		retval = 0;
     8.7 -		for ( i=0; i<SDL_TABLESIZE(arts_functions); ++i ) {
     8.8 +		for ( i=0; i<SDL_arraysize(arts_functions); ++i ) {
     8.9  			*arts_functions[i].func = SDL_LoadFunction(arts_handle, arts_functions[i].name);
    8.10  			if ( !*arts_functions[i].func ) {
    8.11  				retval = -1;
     9.1 --- a/src/audio/esd/SDL_esdaudio.c	Sun Feb 19 23:38:57 2006 +0000
     9.2 +++ b/src/audio/esd/SDL_esdaudio.c	Sun Feb 19 23:46:34 2006 +0000
     9.3 @@ -85,7 +85,7 @@
     9.4  	if ( esd_handle ) {
     9.5  		esd_loaded = 1;
     9.6  		retval = 0;
     9.7 -		for ( i=0; i<SDL_TABLESIZE(esd_functions); ++i ) {
     9.8 +		for ( i=0; i<SDL_arraysize(esd_functions); ++i ) {
     9.9  			*esd_functions[i].func = SDL_LoadFunction(esd_handle, esd_functions[i].name);
    9.10  			if ( !*esd_functions[i].func ) {
    9.11  				retval = -1;
    10.1 --- a/src/cdrom/aix/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    10.2 +++ b/src/cdrom/aix/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    10.3 @@ -124,12 +124,11 @@
    10.4  
    10.5  		/* Add this drive to our list */
    10.6  		i = SDL_numcds;
    10.7 -		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    10.8 +		SDL_cdlist[i] = SDL_strdup(drive);
    10.9  		if ( SDL_cdlist[i] == NULL ) {
   10.10  			SDL_OutOfMemory();
   10.11  			return;
   10.12  		}
   10.13 -		SDL_strcpy(SDL_cdlist[i], drive);
   10.14  		SDL_cdmode[i] = stbuf->st_rdev;
   10.15  		++SDL_numcds;
   10.16  #ifdef DEBUG_CDROM
   10.17 @@ -302,9 +301,10 @@
   10.18  	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   10.19  	if ( SDLcdrom != NULL ) {
   10.20  		char *cdpath, *delim;
   10.21 -		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   10.22 +		size_t len = SDL_strlen(SDLcdrom)+1;
   10.23 +		cdpath = SDL_stack_alloc(char, len);
   10.24  		if ( cdpath != NULL ) {
   10.25 -			SDL_strcpy(cdpath, SDLcdrom);
   10.26 +			SDL_strlcpy(cdpath, SDLcdrom, len);
   10.27  			SDLcdrom = cdpath;
   10.28  			do {
   10.29  				delim = SDL_strchr(SDLcdrom, ':');
   10.30 @@ -323,7 +323,7 @@
   10.31  					SDLcdrom = NULL;
   10.32  				}
   10.33  			} while ( SDLcdrom );
   10.34 -			SDL_free(cdpath);
   10.35 +			SDL_stack_free(cdpath);
   10.36  		}
   10.37  
   10.38  		/* If we found our drives, there's nothing left to do */
   10.39 @@ -360,21 +360,23 @@
   10.40      int   fd;
   10.41      char* lastsl;
   10.42      char* cdromname;
   10.43 +    size_t len;
   10.44  
   10.45      /*
   10.46       * We found /dev/cd? drives and that is in our list. But we can
   10.47       * open only the /dev/rcd? versions of those devices for Audio CD.
   10.48       */
   10.49 -    cdromname = (char*)SDL_malloc( SDL_strlen(SDL_cdlist[drive]+2) );
   10.50 -    SDL_strcpy(cdromname,SDL_cdlist[drive]);
   10.51 +    len = SDL_strlen(SDL_cdlist[drive])+2;
   10.52 +    cdromname = (char*)SDL_malloc(len);
   10.53 +    SDL_strlcpy(cdromname,SDL_cdlist[drive],len);
   10.54      lastsl = SDL_strrchr(cdromname,'/');
   10.55      if (lastsl) {
   10.56  	*lastsl = 0;
   10.57 -	strcat(cdromname,"/r");
   10.58 +	SDL_strlcat(cdromname,"/r",len);
   10.59  	lastsl = SDL_strrchr(SDL_cdlist[drive],'/');
   10.60  	if (lastsl) {
   10.61  	    lastsl++;
   10.62 -	    strcat(cdromname,lastsl);
   10.63 +	    SDL_strlcat(cdromname,lastsl,len);
   10.64  	}
   10.65      }
   10.66  
    11.1 --- a/src/cdrom/bsdi/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    11.2 +++ b/src/cdrom/bsdi/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    11.3 @@ -224,12 +224,11 @@
    11.4  
    11.5  		/* Add this drive to our list */
    11.6  		i = SDL_numcds;
    11.7 -		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    11.8 +		SDL_cdlist[i] = SDL_strdup(drive);
    11.9  		if ( SDL_cdlist[i] == NULL ) {
   11.10  			SDL_OutOfMemory();
   11.11  			return;
   11.12  		}
   11.13 -		SDL_strcpy(SDL_cdlist[i], drive);
   11.14  		SDL_cdmode[i] = stbuf->st_rdev;
   11.15  		++SDL_numcds;
   11.16  #ifdef DEBUG_CDROM
   11.17 @@ -265,9 +264,10 @@
   11.18  	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   11.19  	if ( SDLcdrom != NULL ) {
   11.20  		char *cdpath, *delim;
   11.21 -		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   11.22 +		size_t len = SDL_strlen(SDLcdrom)+1;
   11.23 +		cdpath = SDL_stack_alloc(char, len);
   11.24  		if ( cdpath != NULL ) {
   11.25 -			SDL_strcpy(cdpath, SDLcdrom);
   11.26 +			SDL_strlcpy(cdpath, SDLcdrom, len);
   11.27  			SDLcdrom = cdpath;
   11.28  			do {
   11.29  				delim = SDL_strchr(SDLcdrom, ':');
   11.30 @@ -283,7 +283,7 @@
   11.31  					SDLcdrom = NULL;
   11.32  				}
   11.33  			} while ( SDLcdrom );
   11.34 -			SDL_free(cdpath);
   11.35 +			SDL_stack_free(cdpath);
   11.36  		}
   11.37  
   11.38  		/* If we found our drives, there's nothing left to do */
    12.1 --- a/src/cdrom/freebsd/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    12.2 +++ b/src/cdrom/freebsd/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    12.3 @@ -110,12 +110,11 @@
    12.4  
    12.5  		/* Add this drive to our list */
    12.6  		i = SDL_numcds;
    12.7 -		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    12.8 +		SDL_cdlist[i] = SDL_strdup(drive);
    12.9  		if ( SDL_cdlist[i] == NULL ) {
   12.10  			SDL_OutOfMemory();
   12.11  			return;
   12.12  		}
   12.13 -		SDL_strcpy(SDL_cdlist[i], drive);
   12.14  		SDL_cdmode[i] = stbuf->st_rdev;
   12.15  		++SDL_numcds;
   12.16  #ifdef DEBUG_CDROM
   12.17 @@ -152,9 +151,10 @@
   12.18  	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   12.19  	if ( SDLcdrom != NULL ) {
   12.20  		char *cdpath, *delim;
   12.21 -		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   12.22 +		size_t len = SDL_strlen(SDLcdrom)+1;
   12.23 +		cdpath = SDL_stack_alloc(char, len);
   12.24  		if ( cdpath != NULL ) {
   12.25 -			SDL_strcpy(cdpath, SDLcdrom);
   12.26 +			SDL_strlcpy(cdpath, SDLcdrom, len);
   12.27  			SDLcdrom = cdpath;
   12.28  			do {
   12.29  				delim = SDL_strchr(SDLcdrom, ':');
   12.30 @@ -170,7 +170,7 @@
   12.31  					SDLcdrom = NULL;
   12.32  				}
   12.33  			} while ( SDLcdrom );
   12.34 -			SDL_free(cdpath);
   12.35 +			SDL_stack_free(cdpath);
   12.36  		}
   12.37  
   12.38  		/* If we found our drives, there's nothing left to do */
    13.1 --- a/src/cdrom/linux/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    13.2 +++ b/src/cdrom/linux/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    13.3 @@ -167,12 +167,11 @@
    13.4  
    13.5  		/* Add this drive to our list */
    13.6  		i = SDL_numcds;
    13.7 -		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    13.8 +		SDL_cdlist[i] = SDL_strdup(drive);
    13.9  		if ( SDL_cdlist[i] == NULL ) {
   13.10  			SDL_OutOfMemory();
   13.11  			return;
   13.12  		}
   13.13 -		SDL_strcpy(SDL_cdlist[i], drive);
   13.14  		SDL_cdmode[i] = stbuf->st_rdev;
   13.15  		++SDL_numcds;
   13.16  #ifdef DEBUG_CDROM
   13.17 @@ -192,21 +191,25 @@
   13.18  	if ( mntfp != NULL ) {
   13.19  		char *tmp;
   13.20  		char *mnt_type;
   13.21 +		size_t mnt_type_len;
   13.22  		char *mnt_dev;
   13.23 +		size_t mnt_dev_len;
   13.24  
   13.25  		while ( (mntent=getmntent(mntfp)) != NULL ) {
   13.26 -			mnt_type = SDL_malloc(SDL_strlen(mntent->mnt_type) + 1);
   13.27 +			mnt_type_len = SDL_strlen(mntent->mnt_type) + 1;
   13.28 +			mnt_type = SDL_stack_alloc(char, mnt_type_len);
   13.29  			if (mnt_type == NULL)
   13.30  				continue;  /* maybe you'll get lucky next time. */
   13.31  
   13.32 -			mnt_dev = SDL_malloc(SDL_strlen(mntent->mnt_fsname) + 1);
   13.33 +			mnt_dev_len = SDL_strlen(mntent->mnt_fsname) + 1;
   13.34 +			mnt_dev = SDL_stack_alloc(char, mnt_dev_len);
   13.35  			if (mnt_dev == NULL) {
   13.36 -				SDL_free(mnt_type);
   13.37 +				SDL_stack_free(mnt_type);
   13.38  				continue;
   13.39  			}
   13.40  
   13.41 -			SDL_strcpy(mnt_type, mntent->mnt_type);
   13.42 -			SDL_strcpy(mnt_dev, mntent->mnt_fsname);
   13.43 +			SDL_strlcpy(mnt_type, mntent->mnt_type, mnt_type_len);
   13.44 +			SDL_strlcpy(mnt_dev, mntent->mnt_fsname, mnt_dev_len);
   13.45  
   13.46  			/* Handle "supermount" filesystem mounts */
   13.47  			if ( SDL_strcmp(mnt_type, MNTTYPE_SUPER) == 0 ) {
   13.48 @@ -242,8 +245,8 @@
   13.49  					AddDrive(mnt_dev, &stbuf);
   13.50  				}
   13.51  			}
   13.52 -			SDL_free(mnt_dev);
   13.53 -			SDL_free(mnt_type);
   13.54 +			SDL_stack_free(mnt_dev);
   13.55 +			SDL_stack_free(mnt_type);
   13.56  		}
   13.57  		endmntent(mntfp);
   13.58  	}
   13.59 @@ -277,9 +280,10 @@
   13.60  	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   13.61  	if ( SDLcdrom != NULL ) {
   13.62  		char *cdpath, *delim;
   13.63 -		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   13.64 +		size_t len = SDL_strlen(SDLcdrom)+1;
   13.65 +		cdpath = SDL_stack_alloc(char, len);
   13.66  		if ( cdpath != NULL ) {
   13.67 -			SDL_strcpy(cdpath, SDLcdrom);
   13.68 +			SDL_strlcpy(cdpath, SDLcdrom, len);
   13.69  			SDLcdrom = cdpath;
   13.70  			do {
   13.71  				delim = SDL_strchr(SDLcdrom, ':');
   13.72 @@ -298,7 +302,7 @@
   13.73  					SDLcdrom = NULL;
   13.74  				}
   13.75  			} while ( SDLcdrom );
   13.76 -			SDL_free(cdpath);
   13.77 +			SDL_stack_free(cdpath);
   13.78  		}
   13.79  
   13.80  		/* If we found our drives, there's nothing left to do */
    14.1 --- a/src/cdrom/openbsd/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    14.2 +++ b/src/cdrom/openbsd/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    14.3 @@ -115,12 +115,11 @@
    14.4  
    14.5  		/* Add this drive to our list */
    14.6  		i = SDL_numcds;
    14.7 -		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    14.8 +		SDL_cdlist[i] = SDL_strdup(drive);
    14.9  		if ( SDL_cdlist[i] == NULL ) {
   14.10  			SDL_OutOfMemory();
   14.11  			return;
   14.12  		}
   14.13 -		SDL_strcpy(SDL_cdlist[i], drive);
   14.14  		SDL_cdmode[i] = stbuf->st_rdev;
   14.15  		++SDL_numcds;
   14.16  #ifdef DEBUG_CDROM
   14.17 @@ -161,9 +160,10 @@
   14.18  	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   14.19  	if ( SDLcdrom != NULL ) {
   14.20  		char *cdpath, *delim;
   14.21 -		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   14.22 +		size_t len = SDL_strlen(SDLcdrom)+1;
   14.23 +		cdpath = SDL_stack_alloc(char, len);
   14.24  		if ( cdpath != NULL ) {
   14.25 -			SDL_strcpy(cdpath, SDLcdrom);
   14.26 +			SDL_strlcpy(cdpath, SDLcdrom, len);
   14.27  			SDLcdrom = cdpath;
   14.28  			do {
   14.29  				delim = SDL_strchr(SDLcdrom, ':');
   14.30 @@ -179,7 +179,7 @@
   14.31  					SDLcdrom = NULL;
   14.32  				}
   14.33  			} while ( SDLcdrom );
   14.34 -			SDL_free(cdpath);
   14.35 +			SDL_stack_free(cdpath);
   14.36  		}
   14.37  
   14.38  		/* If we found our drives, there's nothing left to do */
    15.1 --- a/src/cdrom/os2/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    15.2 +++ b/src/cdrom/os2/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    15.3 @@ -94,13 +94,12 @@
    15.4  	{
    15.5  	msp.ulNumber = i+1;
    15.6  	mciSendCommand(0,MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_WAIT,&msp, 0);
    15.7 -	SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(SysInfoRet)+1);
    15.8 +	SDL_cdlist[i] = SDL_strdup(SysInfoRet);
    15.9  	if ( SDL_cdlist[i] == NULL )
   15.10  		{
   15.11  		SDL_OutOfMemory();
   15.12  		return(-1);
   15.13  		}
   15.14 -	SDL_strcpy(SDL_cdlist[i], SysInfoRet);
   15.15  	}
   15.16  return(0);
   15.17  }
    16.1 --- a/src/cdrom/osf/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    16.2 +++ b/src/cdrom/osf/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    16.3 @@ -119,13 +119,11 @@
    16.4  
    16.5  	/* Add this drive to our list */
    16.6  	i = SDL_numcds;
    16.7 -	SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    16.8 +	SDL_cdlist[i] = SDL_strdup(drive);
    16.9  	if ( SDL_cdlist[i] == NULL ) {
   16.10  	    SDL_OutOfMemory();
   16.11  	    return;
   16.12  	}
   16.13 -
   16.14 -	SDL_strcpy(SDL_cdlist[i], drive);
   16.15  	SDL_cdmode[i] = stbuf->st_rdev;
   16.16  	++SDL_numcds;
   16.17  #ifdef DEBUG_CDROM
   16.18 @@ -174,9 +172,10 @@
   16.19      SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   16.20      if ( SDLcdrom != NULL ) {
   16.21  	char *cdpath, *delim;
   16.22 -	cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   16.23 +	size_t len = SDL_strlen(SDLcdrom)+1;
   16.24 +	cdpath = SDL_stack_alloc(len);
   16.25  	if ( cdpath != NULL ) {
   16.26 -	    SDL_strcpy(cdpath, SDLcdrom);
   16.27 +	    SDL_strlcpy(cdpath, SDLcdrom, len);
   16.28  	    SDLcdrom = cdpath;
   16.29  	    do {
   16.30  		delim = SDL_strchr(SDLcdrom, ':');
   16.31 @@ -192,7 +191,7 @@
   16.32  		    SDLcdrom = NULL;
   16.33  		}
   16.34  	    } while ( SDLcdrom );
   16.35 -	    SDL_free(cdpath);
   16.36 +	    SDL_stack_free(cdpath);
   16.37  	}
   16.38  
   16.39  	/* If we found our drives, there's nothing left to do */
   16.40 @@ -226,15 +225,6 @@
   16.41  #endif
   16.42  	}
   16.43      }
   16.44 -
   16.45 -/*
   16.46 -    SDLcdrom=SDL_malloc(sizeof(char) * 32);
   16.47 -    SDL_strcpy(SDLcdrom,"/dev/rdisk/cdrom0c");
   16.48 -    SDL_cdlist[0] = SDLcdrom;
   16.49 -    stat(SDLcdrom, &stbuf);
   16.50 -    SDL_cdmode[0] = stbuf.st_rdev;
   16.51 -    SDL_numcds = 1;
   16.52 - */
   16.53      return (0);
   16.54  }
   16.55  
    17.1 --- a/src/cdrom/qnx/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    17.2 +++ b/src/cdrom/qnx/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    17.3 @@ -124,13 +124,12 @@
    17.4          /* Add this drive to our list */
    17.5  
    17.6          i = SDL_numcds;
    17.7 -        SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    17.8 +        SDL_cdlist[i] = SDL_strdup(drive);
    17.9          if (SDL_cdlist[i] == NULL)
   17.10          {
   17.11              SDL_OutOfMemory();
   17.12              return;
   17.13          }
   17.14 -        SDL_strcpy(SDL_cdlist[i], drive);
   17.15          SDL_cdmode[i] = stbuf->st_rdev;
   17.16          ++SDL_numcds;
   17.17      }
   17.18 @@ -169,11 +168,11 @@
   17.19      if ( SDLcdrom != NULL )
   17.20      {
   17.21          char *cdpath, *delim;
   17.22 -
   17.23 -        cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
   17.24 +	size_t len = SDL_strlen(SDLcdrom)+1;
   17.25 +        cdpath = SDL_stack_alloc(len);
   17.26          if (cdpath != NULL)
   17.27          {
   17.28 -            SDL_strcpy(cdpath, SDLcdrom);
   17.29 +            SDL_strlcpy(cdpath, SDLcdrom, len);
   17.30              SDLcdrom = cdpath;
   17.31              do {
   17.32                  delim = SDL_strchr(SDLcdrom, ':');
   17.33 @@ -194,7 +193,7 @@
   17.34                      SDLcdrom = NULL;
   17.35                  }
   17.36              } while (SDLcdrom);
   17.37 -            SDL_free(cdpath);
   17.38 +            SDL_stack_free(cdpath);
   17.39          }
   17.40  
   17.41          /* If we found our drives, there's nothing left to do */
    18.1 --- a/src/cdrom/win32/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
    18.2 +++ b/src/cdrom/win32/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
    18.3 @@ -63,12 +63,11 @@
    18.4  	if ( SDL_numcds < MAX_DRIVES ) {
    18.5  		/* Add this drive to our list */
    18.6  		i = SDL_numcds;
    18.7 -		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
    18.8 +		SDL_cdlist[i] = SDL_strdup(drive);
    18.9  		if ( SDL_cdlist[i] == NULL ) {
   18.10  			SDL_OutOfMemory();
   18.11  			return;
   18.12  		}
   18.13 -		SDL_strcpy(SDL_cdlist[i], drive);
   18.14  		++SDL_numcds;
   18.15  #ifdef CDROM_DEBUG
   18.16    fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
    19.1 --- a/src/joystick/SDL_joystick.c	Sun Feb 19 23:38:57 2006 +0000
    19.2 +++ b/src/joystick/SDL_joystick.c	Sun Feb 19 23:46:34 2006 +0000
    19.3 @@ -554,7 +554,7 @@
    19.4  	switch (state) {
    19.5  		case SDL_QUERY:
    19.6  			state = SDL_IGNORE;
    19.7 -			for ( i=0; i<SDL_TABLESIZE(event_list); ++i ) {
    19.8 +			for ( i=0; i<SDL_arraysize(event_list); ++i ) {
    19.9  				state = SDL_EventState(event_list[i],SDL_QUERY);
   19.10  				if ( state == SDL_ENABLE ) {
   19.11  					break;
   19.12 @@ -562,7 +562,7 @@
   19.13  			}
   19.14  			break;
   19.15  		default:
   19.16 -			for ( i=0; i<SDL_TABLESIZE(event_list); ++i ) {
   19.17 +			for ( i=0; i<SDL_arraysize(event_list); ++i ) {
   19.18  				SDL_EventState(event_list[i], state);
   19.19  			}
   19.20  			break;
    20.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Sun Feb 19 23:38:57 2006 +0000
    20.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Sun Feb 19 23:46:34 2006 +0000
    20.3 @@ -179,17 +179,6 @@
    20.4  #endif
    20.5  };
    20.6  
    20.7 -static char *mystrdup(const char *string)
    20.8 -{
    20.9 -	char *newstring;
   20.10 -
   20.11 -	newstring = (char *)SDL_malloc(SDL_strlen(string)+1);
   20.12 -	if ( newstring ) {
   20.13 -		SDL_strcpy(newstring, string);
   20.14 -	}
   20.15 -	return(newstring);
   20.16 -}
   20.17 -
   20.18  
   20.19  #ifndef NO_LOGICAL_JOYSTICKS
   20.20  
   20.21 @@ -204,7 +193,7 @@
   20.22        name = SDL_SYS_JoystickName(i);
   20.23  
   20.24        if (name) {
   20.25 -         for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) {
   20.26 +         for(j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
   20.27              if (!SDL_strcmp(name, joystick_logicalmap[j].name)) {
   20.28  
   20.29                 prev = i;
   20.30 @@ -303,13 +292,12 @@
   20.31  
   20.32  	/* First see if the user specified a joystick to use */
   20.33  	if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {
   20.34 -		SDL_strncpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
   20.35 -		path[sizeof(path)-1] = '\0';
   20.36 +		SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
   20.37  		if ( stat(path, &sb) == 0 ) {
   20.38  			fd = open(path, O_RDONLY, 0);
   20.39  			if ( fd >= 0 ) {
   20.40  				/* Assume the user knows what they're doing. */
   20.41 -				SDL_joylist[numjoysticks].fname =mystrdup(path);
   20.42 +				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
   20.43  				if ( SDL_joylist[numjoysticks].fname ) {
   20.44  					dev_nums[numjoysticks] = sb.st_rdev;
   20.45  					++numjoysticks;
   20.46 @@ -319,7 +307,7 @@
   20.47  		}
   20.48  	}
   20.49  
   20.50 -	for ( i=0; i<SDL_TABLESIZE(joydev_pattern); ++i ) {
   20.51 +	for ( i=0; i<SDL_arraysize(joydev_pattern); ++i ) {
   20.52  		for ( j=0; j < MAX_JOYSTICKS; ++j ) {
   20.53  			SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);
   20.54  
   20.55 @@ -357,7 +345,7 @@
   20.56  				close(fd);
   20.57  
   20.58  				/* We're fine, add this joystick */
   20.59 -				SDL_joylist[numjoysticks].fname =mystrdup(path);
   20.60 +				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
   20.61  				if ( SDL_joylist[numjoysticks].fname ) {
   20.62  					dev_nums[numjoysticks] = sb.st_rdev;
   20.63  					++numjoysticks;
   20.64 @@ -489,7 +477,7 @@
   20.65  	}
   20.66  
   20.67  	/* Special joystick support */
   20.68 -	for ( i=0; i < SDL_TABLESIZE(special_joysticks); ++i ) {
   20.69 +	for ( i=0; i < SDL_arraysize(special_joysticks); ++i ) {
   20.70  		if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {
   20.71  
   20.72  			joystick->naxes = special_joysticks[i].naxes;
   20.73 @@ -503,7 +491,7 @@
   20.74  
   20.75  	/* User environment joystick support */
   20.76  	if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {
   20.77 -		SDL_strcpy(env_name, "");
   20.78 +		*env_name = '\0';
   20.79  		if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )
   20.80  			env += SDL_strlen(env_name)+2;
   20.81  		else if ( SDL_sscanf(env, "%s", env_name) == 1 )
    21.1 --- a/src/joystick/macos/SDL_sysjoystick.c	Sun Feb 19 23:38:57 2006 +0000
    21.2 +++ b/src/joystick/macos/SDL_sysjoystick.c	Sun Feb 19 23:46:34 2006 +0000
    21.3 @@ -165,7 +165,7 @@
    21.4  		return(-1);
    21.5      }
    21.6      SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
    21.7 -    SDL_strcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index));
    21.8 +    SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index), SDL_arraysize(joystick->hwdata->name));
    21.9      joystick->name = joystick->hwdata->name;
   21.10  
   21.11      ISpElementList_ExtractByKind(
    22.1 --- a/src/joystick/os2/SDL_sysjoystick.c	Sun Feb 19 23:38:57 2006 +0000
    22.2 +++ b/src/joystick/os2/SDL_sysjoystick.c	Sun Feb 19 23:46:34 2006 +0000
    22.3 @@ -214,7 +214,7 @@
    22.4  		if (joycfg.buttons>=7) SYS_JoyData[0].buttoncalc[2]=((axis[2]->upper+axis[3]->centre)>>1);
    22.5  		if (joycfg.buttons>=8) SYS_JoyData[0].buttoncalc[3]=((axis[3]->upper+axis[3]->centre)>>1);
    22.6  		/* Intialize Joystick Name */
    22.7 -		SDL_strcpy (SYS_JoyData[0].szDeviceName,joycfg.name);
    22.8 +		SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName));
    22.9  		}
   22.10  	/* Default Init ... autoconfig */
   22.11  	else
    23.1 --- a/src/loadso/dlopen/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
    23.2 +++ b/src/loadso/dlopen/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
    23.3 @@ -42,9 +42,10 @@
    23.4  {
    23.5  	void *symbol = dlsym(handle, name);
    23.6  	if ( symbol == NULL ) {
    23.7 -		char *_name = SDL_stack_alloc(char, 1+SDL_strlen(name)+1);
    23.8 +		size_t len = 1+SDL_strlen(name)+1;
    23.9 +		char *_name = SDL_stack_alloc(char, len);
   23.10  		_name[0] = '_';
   23.11 -		SDL_strcpy(&_name[1], name);
   23.12 +		SDL_strlcpy(&_name[1], name, len);
   23.13  		symbol = dlsym(handle, name);
   23.14  		SDL_stack_free(_name);
   23.15  		if ( symbol == NULL ) {
    24.1 --- a/src/loadso/macos/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
    24.2 +++ b/src/loadso/macos/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
    24.3 @@ -42,8 +42,7 @@
    24.4  	OSErr error;
    24.5  	char psofile[512];
    24.6  
    24.7 -	SDL_strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
    24.8 -	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
    24.9 +	SDL_strlcpy(psofile, sofile, SDL_arraysize(psofile));
   24.10  	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
   24.11  			kLoadCFrag, &library_id, &mainAddr, errName);
   24.12  	switch (error) {
   24.13 @@ -80,8 +79,7 @@
   24.14  	CFragConnectionID library_id = (CFragConnectionID)handle;
   24.15  	char pname[512];
   24.16  
   24.17 -	SDL_strncpy(pname, name, SDL_TABLESIZE(pname));
   24.18 -	pname[SDL_TABLESIZE(pname)-1] = '\0';
   24.19 +	SDL_strlcpy(pname, name, SDL_arraysize(pname));
   24.20  	if ( FindSymbol(library_id, C2PStr(pname),
   24.21  	                (char **)&symbol, &class) != noErr ) {
   24.22  		loaderror = "Symbol not found";
    25.1 --- a/src/loadso/macosx/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
    25.2 +++ b/src/loadso/macosx/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
    25.3 @@ -281,7 +281,7 @@
    25.4  	va_start(arg, str);
    25.5  	tss = pthread_getspecific(dlerror_key);
    25.6  	err_str = tss->errstr;
    25.7 -	SDL_strncpy(err_str, "dlcompat: ", ERR_STR_LEN);
    25.8 +	SDL_strlcpy(err_str, "dlcompat: ", ERR_STR_LEN);
    25.9  	vsnprintf(err_str + 10, ERR_STR_LEN - 10, str, arg);
   25.10  	va_end(arg);
   25.11  	debug("ERROR: %s\n", err_str);
   25.12 @@ -620,18 +620,17 @@
   25.13  }
   25.14  
   25.15  /* Up to the caller to SDL_free() returned string */
   25.16 -static inline const char *dyld_error_str()
   25.17 +static inline char *dyld_error_str()
   25.18  {
   25.19  	NSLinkEditErrors dylder;
   25.20  	int dylderno;
   25.21  	const char *dylderrstr;
   25.22  	const char *dyldfile;
   25.23 -	const char* retStr = NULL;
   25.24 +	char* retStr = NULL;
   25.25  	NSLinkEditError(&dylder, &dylderno, &dyldfile, &dylderrstr);
   25.26 -	if (dylderrstr && SDL_strlen(dylderrstr))
   25.27 +	if (dylderrstr && *dylderrstr)
   25.28  	{
   25.29 -		retStr = SDL_malloc(SDL_strlen(dylderrstr) +1);
   25.30 -		SDL_strcpy((char*)retStr,dylderrstr);
   25.31 +		retStr = SDL_strdup(dylderrstr);
   25.32  	}
   25.33  	return retStr;
   25.34  }
   25.35 @@ -645,7 +644,7 @@
   25.36  	void *caller = NULL;
   25.37  #endif
   25.38  	const struct mach_header *caller_mh = 0;
   25.39 -	const char* savedErrorStr = NULL;
   25.40 +	char* savedErrorStr = NULL;
   25.41  	resetdlerror();
   25.42  #ifndef RTLD_SELF
   25.43  #define	RTLD_SELF		((void *) -3)
   25.44 @@ -734,9 +733,9 @@
   25.45  			else
   25.46  			{
   25.47  				if (savedErrorStr)
   25.48 -					SDL_free((char*)savedErrorStr);			
   25.49 +					SDL_free(savedErrorStr);			
   25.50  				savedErrorStr = SDL_malloc(256);
   25.51 -				SDL_snprintf((char*)savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol);	
   25.52 +				SDL_snprintf(savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol);	
   25.53  			}
   25.54  		}
   25.55  	}
   25.56 @@ -746,9 +745,9 @@
   25.57  		if (!savedErrorStr || !SDL_strlen(savedErrorStr))
   25.58  		{
   25.59  			if (savedErrorStr)
   25.60 -				SDL_free((char*)savedErrorStr);
   25.61 +				SDL_free(savedErrorStr);
   25.62  			savedErrorStr = SDL_malloc(256);
   25.63 -			SDL_snprintf((char*)savedErrorStr, 256,"Symbol \"%s\" not found",symbol);
   25.64 +			SDL_snprintf(savedErrorStr, 256,"Symbol \"%s\" not found",symbol);
   25.65  		}
   25.66  		if (canSetError)
   25.67  		{
   25.68 @@ -759,7 +758,7 @@
   25.69  			debug(savedErrorStr);
   25.70  		}
   25.71  		if (savedErrorStr)
   25.72 -			SDL_free((char*)savedErrorStr);
   25.73 +			SDL_free(savedErrorStr);
   25.74  		return NULL;
   25.75  	}
   25.76  	return NSAddressOfSymbol(nssym);
    26.1 --- a/src/loadso/win32/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
    26.2 +++ b/src/loadso/win32/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
    26.3 @@ -47,7 +47,7 @@
    26.4  					FORMAT_MESSAGE_FROM_SYSTEM),
    26.5  				NULL, GetLastError(), 
    26.6  				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    26.7 -				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
    26.8 +				errbuf_t, SDL_arraysize(errbuf), NULL);
    26.9  		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
   26.10  		loaderror = errbuf;
   26.11  	}
   26.12 @@ -66,7 +66,7 @@
   26.13  					FORMAT_MESSAGE_FROM_SYSTEM),
   26.14  				NULL, GetLastError(), 
   26.15  				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   26.16 -				errbuf, SDL_TABLESIZE(errbuf), NULL);
   26.17 +				errbuf, SDL_arraysize(errbuf), NULL);
   26.18  		loaderror = errbuf;
   26.19  	}
   26.20  #endif
   26.21 @@ -97,7 +97,7 @@
   26.22  					FORMAT_MESSAGE_FROM_SYSTEM),
   26.23  				NULL, GetLastError(), 
   26.24  				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   26.25 -				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
   26.26 +				errbuf_t, SDL_arraysize(errbuf), NULL);
   26.27  		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
   26.28  		loaderror = errbuf;
   26.29  	}
   26.30 @@ -114,7 +114,7 @@
   26.31  					FORMAT_MESSAGE_FROM_SYSTEM),
   26.32  				NULL, GetLastError(), 
   26.33  				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   26.34 -				errbuf, SDL_TABLESIZE(errbuf), NULL);
   26.35 +				errbuf, SDL_arraysize(errbuf), NULL);
   26.36  		loaderror = errbuf;
   26.37  	}
   26.38  #endif
    27.1 --- a/src/main/win32/SDL_win32_main.c	Sun Feb 19 23:38:57 2006 +0000
    27.2 +++ b/src/main/win32/SDL_win32_main.c	Sun Feb 19 23:46:34 2006 +0000
    27.3 @@ -18,7 +18,6 @@
    27.4  # define fopen		_wfopen
    27.5  # define freopen	_wfreopen
    27.6  # define remove(x)	DeleteFile(x)
    27.7 -# define strcat		wcscat
    27.8  #else
    27.9  # define DIR_SEPERATOR TEXT("/")
   27.10  # include <direct.h>
   27.11 @@ -208,8 +207,7 @@
   27.12  	if ( bufp == NULL ) {
   27.13  		return OutOfMemory();
   27.14  	}
   27.15 -	SDL_strncpy(bufp, appname, n);
   27.16 -	bufp[n] = '\0';
   27.17 +	SDL_strlcpy(bufp, appname, n);
   27.18  	appname = bufp;
   27.19  
   27.20  	/* Load SDL dynamic link library */
   27.21 @@ -259,10 +257,12 @@
   27.22  	int nLen;
   27.23  #else
   27.24  	char *bufp;
   27.25 +	size_t nLen;
   27.26  #endif
   27.27  #ifndef NO_STDIO_REDIRECT
   27.28  	FILE *newfp;
   27.29  #endif
   27.30 +	int retval;
   27.31  
   27.32  	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   27.33  	   keep them open.  This is a hack.. hopefully it will be fixed 
   27.34 @@ -274,14 +274,14 @@
   27.35  	}
   27.36  
   27.37  #ifndef NO_STDIO_REDIRECT
   27.38 -	pathlen = GetModuleFileName(NULL, path, SDL_TABLESIZE(path));
   27.39 +	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   27.40  	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   27.41  		--pathlen;
   27.42  	}
   27.43  	path[pathlen] = '\0';
   27.44  
   27.45 -	SDL_strcpy( stdoutPath, path );
   27.46 -	SDL_strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
   27.47 +	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   27.48 +	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   27.49      
   27.50  	/* Redirect standard input and standard output */
   27.51  	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   27.52 @@ -299,8 +299,8 @@
   27.53  	}
   27.54  #endif /* _WIN32_WCE */
   27.55  
   27.56 -	SDL_strcpy( stderrPath, path );
   27.57 -	SDL_strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
   27.58 +	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   27.59 +	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   27.60  
   27.61  	newfp = freopen(stderrPath, TEXT("w"), stderr);
   27.62  #ifndef _WIN32_WCE
   27.63 @@ -328,7 +328,7 @@
   27.64  	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   27.65  	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   27.66  	nLen = wcslen(bufp)+1;
   27.67 -	cmdline = (char *)alloca(nLen);
   27.68 +	cmdline = SDL_stack_alloc(wchar_t, nLen);
   27.69  	if ( cmdline == NULL ) {
   27.70  		return OutOfMemory();
   27.71  	}
   27.72 @@ -336,21 +336,26 @@
   27.73  #else
   27.74  	/* Grab the command line (use alloca() on Windows) */
   27.75  	bufp = GetCommandLine();
   27.76 -	cmdline = (char *)alloca(SDL_strlen(bufp)+1);
   27.77 +	nLen = SDL_strlen(bufp)+1;
   27.78 +	cmdline = SDL_stack_alloc(char, nLen);
   27.79  	if ( cmdline == NULL ) {
   27.80  		return OutOfMemory();
   27.81  	}
   27.82 -	SDL_strcpy(cmdline, bufp);
   27.83 +	SDL_strlcpy(cmdline, bufp, nLen);
   27.84  #endif
   27.85  
   27.86  	/* Parse it into argv and argc */
   27.87  	argc = ParseCommandLine(cmdline, NULL);
   27.88 -	argv = (char **)alloca((argc+1)*(sizeof *argv));
   27.89 +	argv = SDL_stack_alloc(char*, argc+1);
   27.90  	if ( argv == NULL ) {
   27.91  		return OutOfMemory();
   27.92  	}
   27.93  	ParseCommandLine(cmdline, argv);
   27.94  
   27.95  	/* Run the main program (after a little SDL initialization) */
   27.96 -	return(console_main(argc, argv));
   27.97 +	retval = console_main(argc, argv);
   27.98 +
   27.99 +	SDL_stack_free(cmdline);
  27.100 +	SDL_stack_free(argv);
  27.101 +	return retval;
  27.102  }
    28.1 --- a/src/stdlib/SDL_getenv.c	Sun Feb 19 23:38:57 2006 +0000
    28.2 +++ b/src/stdlib/SDL_getenv.c	Sun Feb 19 23:46:34 2006 +0000
    28.3 @@ -53,7 +53,7 @@
    28.4  		SDL_envmem = newmem;
    28.5  		SDL_envmemlen = bufferlen;
    28.6  	}
    28.7 -	SDL_strcpy(SDL_envmem, variable);
    28.8 +	SDL_strlcpy(SDL_envmem, variable, bufferlen);
    28.9  	value = SDL_envmem + (sep - variable);
   28.10  	*value++ = '\0';
   28.11  	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
   28.12 @@ -111,11 +111,10 @@
   28.13  	}
   28.14  
   28.15  	/* Allocate memory for the variable */
   28.16 -	new_variable = (char *)SDL_malloc(SDL_strlen(variable)+1);
   28.17 +	new_variable = SDL_strdup(variable);
   28.18  	if ( ! new_variable ) {
   28.19  		return(-1);
   28.20  	}
   28.21 -	SDL_strcpy(new_variable, variable);
   28.22  
   28.23  	/* Actually put it into the environment */
   28.24  	added = 0;
    29.1 --- a/src/stdlib/SDL_string.c	Sun Feb 19 23:38:57 2006 +0000
    29.2 +++ b/src/stdlib/SDL_string.c	Sun Feb 19 23:46:34 2006 +0000
    29.3 @@ -294,39 +294,38 @@
    29.4  }
    29.5  #endif
    29.6  
    29.7 -#ifndef HAVE_STRCPY
    29.8 -char *SDL_strcpy(char *dst, const char *src)
    29.9 +#ifndef HAVE_STRLCPY
   29.10 +size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen)
   29.11  {
   29.12 -    char *dstp = dst;
   29.13 -    while ( *src ) {
   29.14 -        *dstp++ = *src++;
   29.15 +    size_t srclen = SDL_strlen(src);
   29.16 +    if ( maxlen > 0 ) {
   29.17 +        size_t len = SDL_min(srclen, maxlen-1);
   29.18 +        SDL_memcpy(dst, src, len);
   29.19 +        dst[len] = '\0';
   29.20      }
   29.21 -    *dstp = '\0';
   29.22 -
   29.23 -    return dst;
   29.24 +    return srclen;
   29.25  }
   29.26  #endif
   29.27  
   29.28 -#ifndef HAVE_STRNCPY
   29.29 -char *SDL_strncpy(char *dst, const char *src, size_t maxlen)
   29.30 +#ifndef HAVE_STRLCAT
   29.31 +size_t SDL_strlcat(char *dst, const char *src, size_t maxlen)
   29.32  {
   29.33 -    char *dstp = dst;
   29.34 -    while ( maxlen-- && *src ) {
   29.35 -        *dstp++ = *src++;
   29.36 +    size_t dstlen = SDL_strlen(dst);
   29.37 +    size_t srclen = SDL_strlen(src);
   29.38 +    if ( dstlen < maxlen ) {
   29.39 +        SDL_strlcpy(dst+dstlen, src, maxlen-dstlen);
   29.40      }
   29.41 -    *dstp = '\0';
   29.42 -
   29.43 -    return dst;
   29.44 +    return dstlen+srclen;
   29.45  }
   29.46  #endif
   29.47  
   29.48  #ifndef HAVE_STRDUP
   29.49  char *SDL_strdup(const char *string)
   29.50  {
   29.51 -    size_t len = SDL_strlen(string);
   29.52 -    char *newstr = SDL_malloc(len+1);
   29.53 +    size_t len = SDL_strlen(string)+1;
   29.54 +    char *newstr = SDL_malloc(len);
   29.55      if ( newstr ) {
   29.56 -        SDL_strcpy(newstr, string);
   29.57 +        SDL_strlcpy(newstr, string, len);
   29.58      }
   29.59      return newstr;
   29.60  }
   29.61 @@ -912,7 +911,7 @@
   29.62      if ( size > maxlen ) {
   29.63          size = maxlen;
   29.64      }
   29.65 -    SDL_strncpy(text, num, size);
   29.66 +    SDL_strlcpy(text, num, size);
   29.67  
   29.68      return size;
   29.69  }
   29.70 @@ -926,7 +925,7 @@
   29.71      if ( size > maxlen ) {
   29.72          size = maxlen;
   29.73      }
   29.74 -    SDL_strncpy(text, num, size);
   29.75 +    SDL_strlcpy(text, num, size);
   29.76  
   29.77      return size;
   29.78  }
   29.79 @@ -941,7 +940,7 @@
   29.80      if ( size > maxlen ) {
   29.81          size = maxlen;
   29.82      }
   29.83 -    SDL_strncpy(text, num, size);
   29.84 +    SDL_strlcpy(text, num, size);
   29.85  
   29.86      return size;
   29.87  }
   29.88 @@ -955,7 +954,7 @@
   29.89      if ( size > maxlen ) {
   29.90          size = maxlen;
   29.91      }
   29.92 -    SDL_strncpy(text, num, size);
   29.93 +    SDL_strlcpy(text, num, size);
   29.94  
   29.95      return size;
   29.96  }
    30.1 --- a/src/video/SDL_video.c	Sun Feb 19 23:38:57 2006 +0000
    30.2 +++ b/src/video/SDL_video.c	Sun Feb 19 23:46:34 2006 +0000
    30.3 @@ -283,8 +283,7 @@
    30.4  char *SDL_VideoDriverName(char *namebuf, int maxlen)
    30.5  {
    30.6  	if ( current_video != NULL ) {
    30.7 -		SDL_strncpy(namebuf, current_video->name, maxlen-1);
    30.8 -		namebuf[maxlen-1] = '\0';
    30.9 +		SDL_strlcpy(namebuf, current_video->name, maxlen);
   30.10  		return(namebuf);
   30.11  	}
   30.12  	return(NULL);
   30.13 @@ -1664,19 +1663,13 @@
   30.14  			if ( video->wm_title ) {
   30.15  				SDL_free(video->wm_title);
   30.16  			}
   30.17 -			video->wm_title = (char *)SDL_malloc(SDL_strlen(title)+1);
   30.18 -			if ( video->wm_title != NULL ) {
   30.19 -				SDL_strcpy(video->wm_title, title);
   30.20 -			}
   30.21 +			video->wm_title = SDL_strdup(title);
   30.22  		}
   30.23  		if ( icon ) {
   30.24  			if ( video->wm_icon ) {
   30.25  				SDL_free(video->wm_icon);
   30.26  			}
   30.27 -			video->wm_icon = (char *)SDL_malloc(SDL_strlen(icon)+1);
   30.28 -			if ( video->wm_icon != NULL ) {
   30.29 -				SDL_strcpy(video->wm_icon, icon);
   30.30 -			}
   30.31 +			video->wm_icon = SDL_strdup(icon);
   30.32  		}
   30.33  		if ( (title || icon) && (video->SetCaption != NULL) ) {
   30.34  			video->SetCaption(this, video->wm_title,video->wm_icon);
    31.1 --- a/src/video/Xext/Xv/Xv.c	Sun Feb 19 23:38:57 2006 +0000
    31.2 +++ b/src/video/Xext/Xv/Xv.c	Sun Feb 19 23:46:34 2006 +0000
    31.3 @@ -234,8 +234,7 @@
    31.4            SyncHandle();
    31.5  	  return(XvBadAlloc);
    31.6  	}
    31.7 -      (void)strncpy(name, u.string, size);
    31.8 -      name[size] = '\0';
    31.9 +      SDL_strlcpy(name, u.string, size);
   31.10        pa->name = name;
   31.11  
   31.12        u.buffer += (size + 3) & ~3;
   31.13 @@ -386,8 +385,7 @@
   31.14            SyncHandle();
   31.15  	  return(XvBadAlloc);
   31.16        }
   31.17 -      strncpy(name, u.string, size);
   31.18 -      name[size] = '\0';
   31.19 +      SDL_strlcpy(name, u.string, size);
   31.20        pe->name = name;
   31.21        pe++;
   31.22  
    32.1 --- a/src/video/Xext/extensions/extutil.h	Sun Feb 19 23:38:57 2006 +0000
    32.2 +++ b/src/video/Xext/extensions/extutil.h	Sun Feb 19 23:46:34 2006 +0000
    32.3 @@ -36,6 +36,8 @@
    32.4  #ifndef _EXTUTIL_H_
    32.5  #define _EXTUTIL_H_
    32.6  
    32.7 +#include "SDL_stdinc.h"		/* For portable string functions */
    32.8 +
    32.9  #include <X11/extensions/Xext.h>
   32.10  
   32.11  /*
   32.12 @@ -212,7 +214,7 @@
   32.13      code -= codes->first_error;  \
   32.14      if (code >= 0 && code < nerr) { \
   32.15  	char tmp[256]; \
   32.16 -	sprintf (tmp, "%s.%d", extname, code); \
   32.17 +	SDL_snprintf (tmp, SDL_arraysize(tmp), "%s.%d", extname, code); \
   32.18  	pXGetErrorDatabaseText (dpy, "XProtoError", tmp, errl[code], buf, n); \
   32.19  	return buf; \
   32.20      } \
    33.1 --- a/src/video/aalib/SDL_aaevents.c	Sun Feb 19 23:38:57 2006 +0000
    33.2 +++ b/src/video/aalib/SDL_aaevents.c	Sun Feb 19 23:46:34 2006 +0000
    33.3 @@ -122,7 +122,7 @@
    33.4  	const char *std;
    33.5  
    33.6  	/* Initialize the AAlib key translation table */
    33.7 -	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
    33.8 +	for ( i=0; i<SDL_arraysize(keymap); ++i )
    33.9  		keymap[i] = SDLK_UNKNOWN;
   33.10  
   33.11  	keymap[AA_ESC] = SDLK_ESCAPE;
    34.1 --- a/src/video/ataricommon/SDL_atarigl.c	Sun Feb 19 23:38:57 2006 +0000
    34.2 +++ b/src/video/ataricommon/SDL_atarigl.c	Sun Feb 19 23:46:34 2006 +0000
    34.3 @@ -219,10 +219,10 @@
    34.4  
    34.5  	this->gl_config.dll_handle = handle;
    34.6  	if ( path ) {
    34.7 -		SDL_strncpy(this->gl_config.driver_path, path,
    34.8 -			sizeof(this->gl_config.driver_path)-1);
    34.9 +		SDL_strlcpy(this->gl_config.driver_path, path,
   34.10 +			SDL_arraysize(this->gl_config.driver_path));
   34.11  	} else {
   34.12 -		SDL_strcpy(this->gl_config.driver_path, "");
   34.13 +		*this->gl_config.driver_path = '\0';
   34.14  	}
   34.15  
   34.16  #endif
    35.1 --- a/src/video/cybergfx/SDL_amigaevents.c	Sun Feb 19 23:38:57 2006 +0000
    35.2 +++ b/src/video/cybergfx/SDL_amigaevents.c	Sun Feb 19 23:46:34 2006 +0000
    35.3 @@ -301,7 +301,7 @@
    35.4  	int i;
    35.5  
    35.6  	/* Map the miscellaneous keys */
    35.7 -	for ( i=0; i<SDL_TABLESIZE(MISC_keymap); ++i )
    35.8 +	for ( i=0; i<SDL_arraysize(MISC_keymap); ++i )
    35.9  		MISC_keymap[i] = SDLK_UNKNOWN;
   35.10  
   35.11  	/* These X keysyms have 0xFF as the high byte */
    36.1 --- a/src/video/directfb/SDL_DirectFB_events.c	Sun Feb 19 23:38:57 2006 +0000
    36.2 +++ b/src/video/directfb/SDL_DirectFB_events.c	Sun Feb 19 23:46:34 2006 +0000
    36.3 @@ -90,7 +90,7 @@
    36.4    int i;
    36.5  	
    36.6    /* Initialize the DirectFB key translation table */
    36.7 -  for (i=0; i<SDL_TABLESIZE(keymap); ++i)
    36.8 +  for (i=0; i<SDL_arraysize(keymap); ++i)
    36.9      keymap[i] = SDLK_UNKNOWN;
   36.10  
   36.11    keymap[DIKI_A - DIKI_UNKNOWN] = SDLK_a;
    37.1 --- a/src/video/fbcon/SDL_fbevents.c	Sun Feb 19 23:38:57 2006 +0000
    37.2 +++ b/src/video/fbcon/SDL_fbevents.c	Sun Feb 19 23:46:34 2006 +0000
    37.3 @@ -994,7 +994,7 @@
    37.4  	/* Initialize the Linux key translation table */
    37.5  
    37.6  	/* First get the ascii keys and others not well handled */
    37.7 -	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
    37.8 +	for (i=0; i<SDL_arraysize(keymap); ++i) {
    37.9  	  switch(i) {
   37.10  	  /* These aren't handled by the x86 kernel keymapping (?) */
   37.11  	  case SCANCODE_PRINTSCREEN:
   37.12 @@ -1033,7 +1033,7 @@
   37.13  	    break;
   37.14            }
   37.15  	}
   37.16 -	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
   37.17 +	for (i=0; i<SDL_arraysize(keymap); ++i) {
   37.18  	  switch(keymap_temp[i]) {
   37.19  	    case K_F1:  keymap[i] = SDLK_F1;  break;
   37.20  	    case K_F2:  keymap[i] = SDLK_F2;  break;
    38.1 --- a/src/video/ggi/SDL_ggievents.c	Sun Feb 19 23:38:57 2006 +0000
    38.2 +++ b/src/video/ggi/SDL_ggievents.c	Sun Feb 19 23:46:34 2006 +0000
    38.3 @@ -123,7 +123,7 @@
    38.4  	int i;
    38.5  	
    38.6  	/* Initialize the GGI key translation table */
    38.7 -	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
    38.8 +	for ( i=0; i<SDL_arraysize(keymap); ++i )
    38.9  		keymap[i] = SDLK_UNKNOWN;
   38.10  	
   38.11  	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
    39.1 --- a/src/video/maccommon/SDL_macevents.c	Sun Feb 19 23:38:57 2006 +0000
    39.2 +++ b/src/video/maccommon/SDL_macevents.c	Sun Feb 19 23:46:34 2006 +0000
    39.3 @@ -457,7 +457,7 @@
    39.4  	int world = SDLK_WORLD_0;
    39.5  
    39.6  	/* Map the MAC keysyms */
    39.7 -	for ( i=0; i<SDL_TABLESIZE(MAC_keymap); ++i )
    39.8 +	for ( i=0; i<SDL_arraysize(MAC_keymap); ++i )
    39.9  		MAC_keymap[i] = SDLK_UNKNOWN;
   39.10  
   39.11  	/* Defined MAC_* constants */
    40.1 --- a/src/video/macdsp/SDL_dspvideo.c	Sun Feb 19 23:38:57 2006 +0000
    40.2 +++ b/src/video/macdsp/SDL_dspvideo.c	Sun Feb 19 23:46:34 2006 +0000
    40.3 @@ -355,7 +355,7 @@
    40.4  	if ( DSpContext_GetAttributes (context, &attributes) != noErr )
    40.5  		return NULL;
    40.6  			
    40.7 -	for ( i = 0; i < SDL_TABLESIZE(temp_list); i++ ) {
    40.8 +	for ( i = 0; i < SDL_arraysize(temp_list); i++ ) {
    40.9  		width  = attributes.displayWidth;
   40.10  		height = attributes.displayHeight;
   40.11  		
    41.1 --- a/src/video/nanox/SDL_nxevents.c	Sun Feb 19 23:38:57 2006 +0000
    41.2 +++ b/src/video/nanox/SDL_nxevents.c	Sun Feb 19 23:46:34 2006 +0000
    41.3 @@ -41,7 +41,7 @@
    41.4      Dprintf ("enter NX_InitOSKeymap\n") ;
    41.5  
    41.6      // Map the nanox scancodes to SDL keysyms
    41.7 -    for (i = 0; i < SDL_TABLESIZE (NX_NONASCII_keymap); ++ i)
    41.8 +    for (i = 0; i < SDL_arraysize (NX_NONASCII_keymap); ++ i)
    41.9          NX_NONASCII_keymap [i] = SDLK_UNKNOWN ;
   41.10  
   41.11      NX_NONASCII_keymap [MWKEY_LEFT        & 0xFF] = SDLK_LEFT ;
    42.1 --- a/src/video/photon/SDL_ph_events.c	Sun Feb 19 23:38:57 2006 +0000
    42.2 +++ b/src/video/photon/SDL_ph_events.c	Sun Feb 19 23:46:34 2006 +0000
    42.3 @@ -442,13 +442,13 @@
    42.4      int i;
    42.5  
    42.6      /* Odd keys used in international keyboards */
    42.7 -    for (i=0; i<SDL_TABLESIZE(ODD_keymap); ++i)
    42.8 +    for (i=0; i<SDL_arraysize(ODD_keymap); ++i)
    42.9      {
   42.10          ODD_keymap[i] = SDLK_UNKNOWN;
   42.11      }
   42.12  
   42.13      /* Map the miscellaneous keys */
   42.14 -    for (i=0; i<SDL_TABLESIZE(MISC_keymap); ++i)
   42.15 +    for (i=0; i<SDL_arraysize(MISC_keymap); ++i)
   42.16      {
   42.17          MISC_keymap[i] = SDLK_UNKNOWN;
   42.18      }
    43.1 --- a/src/video/photon/SDL_ph_gl.c	Sun Feb 19 23:38:57 2006 +0000
    43.2 +++ b/src/video/photon/SDL_ph_gl.c	Sun Feb 19 23:46:34 2006 +0000
    43.3 @@ -123,7 +123,7 @@
    43.4      this->gl_config.dll_handle = handle;
    43.5      this->gl_config.driver_loaded = 1;
    43.6  
    43.7 -    SDL_strncpy(this->gl_config.driver_path, path, sizeof(this->gl_config.driver_path)-1);
    43.8 +    SDL_strlcpy(this->gl_config.driver_path, path, SDL_arraysize(this->gl_config.driver_path));
    43.9  
   43.10      return 0;
   43.11  }
    44.1 --- a/src/video/ps2gs/SDL_gsevents.c	Sun Feb 19 23:38:57 2006 +0000
    44.2 +++ b/src/video/ps2gs/SDL_gsevents.c	Sun Feb 19 23:46:34 2006 +0000
    44.3 @@ -822,7 +822,7 @@
    44.4  	/* Initialize the Linux key translation table */
    44.5  
    44.6  	/* First get the ascii keys and others not well handled */
    44.7 -	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
    44.8 +	for (i=0; i<SDL_arraysize(keymap); ++i) {
    44.9  	  switch(i) {
   44.10  	  /* These aren't handled by the x86 kernel keymapping (?) */
   44.11  	  case SCANCODE_PRINTSCREEN:
   44.12 @@ -861,7 +861,7 @@
   44.13  	    break;
   44.14            }
   44.15  	}
   44.16 -	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
   44.17 +	for (i=0; i<SDL_arraysize(keymap); ++i) {
   44.18  	  switch(keymap_temp[i]) {
   44.19  	    case K_F1:  keymap[i] = SDLK_F1;  break;
   44.20  	    case K_F2:  keymap[i] = SDLK_F2;  break;
    45.1 --- a/src/video/riscos/SDL_riscosFullScreenVideo.c	Sun Feb 19 23:38:57 2006 +0000
    45.2 +++ b/src/video/riscos/SDL_riscosFullScreenVideo.c	Sun Feb 19 23:46:34 2006 +0000
    45.3 @@ -657,8 +657,7 @@
    45.4  /** Store caption in case this is called before we create a window */
    45.5  void FULLSCREEN_SetWMCaption(_THIS, const char *title, const char *icon)
    45.6  {
    45.7 -	SDL_strncpy(this->hidden->title, title, 255);
    45.8 -	this->hidden->title[255] = 0;
    45.9 +	SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
   45.10  }
   45.11  
   45.12  /* Set screen mode
    46.1 --- a/src/video/riscos/SDL_riscosevents.c	Sun Feb 19 23:38:57 2006 +0000
    46.2 +++ b/src/video/riscos/SDL_riscosevents.c	Sun Feb 19 23:46:34 2006 +0000
    46.3 @@ -101,7 +101,7 @@
    46.4  	int i;
    46.5  
    46.6  	/* Map the VK keysyms */
    46.7 -	for ( i=0; i<SDL_TABLESIZE(RO_keymap); ++i )
    46.8 +	for ( i=0; i<SDL_arraysize(RO_keymap); ++i )
    46.9  		RO_keymap[i] = SDLK_UNKNOWN;
   46.10  
   46.11    RO_keymap[3] = SDLK_LSHIFT;
    47.1 --- a/src/video/riscos/SDL_riscostask.c	Sun Feb 19 23:38:57 2006 +0000
    47.2 +++ b/src/video/riscos/SDL_riscostask.c	Sun Feb 19 23:46:34 2006 +0000
    47.3 @@ -57,7 +57,7 @@
    47.4  
    47.5  /* Local function */
    47.6  
    47.7 -static int RISCOS_GetTaskName(char *task_name);
    47.8 +static int RISCOS_GetTaskName(char *task_name, size_t maxlen);
    47.9  
   47.10  /* Uncomment next line to copy mode changes/restores to stderr */
   47.11  /* #define DUMP_MODE */
   47.12 @@ -91,7 +91,7 @@
   47.13     _kernel_swi_regs regs;
   47.14     int messages[4];
   47.15  
   47.16 -   if (RISCOS_GetTaskName(task_name) == 0) return 0;
   47.17 +   if (RISCOS_GetTaskName(task_name, SDL_arraysize(task_name)) == 0) return 0;
   47.18  
   47.19     messages[0] = 9;       /* Palette changed */
   47.20     messages[1] = 0x400c1; /* Mode changed */
   47.21 @@ -177,7 +177,7 @@
   47.22  
   47.23  ***************************************************************************/
   47.24  
   47.25 -int RISCOS_GetTaskName(char *task_name)
   47.26 +int RISCOS_GetTaskName(char *task_name, size_t maxlen)
   47.27  {
   47.28  	_kernel_swi_regs regs;
   47.29  
   47.30 @@ -187,11 +187,12 @@
   47.31     if (_kernel_swi(OS_GetEnv, &regs, &regs) == 0)
   47.32     {
   47.33  	   char *command_line = (char *)regs.r[0];
   47.34 -	   char *buffer = SDL_malloc(SDL_strlen(command_line)+1);
   47.35 +	   size_t len = SDL_strlen(command_line)+1;
   47.36 +	   char *buffer = SDL_stack_alloc(char, len);
   47.37  	   char *env_var;
   47.38  	   char *p;
   47.39  
   47.40 -	   SDL_strcpy(buffer, command_line);
   47.41 +	   SDL_strlcpy(buffer, command_line, len);
   47.42  	   p = SDL_strchr(buffer, ' ');
   47.43  	   if (p) *p = 0;
   47.44  	   p = SDL_strrchr(buffer, '.');
   47.45 @@ -217,45 +218,45 @@
   47.46  	   if (*p)
   47.47  	   {
   47.48  		   /* Read variables that effect the RISC OS SDL engine for this task */
   47.49 -		   env_var = SDL_malloc(SDL_strlen(p) + 18); /* 18 is larger than the biggest variable name */
   47.50 +		   len = SDL_strlen(p) + 18; /* 18 is larger than the biggest variable name */
   47.51 +		   env_var = SDL_stack_alloc(char, len);
   47.52  		   if (env_var)
   47.53  		   {
   47.54  			   char *env_val;
   47.55  
   47.56  			   /* See if a variable of form SDL$<dirname>$TaskName exists */
   47.57  
   47.58 -			   SDL_strcpy(env_var, "SDL$");
   47.59 -			   strcat(env_var, p);
   47.60 -			   strcat(env_var, "$TaskName");
   47.61 +			   SDL_strlcpy(env_var, "SDL$", len);
   47.62 +			   SDL_strlcat(env_var, p, len);
   47.63 +			   SDL_strlcat(env_var, "$TaskName", len);
   47.64  
   47.65  			   env_val = SDL_getenv(env_var);
   47.66 -			   if (env_val) SDL_strncpy(task_name, env_val, 31);
   47.67 +			   if (env_val) SDL_strlcpy(task_name, env_val, maxlen);
   47.68  
   47.69 -			   SDL_strcpy(env_var, "SDL$");
   47.70 -			   strcat(env_var, p);
   47.71 -			   strcat(env_var, "$BackBuffer");
   47.72 +			   SDL_strlcpy(env_var, "SDL$", len);
   47.73 +			   SDL_strlcat(env_var, p, len);
   47.74 +			   SDL_strlcat(env_var, "$BackBuffer", len);
   47.75  
   47.76  			   env_val = SDL_getenv(env_var);
   47.77  			   if (env_val) riscos_backbuffer = atoi(env_val);
   47.78  
   47.79 -			   SDL_strcpy(env_var, "SDL$");
   47.80 -			   strcat(env_var, p);
   47.81 -			   strcat(env_var, "$CloseAction");
   47.82 +			   SDL_strlcpy(env_var, "SDL$", len);
   47.83 +			   SDL_strlcat(env_var, p, len);
   47.84 +			   SDL_strlcat(env_var, "$CloseAction", len);
   47.85  
   47.86  			   env_val = SDL_getenv(env_var);
   47.87  			   if (env_val && SDL_strcmp(env_val,"0") == 0) riscos_closeaction = 0;
   47.88  
   47.89 -			   SDL_free(env_var);
   47.90 +			   SDL_stack_free(env_var);
   47.91  		   }
   47.92  		   
   47.93 -		   if (task_name[0] == 0) SDL_strncpy(task_name, p, 31);
   47.94 -		   task_name[31] = 0;
   47.95 +		   if (!*task_name) SDL_strlcpy(task_name, p, maxlen);
   47.96  	   }
   47.97  
   47.98 -	   SDL_free(buffer);
   47.99 +	   SDL_stack_free(buffer);
  47.100     }
  47.101  
  47.102 -   if (task_name[0] == 0) SDL_strcpy(task_name, "SDL Task");
  47.103 +   if (task_name[0] == 0) SDL_strlcpy(task_name, "SDL Task", maxlen);
  47.104  
  47.105     return 1;
  47.106  }
    48.1 --- a/src/video/riscos/SDL_wimpvideo.c	Sun Feb 19 23:38:57 2006 +0000
    48.2 +++ b/src/video/riscos/SDL_wimpvideo.c	Sun Feb 19 23:46:34 2006 +0000
    48.3 @@ -384,8 +384,7 @@
    48.4  {
    48.5  	_kernel_swi_regs regs;
    48.6  
    48.7 -	SDL_strncpy(this->hidden->title, title, 255);
    48.8 -	this->hidden->title[255] = 0;
    48.9 +	SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
   48.10  
   48.11  	if (RISCOS_GetWimpVersion() < 380)
   48.12  	{
    49.1 --- a/src/video/svga/SDL_svgaevents.c	Sun Feb 19 23:38:57 2006 +0000
    49.2 +++ b/src/video/svga/SDL_svgaevents.c	Sun Feb 19 23:46:34 2006 +0000
    49.3 @@ -210,7 +210,7 @@
    49.4  	int i;
    49.5  
    49.6  	/* Initialize the BeOS key translation table */
    49.7 -	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
    49.8 +	for ( i=0; i<SDL_arraysize(keymap); ++i )
    49.9  		keymap[i] = SDLK_UNKNOWN;
   49.10  
   49.11  	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
    50.1 --- a/src/video/vgl/SDL_vglevents.c	Sun Feb 19 23:38:57 2006 +0000
    50.2 +++ b/src/video/vgl/SDL_vglevents.c	Sun Feb 19 23:46:34 2006 +0000
    50.3 @@ -143,7 +143,7 @@
    50.4  	int i;
    50.5  
    50.6  	/* Initialize the BeOS key translation table */
    50.7 -	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
    50.8 +	for ( i=0; i<SDL_arraysize(keymap); ++i )
    50.9  		keymap[i] = SDLK_UNKNOWN;
   50.10  
   50.11  	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
    51.1 --- a/src/video/wincommon/SDL_sysevents.c	Sun Feb 19 23:38:57 2006 +0000
    51.2 +++ b/src/video/wincommon/SDL_sysevents.c	Sun Feb 19 23:46:34 2006 +0000
    51.3 @@ -702,13 +702,13 @@
    51.4  	if ( name ) {
    51.5  #ifdef _WIN32_WCE
    51.6  		/* WinCE uses the UNICODE version */
    51.7 -		int nLen = SDL_strlen(name)+1;
    51.8 +		size_t nLen = SDL_strlen(name)+1;
    51.9  		SDL_Appname = SDL_malloc(nLen*2);
   51.10  		MultiByteToWideChar(CP_ACP, 0, name, -1, SDL_Appname, nLen);
   51.11  #else
   51.12 -		int nLen = SDL_strlen(name)+1;
   51.13 +		size_t nLen = SDL_strlen(name)+1;
   51.14  		SDL_Appname = SDL_malloc(nLen);
   51.15 -		SDL_strcpy(SDL_Appname, name);
   51.16 +		SDL_strlcpy(SDL_Appname, name, nLen);
   51.17  #endif /* _WIN32_WCE */
   51.18  		SDL_Appstyle = style;
   51.19  		SDL_Instance = hInst ? hInst : SDL_GetModuleHandle();
    52.1 --- a/src/video/wincommon/SDL_wingl.c	Sun Feb 19 23:38:57 2006 +0000
    52.2 +++ b/src/video/wincommon/SDL_wingl.c	Sun Feb 19 23:46:34 2006 +0000
    52.3 @@ -551,7 +551,7 @@
    52.4  	}
    52.5  
    52.6  	this->gl_config.dll_handle = handle;
    52.7 -	SDL_strcpy(this->gl_config.driver_path, path);
    52.8 +	SDL_strlcpy(this->gl_config.driver_path, path, SDL_arraysize(this->gl_config.driver_path));
    52.9  	this->gl_config.driver_loaded = 1;
   52.10  	return 0;
   52.11  }
    53.1 --- a/src/video/windib/SDL_dibevents.c	Sun Feb 19 23:38:57 2006 +0000
    53.2 +++ b/src/video/windib/SDL_dibevents.c	Sun Feb 19 23:46:34 2006 +0000
    53.3 @@ -251,7 +251,7 @@
    53.4  	int i;
    53.5  
    53.6  	/* Map the VK keysyms */
    53.7 -	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
    53.8 +	for ( i=0; i<SDL_arraysize(VK_keymap); ++i )
    53.9  		VK_keymap[i] = SDLK_UNKNOWN;
   53.10  
   53.11  	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
    54.1 --- a/src/video/windx5/SDL_dx5events.c	Sun Feb 19 23:38:57 2006 +0000
    54.2 +++ b/src/video/windx5/SDL_dx5events.c	Sun Feb 19 23:46:34 2006 +0000
    54.3 @@ -703,7 +703,7 @@
    54.4  	int i;
    54.5  
    54.6  	/* Map the DIK scancodes to SDL keysyms */
    54.7 -	for ( i=0; i<SDL_TABLESIZE(DIK_keymap); ++i )
    54.8 +	for ( i=0; i<SDL_arraysize(DIK_keymap); ++i )
    54.9  		DIK_keymap[i] = 0;
   54.10  
   54.11  	/* Defined DIK_* constants */
    55.1 --- a/src/video/wscons/SDL_wsconsevents.c	Sun Feb 19 23:38:57 2006 +0000
    55.2 +++ b/src/video/wscons/SDL_wsconsevents.c	Sun Feb 19 23:46:34 2006 +0000
    55.3 @@ -100,7 +100,7 @@
    55.4    keysym->sym = SDLK_UNKNOWN;
    55.5    keysym->mod = KMOD_NONE;
    55.6  
    55.7 -  if (scancode < SDL_TABLESIZE(keymap))
    55.8 +  if (scancode < SDL_arraysize(keymap))
    55.9      keysym->sym = keymap[scancode];
   55.10  
   55.11    if (keysym->sym == SDLK_UNKNOWN)
   55.12 @@ -141,7 +141,7 @@
   55.13    int i;
   55.14  
   55.15    /* Make sure unknown keys are mapped correctly */
   55.16 -  for (i=0; i < SDL_TABLESIZE(keymap); i++) {
   55.17 +  for (i=0; i < SDL_arraysize(keymap); i++) {
   55.18      keymap[i] = SDLK_UNKNOWN;
   55.19    }
   55.20  
    56.1 --- a/src/video/x11/SDL_x11events.c	Sun Feb 19 23:38:57 2006 +0000
    56.2 +++ b/src/video/x11/SDL_x11events.c	Sun Feb 19 23:46:34 2006 +0000
    56.3 @@ -693,7 +693,7 @@
    56.4  	int i;
    56.5  
    56.6  	/* Odd keys used in international keyboards */
    56.7 -	for ( i=0; i<SDL_TABLESIZE(ODD_keymap); ++i )
    56.8 +	for ( i=0; i<SDL_arraysize(ODD_keymap); ++i )
    56.9  		ODD_keymap[i] = SDLK_UNKNOWN;
   56.10  
   56.11   	/* Some of these might be mappable to an existing SDLK_ code */
   56.12 @@ -729,7 +729,7 @@
   56.13  #endif
   56.14  
   56.15  	/* Map the miscellaneous keys */
   56.16 -	for ( i=0; i<SDL_TABLESIZE(MISC_keymap); ++i )
   56.17 +	for ( i=0; i<SDL_arraysize(MISC_keymap); ++i )
   56.18  		MISC_keymap[i] = SDLK_UNKNOWN;
   56.19  
   56.20  	/* These X keysyms have 0xFF as the high byte */
    57.1 --- a/src/video/x11/SDL_x11gl.c	Sun Feb 19 23:38:57 2006 +0000
    57.2 +++ b/src/video/x11/SDL_x11gl.c	Sun Feb 19 23:46:34 2006 +0000
    57.3 @@ -414,10 +414,10 @@
    57.4  	this->gl_config.dll_handle = handle;
    57.5  	this->gl_config.driver_loaded = 1;
    57.6  	if ( path ) {
    57.7 -		SDL_strncpy(this->gl_config.driver_path, path,
    57.8 -			sizeof(this->gl_config.driver_path)-1);
    57.9 +		SDL_strlcpy(this->gl_config.driver_path, path,
   57.10 +			SDL_arraysize(this->gl_config.driver_path));
   57.11  	} else {
   57.12 -		SDL_strcpy(this->gl_config.driver_path, "");
   57.13 +		*this->gl_config.driver_path = '\0';
   57.14  	}
   57.15  	return 0;
   57.16  }
    58.1 --- a/src/video/x11/SDL_x11modes.c	Sun Feb 19 23:38:57 2006 +0000
    58.2 +++ b/src/video/x11/SDL_x11modes.c	Sun Feb 19 23:46:34 2006 +0000
    58.3 @@ -493,7 +493,7 @@
    58.4          }
    58.5          this->hidden->nvisuals = 0;
    58.6          if ( ! add_visual_byid(this, SDL_getenv("SDL_VIDEO_X11_VISUALID")) ) {
    58.7 -                for ( i=0; i<SDL_TABLESIZE(depth_list); ++i ) {
    58.8 +                for ( i=0; i<SDL_arraysize(depth_list); ++i ) {
    58.9                          if ( depth_list[i] > 8 ) {
   58.10                                  if ( use_directcolor ) {
   58.11                                          add_visual(this, depth_list[i], DirectColor);
    59.1 --- a/src/video/x11/SDL_x11mouse.c	Sun Feb 19 23:38:57 2006 +0000
    59.2 +++ b/src/video/x11/SDL_x11mouse.c	Sun Feb 19 23:46:34 2006 +0000
    59.3 @@ -191,14 +191,16 @@
    59.4  static void SetMouseAccel(_THIS, const char *accel_param)
    59.5  {
    59.6  	int i;
    59.7 +	size_t len;
    59.8  	int accel_value[3];
    59.9  	char *mouse_param, *mouse_param_buf, *pin;
   59.10  
   59.11 -	mouse_param_buf = (char *)SDL_malloc(SDL_strlen(accel_param)+1);
   59.12 +	len = SDL_strlen(accel_param)+1;
   59.13 +	mouse_param_buf = SDL_stack_alloc(char, len);
   59.14  	if ( ! mouse_param_buf ) {
   59.15  		return;
   59.16  	}
   59.17 -	SDL_strcpy(mouse_param_buf, accel_param);
   59.18 +	SDL_strlcpy(mouse_param_buf, accel_param, len);
   59.19  	mouse_param = mouse_param_buf;
   59.20  
   59.21  	for ( i=0; (i < 3) && mouse_param; ++i ) {
    60.1 --- a/src/video/x11/SDL_x11video.c	Sun Feb 19 23:38:57 2006 +0000
    60.2 +++ b/src/video/x11/SDL_x11video.c	Sun Feb 19 23:46:34 2006 +0000
    60.3 @@ -275,7 +275,7 @@
    60.4  	/* First allow environment variable override */
    60.5  	spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
    60.6  	if ( spot ) {
    60.7 -		SDL_strncpy(classname, spot, maxlen);
    60.8 +		SDL_strlcpy(classname, spot, maxlen);
    60.9  		return classname;
   60.10  	}
   60.11  
   60.12 @@ -293,16 +293,16 @@
   60.13  		linkfile[linksize] = '\0';
   60.14  		spot = SDL_strrchr(linkfile, '/');
   60.15  		if ( spot ) {
   60.16 -			SDL_strncpy(classname, spot+1, maxlen);
   60.17 +			SDL_strlcpy(classname, spot+1, maxlen);
   60.18  		} else {
   60.19 -			SDL_strncpy(classname, linkfile, maxlen);
   60.20 +			SDL_strlcpy(classname, linkfile, maxlen);
   60.21  		}
   60.22  		return classname;
   60.23  	}
   60.24  #endif /* linux */
   60.25  
   60.26  	/* Finally use the default we've used forever */
   60.27 -	SDL_strncpy(classname, "SDL_App", maxlen);
   60.28 +	SDL_strlcpy(classname, "SDL_App", maxlen);
   60.29  	return classname;
   60.30  }
   60.31