Use only safe string functions
authorSam Lantinga <slouken@libsdl.org>
Sun, 19 Feb 2006 23:46:34 +0000
changeset 1379 c0a74f199ecf
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
--- a/configure.in	Sun Feb 19 23:38:57 2006 +0000
+++ b/configure.in	Sun Feb 19 23:46:34 2006 +0000
@@ -119,7 +119,7 @@
     if test x$ac_cv_func_strtod = xyes; then
         AC_DEFINE(HAVE_STRTOD)
     fi
-    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)
+    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)
 
     AC_CHECK_LIB(m, pow, [BUILD_LIBS="$BUILD_LIBS -lm"])
 fi
--- a/include/SDL_config.h.in	Sun Feb 19 23:38:57 2006 +0000
+++ b/include/SDL_config.h.in	Sun Feb 19 23:46:34 2006 +0000
@@ -87,10 +87,8 @@
 #undef HAVE_MEMMOVE
 #undef HAVE_MEMCMP
 #undef HAVE_STRLEN
-#undef HAVE_STRCPY
-#undef HAVE_STRNCPY
-#undef HAVE_STRCAT
-#undef HAVE_STRNCAT
+#undef HAVE_STRLCPY
+#undef HAVE_STRLCAT
 #undef HAVE_STRDUP
 #undef HAVE__STRREV
 #undef HAVE__STRUPR
--- a/include/SDL_stdinc.h	Sun Feb 19 23:38:57 2006 +0000
+++ b/include/SDL_stdinc.h	Sun Feb 19 23:46:34 2006 +0000
@@ -206,6 +206,9 @@
 #define SDL_abs(X)	((X) < 0 ? -(X) : (X))
 #endif
 
+#define SDL_min(x, y)	(((x) < (y)) ? (x) : (y))
+#define SDL_max(x, y)	(((x) > (y)) ? (x) : (y))
+
 #if HAVE_CTYPE_H
 #define SDL_isdigit(X)  isdigit(X)
 #define SDL_isspace(X)  isspace(X)
@@ -355,28 +358,16 @@
 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string);
 #endif
 
-#if HAVE_STRCPY
-#define SDL_strcpy     strcpy
+#if HAVE_STRLCPY
+#define SDL_strlcpy     strlcpy
 #else
-extern DECLSPEC char * SDLCALL SDL_strcpy(char *dst, const char *src);
+extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen);
 #endif
 
-#if HAVE_STRNCPY
-#define SDL_strncpy     strncpy
+#if HAVE_STRLCAT
+#define SDL_strlcat    strlcat
 #else
-extern DECLSPEC char * SDLCALL SDL_strncpy(char *dst, const char *src, size_t maxlen);
-#endif
-
-#if HAVE_STRCAT
-#define SDL_strcat     strcat
-#else
-#define SDL_strcat(dst, src)    (SDL_strcpy(dst+SDL_strlen(dst), src), dst)
-#endif
-
-#if HAVE_STRNCAT
-#define SDL_strncat    strncat
-#else
-#define SDL_strncat(dst, src, n) (SDL_strncpy(dst+SDL_strlen(dst), src, n), dst)
+extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
 #endif
 
 #if HAVE_STRDUP
--- a/src/SDL_error.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/SDL_error.c	Sun Feb 19 23:46:34 2006 +0000
@@ -59,8 +59,7 @@
 	/* Copy in the key, mark error as valid */
 	error = SDL_GetErrBuf();
 	error->error = 1;
-	SDL_strncpy((char *)error->key, fmt, sizeof(error->key));
-	error->key[sizeof(error->key)-1] = '\0';
+	SDL_strlcpy((char *)error->key, fmt, sizeof(error->key));
 
 	va_start(ap, fmt);
 	error->argc = 0;
@@ -94,8 +93,7 @@
 				  char *str = va_arg(ap, char *);
 				  if (str == NULL)
 				      str = "(null)";
-				  SDL_strncpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
-				  error->args[index].buf[ERR_MAX_STRLEN-1] = 0;
+				  SDL_strlcpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
 				  error->argc++;
 				}
 				break;
@@ -251,8 +249,7 @@
 	/* Allocate the UNICODE buffer */
 	errstr16 = (Uint16 *)SDL_malloc(maxlen * (sizeof *errstr16));
 	if ( ! errstr16 ) {
-		SDL_strncpy((char *)errstr, "Out of memory", maxlen);
-		errstr[maxlen-1] = '\0';
+		SDL_strlcpy((char *)errstr, "Out of memory", maxlen);
 		return(errstr);
 	}
 
--- a/src/audio/SDL_audio.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/audio/SDL_audio.c	Sun Feb 19 23:46:34 2006 +0000
@@ -373,8 +373,7 @@
 char *SDL_AudioDriverName(char *namebuf, int maxlen)
 {
 	if ( current_audio != NULL ) {
-		SDL_strncpy(namebuf, current_audio->name, maxlen-1);
-		namebuf[maxlen-1] = '\0';
+		SDL_strlcpy(namebuf, current_audio->name, maxlen);
 		return(namebuf);
 	}
 	return(NULL);
--- a/src/audio/SDL_audiodev.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/audio/SDL_audiodev.c	Sun Feb 19 23:46:34 2006 +0000
@@ -89,7 +89,7 @@
 		audiodev = audiopath;
 	}
 	if ( path != NULL ) {
-		SDL_strncpy(path, audiodev, maxlen);
+		SDL_strlcpy(path, audiodev, maxlen);
 		path[maxlen-1] = '\0';
 	}
 	return(audio_fd);
@@ -136,7 +136,7 @@
 	}
 	audio_fd = open(audiodev, flags, 0);
 	if ( path != NULL ) {
-		SDL_strncpy(path, audiodev, maxlen);
+		SDL_strlcpy(path, audiodev, maxlen);
 		path[maxlen-1] = '\0';
 	}
 	return audio_fd;
@@ -166,8 +166,7 @@
 	    audio_fd = open(audiopath, flags, 0);
 	    if ( audio_fd > 0 ) {
 		if ( path != NULL ) {
-		    SDL_strncpy( path, audiopath, maxlen );
-		    path[maxlen-1] = '\0';
+		    SDL_strlcpy( path, audiopath, maxlen );
 		}
 	        return audio_fd;
 	    }
--- a/src/audio/alsa/SDL_alsa_audio.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/audio/alsa/SDL_alsa_audio.c	Sun Feb 19 23:46:34 2006 +0000
@@ -124,7 +124,7 @@
 	if (alsa_handle) {
 		alsa_loaded = 1;
 		retval = 0;
-		for (i = 0; i < SDL_TABLESIZE(alsa_functions); i++) {
+		for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
 /*			*alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/
 #if HAVE_DLVSYM
 			*alsa_functions[i].func = dlvsym(alsa_handle,alsa_functions[i].name,"ALSA_0.9");
--- a/src/audio/arts/SDL_artsaudio.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/audio/arts/SDL_artsaudio.c	Sun Feb 19 23:46:34 2006 +0000
@@ -90,7 +90,7 @@
 	if ( arts_handle ) {
 		arts_loaded = 1;
 		retval = 0;
-		for ( i=0; i<SDL_TABLESIZE(arts_functions); ++i ) {
+		for ( i=0; i<SDL_arraysize(arts_functions); ++i ) {
 			*arts_functions[i].func = SDL_LoadFunction(arts_handle, arts_functions[i].name);
 			if ( !*arts_functions[i].func ) {
 				retval = -1;
--- a/src/audio/esd/SDL_esdaudio.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/audio/esd/SDL_esdaudio.c	Sun Feb 19 23:46:34 2006 +0000
@@ -85,7 +85,7 @@
 	if ( esd_handle ) {
 		esd_loaded = 1;
 		retval = 0;
-		for ( i=0; i<SDL_TABLESIZE(esd_functions); ++i ) {
+		for ( i=0; i<SDL_arraysize(esd_functions); ++i ) {
 			*esd_functions[i].func = SDL_LoadFunction(esd_handle, esd_functions[i].name);
 			if ( !*esd_functions[i].func ) {
 				retval = -1;
--- a/src/cdrom/aix/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/aix/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -124,12 +124,11 @@
 
 		/* Add this drive to our list */
 		i = SDL_numcds;
-		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+		SDL_cdlist[i] = SDL_strdup(drive);
 		if ( SDL_cdlist[i] == NULL ) {
 			SDL_OutOfMemory();
 			return;
 		}
-		SDL_strcpy(SDL_cdlist[i], drive);
 		SDL_cdmode[i] = stbuf->st_rdev;
 		++SDL_numcds;
 #ifdef DEBUG_CDROM
@@ -302,9 +301,10 @@
 	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
 	if ( SDLcdrom != NULL ) {
 		char *cdpath, *delim;
-		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+		size_t len = SDL_strlen(SDLcdrom)+1;
+		cdpath = SDL_stack_alloc(char, len);
 		if ( cdpath != NULL ) {
-			SDL_strcpy(cdpath, SDLcdrom);
+			SDL_strlcpy(cdpath, SDLcdrom, len);
 			SDLcdrom = cdpath;
 			do {
 				delim = SDL_strchr(SDLcdrom, ':');
@@ -323,7 +323,7 @@
 					SDLcdrom = NULL;
 				}
 			} while ( SDLcdrom );
-			SDL_free(cdpath);
+			SDL_stack_free(cdpath);
 		}
 
 		/* If we found our drives, there's nothing left to do */
@@ -360,21 +360,23 @@
     int   fd;
     char* lastsl;
     char* cdromname;
+    size_t len;
 
     /*
      * We found /dev/cd? drives and that is in our list. But we can
      * open only the /dev/rcd? versions of those devices for Audio CD.
      */
-    cdromname = (char*)SDL_malloc( SDL_strlen(SDL_cdlist[drive]+2) );
-    SDL_strcpy(cdromname,SDL_cdlist[drive]);
+    len = SDL_strlen(SDL_cdlist[drive])+2;
+    cdromname = (char*)SDL_malloc(len);
+    SDL_strlcpy(cdromname,SDL_cdlist[drive],len);
     lastsl = SDL_strrchr(cdromname,'/');
     if (lastsl) {
 	*lastsl = 0;
-	strcat(cdromname,"/r");
+	SDL_strlcat(cdromname,"/r",len);
 	lastsl = SDL_strrchr(SDL_cdlist[drive],'/');
 	if (lastsl) {
 	    lastsl++;
-	    strcat(cdromname,lastsl);
+	    SDL_strlcat(cdromname,lastsl,len);
 	}
     }
 
--- a/src/cdrom/bsdi/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/bsdi/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -224,12 +224,11 @@
 
 		/* Add this drive to our list */
 		i = SDL_numcds;
-		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+		SDL_cdlist[i] = SDL_strdup(drive);
 		if ( SDL_cdlist[i] == NULL ) {
 			SDL_OutOfMemory();
 			return;
 		}
-		SDL_strcpy(SDL_cdlist[i], drive);
 		SDL_cdmode[i] = stbuf->st_rdev;
 		++SDL_numcds;
 #ifdef DEBUG_CDROM
@@ -265,9 +264,10 @@
 	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
 	if ( SDLcdrom != NULL ) {
 		char *cdpath, *delim;
-		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+		size_t len = SDL_strlen(SDLcdrom)+1;
+		cdpath = SDL_stack_alloc(char, len);
 		if ( cdpath != NULL ) {
-			SDL_strcpy(cdpath, SDLcdrom);
+			SDL_strlcpy(cdpath, SDLcdrom, len);
 			SDLcdrom = cdpath;
 			do {
 				delim = SDL_strchr(SDLcdrom, ':');
@@ -283,7 +283,7 @@
 					SDLcdrom = NULL;
 				}
 			} while ( SDLcdrom );
-			SDL_free(cdpath);
+			SDL_stack_free(cdpath);
 		}
 
 		/* If we found our drives, there's nothing left to do */
--- a/src/cdrom/freebsd/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/freebsd/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -110,12 +110,11 @@
 
 		/* Add this drive to our list */
 		i = SDL_numcds;
-		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+		SDL_cdlist[i] = SDL_strdup(drive);
 		if ( SDL_cdlist[i] == NULL ) {
 			SDL_OutOfMemory();
 			return;
 		}
-		SDL_strcpy(SDL_cdlist[i], drive);
 		SDL_cdmode[i] = stbuf->st_rdev;
 		++SDL_numcds;
 #ifdef DEBUG_CDROM
@@ -152,9 +151,10 @@
 	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
 	if ( SDLcdrom != NULL ) {
 		char *cdpath, *delim;
-		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+		size_t len = SDL_strlen(SDLcdrom)+1;
+		cdpath = SDL_stack_alloc(char, len);
 		if ( cdpath != NULL ) {
-			SDL_strcpy(cdpath, SDLcdrom);
+			SDL_strlcpy(cdpath, SDLcdrom, len);
 			SDLcdrom = cdpath;
 			do {
 				delim = SDL_strchr(SDLcdrom, ':');
@@ -170,7 +170,7 @@
 					SDLcdrom = NULL;
 				}
 			} while ( SDLcdrom );
-			SDL_free(cdpath);
+			SDL_stack_free(cdpath);
 		}
 
 		/* If we found our drives, there's nothing left to do */
--- a/src/cdrom/linux/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/linux/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -167,12 +167,11 @@
 
 		/* Add this drive to our list */
 		i = SDL_numcds;
-		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+		SDL_cdlist[i] = SDL_strdup(drive);
 		if ( SDL_cdlist[i] == NULL ) {
 			SDL_OutOfMemory();
 			return;
 		}
-		SDL_strcpy(SDL_cdlist[i], drive);
 		SDL_cdmode[i] = stbuf->st_rdev;
 		++SDL_numcds;
 #ifdef DEBUG_CDROM
@@ -192,21 +191,25 @@
 	if ( mntfp != NULL ) {
 		char *tmp;
 		char *mnt_type;
+		size_t mnt_type_len;
 		char *mnt_dev;
+		size_t mnt_dev_len;
 
 		while ( (mntent=getmntent(mntfp)) != NULL ) {
-			mnt_type = SDL_malloc(SDL_strlen(mntent->mnt_type) + 1);
+			mnt_type_len = SDL_strlen(mntent->mnt_type) + 1;
+			mnt_type = SDL_stack_alloc(char, mnt_type_len);
 			if (mnt_type == NULL)
 				continue;  /* maybe you'll get lucky next time. */
 
-			mnt_dev = SDL_malloc(SDL_strlen(mntent->mnt_fsname) + 1);
+			mnt_dev_len = SDL_strlen(mntent->mnt_fsname) + 1;
+			mnt_dev = SDL_stack_alloc(char, mnt_dev_len);
 			if (mnt_dev == NULL) {
-				SDL_free(mnt_type);
+				SDL_stack_free(mnt_type);
 				continue;
 			}
 
-			SDL_strcpy(mnt_type, mntent->mnt_type);
-			SDL_strcpy(mnt_dev, mntent->mnt_fsname);
+			SDL_strlcpy(mnt_type, mntent->mnt_type, mnt_type_len);
+			SDL_strlcpy(mnt_dev, mntent->mnt_fsname, mnt_dev_len);
 
 			/* Handle "supermount" filesystem mounts */
 			if ( SDL_strcmp(mnt_type, MNTTYPE_SUPER) == 0 ) {
@@ -242,8 +245,8 @@
 					AddDrive(mnt_dev, &stbuf);
 				}
 			}
-			SDL_free(mnt_dev);
-			SDL_free(mnt_type);
+			SDL_stack_free(mnt_dev);
+			SDL_stack_free(mnt_type);
 		}
 		endmntent(mntfp);
 	}
@@ -277,9 +280,10 @@
 	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
 	if ( SDLcdrom != NULL ) {
 		char *cdpath, *delim;
-		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+		size_t len = SDL_strlen(SDLcdrom)+1;
+		cdpath = SDL_stack_alloc(char, len);
 		if ( cdpath != NULL ) {
-			SDL_strcpy(cdpath, SDLcdrom);
+			SDL_strlcpy(cdpath, SDLcdrom, len);
 			SDLcdrom = cdpath;
 			do {
 				delim = SDL_strchr(SDLcdrom, ':');
@@ -298,7 +302,7 @@
 					SDLcdrom = NULL;
 				}
 			} while ( SDLcdrom );
-			SDL_free(cdpath);
+			SDL_stack_free(cdpath);
 		}
 
 		/* If we found our drives, there's nothing left to do */
--- a/src/cdrom/openbsd/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/openbsd/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -115,12 +115,11 @@
 
 		/* Add this drive to our list */
 		i = SDL_numcds;
-		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+		SDL_cdlist[i] = SDL_strdup(drive);
 		if ( SDL_cdlist[i] == NULL ) {
 			SDL_OutOfMemory();
 			return;
 		}
-		SDL_strcpy(SDL_cdlist[i], drive);
 		SDL_cdmode[i] = stbuf->st_rdev;
 		++SDL_numcds;
 #ifdef DEBUG_CDROM
@@ -161,9 +160,10 @@
 	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
 	if ( SDLcdrom != NULL ) {
 		char *cdpath, *delim;
-		cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+		size_t len = SDL_strlen(SDLcdrom)+1;
+		cdpath = SDL_stack_alloc(char, len);
 		if ( cdpath != NULL ) {
-			SDL_strcpy(cdpath, SDLcdrom);
+			SDL_strlcpy(cdpath, SDLcdrom, len);
 			SDLcdrom = cdpath;
 			do {
 				delim = SDL_strchr(SDLcdrom, ':');
@@ -179,7 +179,7 @@
 					SDLcdrom = NULL;
 				}
 			} while ( SDLcdrom );
-			SDL_free(cdpath);
+			SDL_stack_free(cdpath);
 		}
 
 		/* If we found our drives, there's nothing left to do */
--- a/src/cdrom/os2/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/os2/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -94,13 +94,12 @@
 	{
 	msp.ulNumber = i+1;
 	mciSendCommand(0,MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_WAIT,&msp, 0);
-	SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(SysInfoRet)+1);
+	SDL_cdlist[i] = SDL_strdup(SysInfoRet);
 	if ( SDL_cdlist[i] == NULL )
 		{
 		SDL_OutOfMemory();
 		return(-1);
 		}
-	SDL_strcpy(SDL_cdlist[i], SysInfoRet);
 	}
 return(0);
 }
--- a/src/cdrom/osf/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/osf/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -119,13 +119,11 @@
 
 	/* Add this drive to our list */
 	i = SDL_numcds;
-	SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+	SDL_cdlist[i] = SDL_strdup(drive);
 	if ( SDL_cdlist[i] == NULL ) {
 	    SDL_OutOfMemory();
 	    return;
 	}
-
-	SDL_strcpy(SDL_cdlist[i], drive);
 	SDL_cdmode[i] = stbuf->st_rdev;
 	++SDL_numcds;
 #ifdef DEBUG_CDROM
@@ -174,9 +172,10 @@
     SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
     if ( SDLcdrom != NULL ) {
 	char *cdpath, *delim;
-	cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+	size_t len = SDL_strlen(SDLcdrom)+1;
+	cdpath = SDL_stack_alloc(len);
 	if ( cdpath != NULL ) {
-	    SDL_strcpy(cdpath, SDLcdrom);
+	    SDL_strlcpy(cdpath, SDLcdrom, len);
 	    SDLcdrom = cdpath;
 	    do {
 		delim = SDL_strchr(SDLcdrom, ':');
@@ -192,7 +191,7 @@
 		    SDLcdrom = NULL;
 		}
 	    } while ( SDLcdrom );
-	    SDL_free(cdpath);
+	    SDL_stack_free(cdpath);
 	}
 
 	/* If we found our drives, there's nothing left to do */
@@ -226,15 +225,6 @@
 #endif
 	}
     }
-
-/*
-    SDLcdrom=SDL_malloc(sizeof(char) * 32);
-    SDL_strcpy(SDLcdrom,"/dev/rdisk/cdrom0c");
-    SDL_cdlist[0] = SDLcdrom;
-    stat(SDLcdrom, &stbuf);
-    SDL_cdmode[0] = stbuf.st_rdev;
-    SDL_numcds = 1;
- */
     return (0);
 }
 
--- a/src/cdrom/qnx/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/qnx/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -124,13 +124,12 @@
         /* Add this drive to our list */
 
         i = SDL_numcds;
-        SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+        SDL_cdlist[i] = SDL_strdup(drive);
         if (SDL_cdlist[i] == NULL)
         {
             SDL_OutOfMemory();
             return;
         }
-        SDL_strcpy(SDL_cdlist[i], drive);
         SDL_cdmode[i] = stbuf->st_rdev;
         ++SDL_numcds;
     }
@@ -169,11 +168,11 @@
     if ( SDLcdrom != NULL )
     {
         char *cdpath, *delim;
-
-        cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1);
+	size_t len = SDL_strlen(SDLcdrom)+1;
+        cdpath = SDL_stack_alloc(len);
         if (cdpath != NULL)
         {
-            SDL_strcpy(cdpath, SDLcdrom);
+            SDL_strlcpy(cdpath, SDLcdrom, len);
             SDLcdrom = cdpath;
             do {
                 delim = SDL_strchr(SDLcdrom, ':');
@@ -194,7 +193,7 @@
                     SDLcdrom = NULL;
                 }
             } while (SDLcdrom);
-            SDL_free(cdpath);
+            SDL_stack_free(cdpath);
         }
 
         /* If we found our drives, there's nothing left to do */
--- a/src/cdrom/win32/SDL_syscdrom.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/cdrom/win32/SDL_syscdrom.c	Sun Feb 19 23:46:34 2006 +0000
@@ -63,12 +63,11 @@
 	if ( SDL_numcds < MAX_DRIVES ) {
 		/* Add this drive to our list */
 		i = SDL_numcds;
-		SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1);
+		SDL_cdlist[i] = SDL_strdup(drive);
 		if ( SDL_cdlist[i] == NULL ) {
 			SDL_OutOfMemory();
 			return;
 		}
-		SDL_strcpy(SDL_cdlist[i], drive);
 		++SDL_numcds;
 #ifdef CDROM_DEBUG
   fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
--- a/src/joystick/SDL_joystick.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/joystick/SDL_joystick.c	Sun Feb 19 23:46:34 2006 +0000
@@ -554,7 +554,7 @@
 	switch (state) {
 		case SDL_QUERY:
 			state = SDL_IGNORE;
-			for ( i=0; i<SDL_TABLESIZE(event_list); ++i ) {
+			for ( i=0; i<SDL_arraysize(event_list); ++i ) {
 				state = SDL_EventState(event_list[i],SDL_QUERY);
 				if ( state == SDL_ENABLE ) {
 					break;
@@ -562,7 +562,7 @@
 			}
 			break;
 		default:
-			for ( i=0; i<SDL_TABLESIZE(event_list); ++i ) {
+			for ( i=0; i<SDL_arraysize(event_list); ++i ) {
 				SDL_EventState(event_list[i], state);
 			}
 			break;
--- a/src/joystick/linux/SDL_sysjoystick.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/joystick/linux/SDL_sysjoystick.c	Sun Feb 19 23:46:34 2006 +0000
@@ -179,17 +179,6 @@
 #endif
 };
 
-static char *mystrdup(const char *string)
-{
-	char *newstring;
-
-	newstring = (char *)SDL_malloc(SDL_strlen(string)+1);
-	if ( newstring ) {
-		SDL_strcpy(newstring, string);
-	}
-	return(newstring);
-}
-
 
 #ifndef NO_LOGICAL_JOYSTICKS
 
@@ -204,7 +193,7 @@
       name = SDL_SYS_JoystickName(i);
 
       if (name) {
-         for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) {
+         for(j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
             if (!SDL_strcmp(name, joystick_logicalmap[j].name)) {
 
                prev = i;
@@ -303,13 +292,12 @@
 
 	/* First see if the user specified a joystick to use */
 	if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {
-		SDL_strncpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
-		path[sizeof(path)-1] = '\0';
+		SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
 		if ( stat(path, &sb) == 0 ) {
 			fd = open(path, O_RDONLY, 0);
 			if ( fd >= 0 ) {
 				/* Assume the user knows what they're doing. */
-				SDL_joylist[numjoysticks].fname =mystrdup(path);
+				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
 				if ( SDL_joylist[numjoysticks].fname ) {
 					dev_nums[numjoysticks] = sb.st_rdev;
 					++numjoysticks;
@@ -319,7 +307,7 @@
 		}
 	}
 
-	for ( i=0; i<SDL_TABLESIZE(joydev_pattern); ++i ) {
+	for ( i=0; i<SDL_arraysize(joydev_pattern); ++i ) {
 		for ( j=0; j < MAX_JOYSTICKS; ++j ) {
 			SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);
 
@@ -357,7 +345,7 @@
 				close(fd);
 
 				/* We're fine, add this joystick */
-				SDL_joylist[numjoysticks].fname =mystrdup(path);
+				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
 				if ( SDL_joylist[numjoysticks].fname ) {
 					dev_nums[numjoysticks] = sb.st_rdev;
 					++numjoysticks;
@@ -489,7 +477,7 @@
 	}
 
 	/* Special joystick support */
-	for ( i=0; i < SDL_TABLESIZE(special_joysticks); ++i ) {
+	for ( i=0; i < SDL_arraysize(special_joysticks); ++i ) {
 		if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {
 
 			joystick->naxes = special_joysticks[i].naxes;
@@ -503,7 +491,7 @@
 
 	/* User environment joystick support */
 	if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {
-		SDL_strcpy(env_name, "");
+		*env_name = '\0';
 		if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )
 			env += SDL_strlen(env_name)+2;
 		else if ( SDL_sscanf(env, "%s", env_name) == 1 )
--- a/src/joystick/macos/SDL_sysjoystick.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/joystick/macos/SDL_sysjoystick.c	Sun Feb 19 23:46:34 2006 +0000
@@ -165,7 +165,7 @@
 		return(-1);
     }
     SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
-    SDL_strcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index));
+    SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index), SDL_arraysize(joystick->hwdata->name));
     joystick->name = joystick->hwdata->name;
 
     ISpElementList_ExtractByKind(
--- a/src/joystick/os2/SDL_sysjoystick.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/joystick/os2/SDL_sysjoystick.c	Sun Feb 19 23:46:34 2006 +0000
@@ -214,7 +214,7 @@
 		if (joycfg.buttons>=7) SYS_JoyData[0].buttoncalc[2]=((axis[2]->upper+axis[3]->centre)>>1);
 		if (joycfg.buttons>=8) SYS_JoyData[0].buttoncalc[3]=((axis[3]->upper+axis[3]->centre)>>1);
 		/* Intialize Joystick Name */
-		SDL_strcpy (SYS_JoyData[0].szDeviceName,joycfg.name);
+		SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName));
 		}
 	/* Default Init ... autoconfig */
 	else
--- a/src/loadso/dlopen/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/loadso/dlopen/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
@@ -42,9 +42,10 @@
 {
 	void *symbol = dlsym(handle, name);
 	if ( symbol == NULL ) {
-		char *_name = SDL_stack_alloc(char, 1+SDL_strlen(name)+1);
+		size_t len = 1+SDL_strlen(name)+1;
+		char *_name = SDL_stack_alloc(char, len);
 		_name[0] = '_';
-		SDL_strcpy(&_name[1], name);
+		SDL_strlcpy(&_name[1], name, len);
 		symbol = dlsym(handle, name);
 		SDL_stack_free(_name);
 		if ( symbol == NULL ) {
--- a/src/loadso/macos/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/loadso/macos/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
@@ -42,8 +42,7 @@
 	OSErr error;
 	char psofile[512];
 
-	SDL_strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
-	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
+	SDL_strlcpy(psofile, sofile, SDL_arraysize(psofile));
 	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
 			kLoadCFrag, &library_id, &mainAddr, errName);
 	switch (error) {
@@ -80,8 +79,7 @@
 	CFragConnectionID library_id = (CFragConnectionID)handle;
 	char pname[512];
 
-	SDL_strncpy(pname, name, SDL_TABLESIZE(pname));
-	pname[SDL_TABLESIZE(pname)-1] = '\0';
+	SDL_strlcpy(pname, name, SDL_arraysize(pname));
 	if ( FindSymbol(library_id, C2PStr(pname),
 	                (char **)&symbol, &class) != noErr ) {
 		loaderror = "Symbol not found";
--- a/src/loadso/macosx/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/loadso/macosx/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
@@ -281,7 +281,7 @@
 	va_start(arg, str);
 	tss = pthread_getspecific(dlerror_key);
 	err_str = tss->errstr;
-	SDL_strncpy(err_str, "dlcompat: ", ERR_STR_LEN);
+	SDL_strlcpy(err_str, "dlcompat: ", ERR_STR_LEN);
 	vsnprintf(err_str + 10, ERR_STR_LEN - 10, str, arg);
 	va_end(arg);
 	debug("ERROR: %s\n", err_str);
@@ -620,18 +620,17 @@
 }
 
 /* Up to the caller to SDL_free() returned string */
-static inline const char *dyld_error_str()
+static inline char *dyld_error_str()
 {
 	NSLinkEditErrors dylder;
 	int dylderno;
 	const char *dylderrstr;
 	const char *dyldfile;
-	const char* retStr = NULL;
+	char* retStr = NULL;
 	NSLinkEditError(&dylder, &dylderno, &dyldfile, &dylderrstr);
-	if (dylderrstr && SDL_strlen(dylderrstr))
+	if (dylderrstr && *dylderrstr)
 	{
-		retStr = SDL_malloc(SDL_strlen(dylderrstr) +1);
-		SDL_strcpy((char*)retStr,dylderrstr);
+		retStr = SDL_strdup(dylderrstr);
 	}
 	return retStr;
 }
@@ -645,7 +644,7 @@
 	void *caller = NULL;
 #endif
 	const struct mach_header *caller_mh = 0;
-	const char* savedErrorStr = NULL;
+	char* savedErrorStr = NULL;
 	resetdlerror();
 #ifndef RTLD_SELF
 #define	RTLD_SELF		((void *) -3)
@@ -734,9 +733,9 @@
 			else
 			{
 				if (savedErrorStr)
-					SDL_free((char*)savedErrorStr);			
+					SDL_free(savedErrorStr);			
 				savedErrorStr = SDL_malloc(256);
-				SDL_snprintf((char*)savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol);	
+				SDL_snprintf(savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol);	
 			}
 		}
 	}
@@ -746,9 +745,9 @@
 		if (!savedErrorStr || !SDL_strlen(savedErrorStr))
 		{
 			if (savedErrorStr)
-				SDL_free((char*)savedErrorStr);
+				SDL_free(savedErrorStr);
 			savedErrorStr = SDL_malloc(256);
-			SDL_snprintf((char*)savedErrorStr, 256,"Symbol \"%s\" not found",symbol);
+			SDL_snprintf(savedErrorStr, 256,"Symbol \"%s\" not found",symbol);
 		}
 		if (canSetError)
 		{
@@ -759,7 +758,7 @@
 			debug(savedErrorStr);
 		}
 		if (savedErrorStr)
-			SDL_free((char*)savedErrorStr);
+			SDL_free(savedErrorStr);
 		return NULL;
 	}
 	return NSAddressOfSymbol(nssym);
--- a/src/loadso/win32/SDL_sysloadso.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/loadso/win32/SDL_sysloadso.c	Sun Feb 19 23:46:34 2006 +0000
@@ -47,7 +47,7 @@
 					FORMAT_MESSAGE_FROM_SYSTEM),
 				NULL, GetLastError(), 
 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
+				errbuf_t, SDL_arraysize(errbuf), NULL);
 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
 		loaderror = errbuf;
 	}
@@ -66,7 +66,7 @@
 					FORMAT_MESSAGE_FROM_SYSTEM),
 				NULL, GetLastError(), 
 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-				errbuf, SDL_TABLESIZE(errbuf), NULL);
+				errbuf, SDL_arraysize(errbuf), NULL);
 		loaderror = errbuf;
 	}
 #endif
@@ -97,7 +97,7 @@
 					FORMAT_MESSAGE_FROM_SYSTEM),
 				NULL, GetLastError(), 
 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
+				errbuf_t, SDL_arraysize(errbuf), NULL);
 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
 		loaderror = errbuf;
 	}
@@ -114,7 +114,7 @@
 					FORMAT_MESSAGE_FROM_SYSTEM),
 				NULL, GetLastError(), 
 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-				errbuf, SDL_TABLESIZE(errbuf), NULL);
+				errbuf, SDL_arraysize(errbuf), NULL);
 		loaderror = errbuf;
 	}
 #endif
--- a/src/main/win32/SDL_win32_main.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/main/win32/SDL_win32_main.c	Sun Feb 19 23:46:34 2006 +0000
@@ -18,7 +18,6 @@
 # define fopen		_wfopen
 # define freopen	_wfreopen
 # define remove(x)	DeleteFile(x)
-# define strcat		wcscat
 #else
 # define DIR_SEPERATOR TEXT("/")
 # include <direct.h>
@@ -208,8 +207,7 @@
 	if ( bufp == NULL ) {
 		return OutOfMemory();
 	}
-	SDL_strncpy(bufp, appname, n);
-	bufp[n] = '\0';
+	SDL_strlcpy(bufp, appname, n);
 	appname = bufp;
 
 	/* Load SDL dynamic link library */
@@ -259,10 +257,12 @@
 	int nLen;
 #else
 	char *bufp;
+	size_t nLen;
 #endif
 #ifndef NO_STDIO_REDIRECT
 	FILE *newfp;
 #endif
+	int retval;
 
 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
 	   keep them open.  This is a hack.. hopefully it will be fixed 
@@ -274,14 +274,14 @@
 	}
 
 #ifndef NO_STDIO_REDIRECT
-	pathlen = GetModuleFileName(NULL, path, SDL_TABLESIZE(path));
+	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
 		--pathlen;
 	}
 	path[pathlen] = '\0';
 
-	SDL_strcpy( stdoutPath, path );
-	SDL_strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
+	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
+	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
     
 	/* Redirect standard input and standard output */
 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
@@ -299,8 +299,8 @@
 	}
 #endif /* _WIN32_WCE */
 
-	SDL_strcpy( stderrPath, path );
-	SDL_strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
+	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
+	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
 
 	newfp = freopen(stderrPath, TEXT("w"), stderr);
 #ifndef _WIN32_WCE
@@ -328,7 +328,7 @@
 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
 	nLen = wcslen(bufp)+1;
-	cmdline = (char *)alloca(nLen);
+	cmdline = SDL_stack_alloc(wchar_t, nLen);
 	if ( cmdline == NULL ) {
 		return OutOfMemory();
 	}
@@ -336,21 +336,26 @@
 #else
 	/* Grab the command line (use alloca() on Windows) */
 	bufp = GetCommandLine();
-	cmdline = (char *)alloca(SDL_strlen(bufp)+1);
+	nLen = SDL_strlen(bufp)+1;
+	cmdline = SDL_stack_alloc(char, nLen);
 	if ( cmdline == NULL ) {
 		return OutOfMemory();
 	}
-	SDL_strcpy(cmdline, bufp);
+	SDL_strlcpy(cmdline, bufp, nLen);
 #endif
 
 	/* Parse it into argv and argc */
 	argc = ParseCommandLine(cmdline, NULL);
-	argv = (char **)alloca((argc+1)*(sizeof *argv));
+	argv = SDL_stack_alloc(char*, argc+1);
 	if ( argv == NULL ) {
 		return OutOfMemory();
 	}
 	ParseCommandLine(cmdline, argv);
 
 	/* Run the main program (after a little SDL initialization) */
-	return(console_main(argc, argv));
+	retval = console_main(argc, argv);
+
+	SDL_stack_free(cmdline);
+	SDL_stack_free(argv);
+	return retval;
 }
--- a/src/stdlib/SDL_getenv.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/stdlib/SDL_getenv.c	Sun Feb 19 23:46:34 2006 +0000
@@ -53,7 +53,7 @@
 		SDL_envmem = newmem;
 		SDL_envmemlen = bufferlen;
 	}
-	SDL_strcpy(SDL_envmem, variable);
+	SDL_strlcpy(SDL_envmem, variable, bufferlen);
 	value = SDL_envmem + (sep - variable);
 	*value++ = '\0';
 	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
@@ -111,11 +111,10 @@
 	}
 
 	/* Allocate memory for the variable */
-	new_variable = (char *)SDL_malloc(SDL_strlen(variable)+1);
+	new_variable = SDL_strdup(variable);
 	if ( ! new_variable ) {
 		return(-1);
 	}
-	SDL_strcpy(new_variable, variable);
 
 	/* Actually put it into the environment */
 	added = 0;
--- a/src/stdlib/SDL_string.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/stdlib/SDL_string.c	Sun Feb 19 23:46:34 2006 +0000
@@ -294,39 +294,38 @@
 }
 #endif
 
-#ifndef HAVE_STRCPY
-char *SDL_strcpy(char *dst, const char *src)
+#ifndef HAVE_STRLCPY
+size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen)
 {
-    char *dstp = dst;
-    while ( *src ) {
-        *dstp++ = *src++;
+    size_t srclen = SDL_strlen(src);
+    if ( maxlen > 0 ) {
+        size_t len = SDL_min(srclen, maxlen-1);
+        SDL_memcpy(dst, src, len);
+        dst[len] = '\0';
     }
-    *dstp = '\0';
-
-    return dst;
+    return srclen;
 }
 #endif
 
-#ifndef HAVE_STRNCPY
-char *SDL_strncpy(char *dst, const char *src, size_t maxlen)
+#ifndef HAVE_STRLCAT
+size_t SDL_strlcat(char *dst, const char *src, size_t maxlen)
 {
-    char *dstp = dst;
-    while ( maxlen-- && *src ) {
-        *dstp++ = *src++;
+    size_t dstlen = SDL_strlen(dst);
+    size_t srclen = SDL_strlen(src);
+    if ( dstlen < maxlen ) {
+        SDL_strlcpy(dst+dstlen, src, maxlen-dstlen);
     }
-    *dstp = '\0';
-
-    return dst;
+    return dstlen+srclen;
 }
 #endif
 
 #ifndef HAVE_STRDUP
 char *SDL_strdup(const char *string)
 {
-    size_t len = SDL_strlen(string);
-    char *newstr = SDL_malloc(len+1);
+    size_t len = SDL_strlen(string)+1;
+    char *newstr = SDL_malloc(len);
     if ( newstr ) {
-        SDL_strcpy(newstr, string);
+        SDL_strlcpy(newstr, string, len);
     }
     return newstr;
 }
@@ -912,7 +911,7 @@
     if ( size > maxlen ) {
         size = maxlen;
     }
-    SDL_strncpy(text, num, size);
+    SDL_strlcpy(text, num, size);
 
     return size;
 }
@@ -926,7 +925,7 @@
     if ( size > maxlen ) {
         size = maxlen;
     }
-    SDL_strncpy(text, num, size);
+    SDL_strlcpy(text, num, size);
 
     return size;
 }
@@ -941,7 +940,7 @@
     if ( size > maxlen ) {
         size = maxlen;
     }
-    SDL_strncpy(text, num, size);
+    SDL_strlcpy(text, num, size);
 
     return size;
 }
@@ -955,7 +954,7 @@
     if ( size > maxlen ) {
         size = maxlen;
     }
-    SDL_strncpy(text, num, size);
+    SDL_strlcpy(text, num, size);
 
     return size;
 }
--- a/src/video/SDL_video.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/SDL_video.c	Sun Feb 19 23:46:34 2006 +0000
@@ -283,8 +283,7 @@
 char *SDL_VideoDriverName(char *namebuf, int maxlen)
 {
 	if ( current_video != NULL ) {
-		SDL_strncpy(namebuf, current_video->name, maxlen-1);
-		namebuf[maxlen-1] = '\0';
+		SDL_strlcpy(namebuf, current_video->name, maxlen);
 		return(namebuf);
 	}
 	return(NULL);
@@ -1664,19 +1663,13 @@
 			if ( video->wm_title ) {
 				SDL_free(video->wm_title);
 			}
-			video->wm_title = (char *)SDL_malloc(SDL_strlen(title)+1);
-			if ( video->wm_title != NULL ) {
-				SDL_strcpy(video->wm_title, title);
-			}
+			video->wm_title = SDL_strdup(title);
 		}
 		if ( icon ) {
 			if ( video->wm_icon ) {
 				SDL_free(video->wm_icon);
 			}
-			video->wm_icon = (char *)SDL_malloc(SDL_strlen(icon)+1);
-			if ( video->wm_icon != NULL ) {
-				SDL_strcpy(video->wm_icon, icon);
-			}
+			video->wm_icon = SDL_strdup(icon);
 		}
 		if ( (title || icon) && (video->SetCaption != NULL) ) {
 			video->SetCaption(this, video->wm_title,video->wm_icon);
--- a/src/video/Xext/Xv/Xv.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/Xext/Xv/Xv.c	Sun Feb 19 23:46:34 2006 +0000
@@ -234,8 +234,7 @@
           SyncHandle();
 	  return(XvBadAlloc);
 	}
-      (void)strncpy(name, u.string, size);
-      name[size] = '\0';
+      SDL_strlcpy(name, u.string, size);
       pa->name = name;
 
       u.buffer += (size + 3) & ~3;
@@ -386,8 +385,7 @@
           SyncHandle();
 	  return(XvBadAlloc);
       }
-      strncpy(name, u.string, size);
-      name[size] = '\0';
+      SDL_strlcpy(name, u.string, size);
       pe->name = name;
       pe++;
 
--- a/src/video/Xext/extensions/extutil.h	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/Xext/extensions/extutil.h	Sun Feb 19 23:46:34 2006 +0000
@@ -36,6 +36,8 @@
 #ifndef _EXTUTIL_H_
 #define _EXTUTIL_H_
 
+#include "SDL_stdinc.h"		/* For portable string functions */
+
 #include <X11/extensions/Xext.h>
 
 /*
@@ -212,7 +214,7 @@
     code -= codes->first_error;  \
     if (code >= 0 && code < nerr) { \
 	char tmp[256]; \
-	sprintf (tmp, "%s.%d", extname, code); \
+	SDL_snprintf (tmp, SDL_arraysize(tmp), "%s.%d", extname, code); \
 	pXGetErrorDatabaseText (dpy, "XProtoError", tmp, errl[code], buf, n); \
 	return buf; \
     } \
--- a/src/video/aalib/SDL_aaevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/aalib/SDL_aaevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -122,7 +122,7 @@
 	const char *std;
 
 	/* Initialize the AAlib key translation table */
-	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
+	for ( i=0; i<SDL_arraysize(keymap); ++i )
 		keymap[i] = SDLK_UNKNOWN;
 
 	keymap[AA_ESC] = SDLK_ESCAPE;
--- a/src/video/ataricommon/SDL_atarigl.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/ataricommon/SDL_atarigl.c	Sun Feb 19 23:46:34 2006 +0000
@@ -219,10 +219,10 @@
 
 	this->gl_config.dll_handle = handle;
 	if ( path ) {
-		SDL_strncpy(this->gl_config.driver_path, path,
-			sizeof(this->gl_config.driver_path)-1);
+		SDL_strlcpy(this->gl_config.driver_path, path,
+			SDL_arraysize(this->gl_config.driver_path));
 	} else {
-		SDL_strcpy(this->gl_config.driver_path, "");
+		*this->gl_config.driver_path = '\0';
 	}
 
 #endif
--- a/src/video/cybergfx/SDL_amigaevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/cybergfx/SDL_amigaevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -301,7 +301,7 @@
 	int i;
 
 	/* Map the miscellaneous keys */
-	for ( i=0; i<SDL_TABLESIZE(MISC_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(MISC_keymap); ++i )
 		MISC_keymap[i] = SDLK_UNKNOWN;
 
 	/* These X keysyms have 0xFF as the high byte */
--- a/src/video/directfb/SDL_DirectFB_events.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/directfb/SDL_DirectFB_events.c	Sun Feb 19 23:46:34 2006 +0000
@@ -90,7 +90,7 @@
   int i;
 	
   /* Initialize the DirectFB key translation table */
-  for (i=0; i<SDL_TABLESIZE(keymap); ++i)
+  for (i=0; i<SDL_arraysize(keymap); ++i)
     keymap[i] = SDLK_UNKNOWN;
 
   keymap[DIKI_A - DIKI_UNKNOWN] = SDLK_a;
--- a/src/video/fbcon/SDL_fbevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/fbcon/SDL_fbevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -994,7 +994,7 @@
 	/* Initialize the Linux key translation table */
 
 	/* First get the ascii keys and others not well handled */
-	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
+	for (i=0; i<SDL_arraysize(keymap); ++i) {
 	  switch(i) {
 	  /* These aren't handled by the x86 kernel keymapping (?) */
 	  case SCANCODE_PRINTSCREEN:
@@ -1033,7 +1033,7 @@
 	    break;
           }
 	}
-	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
+	for (i=0; i<SDL_arraysize(keymap); ++i) {
 	  switch(keymap_temp[i]) {
 	    case K_F1:  keymap[i] = SDLK_F1;  break;
 	    case K_F2:  keymap[i] = SDLK_F2;  break;
--- a/src/video/ggi/SDL_ggievents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/ggi/SDL_ggievents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -123,7 +123,7 @@
 	int i;
 	
 	/* Initialize the GGI key translation table */
-	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
+	for ( i=0; i<SDL_arraysize(keymap); ++i )
 		keymap[i] = SDLK_UNKNOWN;
 	
 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
--- a/src/video/maccommon/SDL_macevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/maccommon/SDL_macevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -457,7 +457,7 @@
 	int world = SDLK_WORLD_0;
 
 	/* Map the MAC keysyms */
-	for ( i=0; i<SDL_TABLESIZE(MAC_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(MAC_keymap); ++i )
 		MAC_keymap[i] = SDLK_UNKNOWN;
 
 	/* Defined MAC_* constants */
--- a/src/video/macdsp/SDL_dspvideo.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/macdsp/SDL_dspvideo.c	Sun Feb 19 23:46:34 2006 +0000
@@ -355,7 +355,7 @@
 	if ( DSpContext_GetAttributes (context, &attributes) != noErr )
 		return NULL;
 			
-	for ( i = 0; i < SDL_TABLESIZE(temp_list); i++ ) {
+	for ( i = 0; i < SDL_arraysize(temp_list); i++ ) {
 		width  = attributes.displayWidth;
 		height = attributes.displayHeight;
 		
--- a/src/video/nanox/SDL_nxevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/nanox/SDL_nxevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -41,7 +41,7 @@
     Dprintf ("enter NX_InitOSKeymap\n") ;
 
     // Map the nanox scancodes to SDL keysyms
-    for (i = 0; i < SDL_TABLESIZE (NX_NONASCII_keymap); ++ i)
+    for (i = 0; i < SDL_arraysize (NX_NONASCII_keymap); ++ i)
         NX_NONASCII_keymap [i] = SDLK_UNKNOWN ;
 
     NX_NONASCII_keymap [MWKEY_LEFT        & 0xFF] = SDLK_LEFT ;
--- a/src/video/photon/SDL_ph_events.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/photon/SDL_ph_events.c	Sun Feb 19 23:46:34 2006 +0000
@@ -442,13 +442,13 @@
     int i;
 
     /* Odd keys used in international keyboards */
-    for (i=0; i<SDL_TABLESIZE(ODD_keymap); ++i)
+    for (i=0; i<SDL_arraysize(ODD_keymap); ++i)
     {
         ODD_keymap[i] = SDLK_UNKNOWN;
     }
 
     /* Map the miscellaneous keys */
-    for (i=0; i<SDL_TABLESIZE(MISC_keymap); ++i)
+    for (i=0; i<SDL_arraysize(MISC_keymap); ++i)
     {
         MISC_keymap[i] = SDLK_UNKNOWN;
     }
--- a/src/video/photon/SDL_ph_gl.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/photon/SDL_ph_gl.c	Sun Feb 19 23:46:34 2006 +0000
@@ -123,7 +123,7 @@
     this->gl_config.dll_handle = handle;
     this->gl_config.driver_loaded = 1;
 
-    SDL_strncpy(this->gl_config.driver_path, path, sizeof(this->gl_config.driver_path)-1);
+    SDL_strlcpy(this->gl_config.driver_path, path, SDL_arraysize(this->gl_config.driver_path));
 
     return 0;
 }
--- a/src/video/ps2gs/SDL_gsevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/ps2gs/SDL_gsevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -822,7 +822,7 @@
 	/* Initialize the Linux key translation table */
 
 	/* First get the ascii keys and others not well handled */
-	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
+	for (i=0; i<SDL_arraysize(keymap); ++i) {
 	  switch(i) {
 	  /* These aren't handled by the x86 kernel keymapping (?) */
 	  case SCANCODE_PRINTSCREEN:
@@ -861,7 +861,7 @@
 	    break;
           }
 	}
-	for (i=0; i<SDL_TABLESIZE(keymap); ++i) {
+	for (i=0; i<SDL_arraysize(keymap); ++i) {
 	  switch(keymap_temp[i]) {
 	    case K_F1:  keymap[i] = SDLK_F1;  break;
 	    case K_F2:  keymap[i] = SDLK_F2;  break;
--- a/src/video/riscos/SDL_riscosFullScreenVideo.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/riscos/SDL_riscosFullScreenVideo.c	Sun Feb 19 23:46:34 2006 +0000
@@ -657,8 +657,7 @@
 /** Store caption in case this is called before we create a window */
 void FULLSCREEN_SetWMCaption(_THIS, const char *title, const char *icon)
 {
-	SDL_strncpy(this->hidden->title, title, 255);
-	this->hidden->title[255] = 0;
+	SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
 }
 
 /* Set screen mode
--- a/src/video/riscos/SDL_riscosevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/riscos/SDL_riscosevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -101,7 +101,7 @@
 	int i;
 
 	/* Map the VK keysyms */
-	for ( i=0; i<SDL_TABLESIZE(RO_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(RO_keymap); ++i )
 		RO_keymap[i] = SDLK_UNKNOWN;
 
   RO_keymap[3] = SDLK_LSHIFT;
--- a/src/video/riscos/SDL_riscostask.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/riscos/SDL_riscostask.c	Sun Feb 19 23:46:34 2006 +0000
@@ -57,7 +57,7 @@
 
 /* Local function */
 
-static int RISCOS_GetTaskName(char *task_name);
+static int RISCOS_GetTaskName(char *task_name, size_t maxlen);
 
 /* Uncomment next line to copy mode changes/restores to stderr */
 /* #define DUMP_MODE */
@@ -91,7 +91,7 @@
    _kernel_swi_regs regs;
    int messages[4];
 
-   if (RISCOS_GetTaskName(task_name) == 0) return 0;
+   if (RISCOS_GetTaskName(task_name, SDL_arraysize(task_name)) == 0) return 0;
 
    messages[0] = 9;       /* Palette changed */
    messages[1] = 0x400c1; /* Mode changed */
@@ -177,7 +177,7 @@
 
 ***************************************************************************/
 
-int RISCOS_GetTaskName(char *task_name)
+int RISCOS_GetTaskName(char *task_name, size_t maxlen)
 {
 	_kernel_swi_regs regs;
 
@@ -187,11 +187,12 @@
    if (_kernel_swi(OS_GetEnv, &regs, &regs) == 0)
    {
 	   char *command_line = (char *)regs.r[0];
-	   char *buffer = SDL_malloc(SDL_strlen(command_line)+1);
+	   size_t len = SDL_strlen(command_line)+1;
+	   char *buffer = SDL_stack_alloc(char, len);
 	   char *env_var;
 	   char *p;
 
-	   SDL_strcpy(buffer, command_line);
+	   SDL_strlcpy(buffer, command_line, len);
 	   p = SDL_strchr(buffer, ' ');
 	   if (p) *p = 0;
 	   p = SDL_strrchr(buffer, '.');
@@ -217,45 +218,45 @@
 	   if (*p)
 	   {
 		   /* Read variables that effect the RISC OS SDL engine for this task */
-		   env_var = SDL_malloc(SDL_strlen(p) + 18); /* 18 is larger than the biggest variable name */
+		   len = SDL_strlen(p) + 18; /* 18 is larger than the biggest variable name */
+		   env_var = SDL_stack_alloc(char, len);
 		   if (env_var)
 		   {
 			   char *env_val;
 
 			   /* See if a variable of form SDL$<dirname>$TaskName exists */
 
-			   SDL_strcpy(env_var, "SDL$");
-			   strcat(env_var, p);
-			   strcat(env_var, "$TaskName");
+			   SDL_strlcpy(env_var, "SDL$", len);
+			   SDL_strlcat(env_var, p, len);
+			   SDL_strlcat(env_var, "$TaskName", len);
 
 			   env_val = SDL_getenv(env_var);
-			   if (env_val) SDL_strncpy(task_name, env_val, 31);
+			   if (env_val) SDL_strlcpy(task_name, env_val, maxlen);
 
-			   SDL_strcpy(env_var, "SDL$");
-			   strcat(env_var, p);
-			   strcat(env_var, "$BackBuffer");
+			   SDL_strlcpy(env_var, "SDL$", len);
+			   SDL_strlcat(env_var, p, len);
+			   SDL_strlcat(env_var, "$BackBuffer", len);
 
 			   env_val = SDL_getenv(env_var);
 			   if (env_val) riscos_backbuffer = atoi(env_val);
 
-			   SDL_strcpy(env_var, "SDL$");
-			   strcat(env_var, p);
-			   strcat(env_var, "$CloseAction");
+			   SDL_strlcpy(env_var, "SDL$", len);
+			   SDL_strlcat(env_var, p, len);
+			   SDL_strlcat(env_var, "$CloseAction", len);
 
 			   env_val = SDL_getenv(env_var);
 			   if (env_val && SDL_strcmp(env_val,"0") == 0) riscos_closeaction = 0;
 
-			   SDL_free(env_var);
+			   SDL_stack_free(env_var);
 		   }
 		   
-		   if (task_name[0] == 0) SDL_strncpy(task_name, p, 31);
-		   task_name[31] = 0;
+		   if (!*task_name) SDL_strlcpy(task_name, p, maxlen);
 	   }
 
-	   SDL_free(buffer);
+	   SDL_stack_free(buffer);
    }
 
-   if (task_name[0] == 0) SDL_strcpy(task_name, "SDL Task");
+   if (task_name[0] == 0) SDL_strlcpy(task_name, "SDL Task", maxlen);
 
    return 1;
 }
--- a/src/video/riscos/SDL_wimpvideo.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/riscos/SDL_wimpvideo.c	Sun Feb 19 23:46:34 2006 +0000
@@ -384,8 +384,7 @@
 {
 	_kernel_swi_regs regs;
 
-	SDL_strncpy(this->hidden->title, title, 255);
-	this->hidden->title[255] = 0;
+	SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
 
 	if (RISCOS_GetWimpVersion() < 380)
 	{
--- a/src/video/svga/SDL_svgaevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/svga/SDL_svgaevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -210,7 +210,7 @@
 	int i;
 
 	/* Initialize the BeOS key translation table */
-	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
+	for ( i=0; i<SDL_arraysize(keymap); ++i )
 		keymap[i] = SDLK_UNKNOWN;
 
 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
--- a/src/video/vgl/SDL_vglevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/vgl/SDL_vglevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -143,7 +143,7 @@
 	int i;
 
 	/* Initialize the BeOS key translation table */
-	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
+	for ( i=0; i<SDL_arraysize(keymap); ++i )
 		keymap[i] = SDLK_UNKNOWN;
 
 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
--- a/src/video/wincommon/SDL_sysevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/wincommon/SDL_sysevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -702,13 +702,13 @@
 	if ( name ) {
 #ifdef _WIN32_WCE
 		/* WinCE uses the UNICODE version */
-		int nLen = SDL_strlen(name)+1;
+		size_t nLen = SDL_strlen(name)+1;
 		SDL_Appname = SDL_malloc(nLen*2);
 		MultiByteToWideChar(CP_ACP, 0, name, -1, SDL_Appname, nLen);
 #else
-		int nLen = SDL_strlen(name)+1;
+		size_t nLen = SDL_strlen(name)+1;
 		SDL_Appname = SDL_malloc(nLen);
-		SDL_strcpy(SDL_Appname, name);
+		SDL_strlcpy(SDL_Appname, name, nLen);
 #endif /* _WIN32_WCE */
 		SDL_Appstyle = style;
 		SDL_Instance = hInst ? hInst : SDL_GetModuleHandle();
--- a/src/video/wincommon/SDL_wingl.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/wincommon/SDL_wingl.c	Sun Feb 19 23:46:34 2006 +0000
@@ -551,7 +551,7 @@
 	}
 
 	this->gl_config.dll_handle = handle;
-	SDL_strcpy(this->gl_config.driver_path, path);
+	SDL_strlcpy(this->gl_config.driver_path, path, SDL_arraysize(this->gl_config.driver_path));
 	this->gl_config.driver_loaded = 1;
 	return 0;
 }
--- a/src/video/windib/SDL_dibevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/windib/SDL_dibevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -251,7 +251,7 @@
 	int i;
 
 	/* Map the VK keysyms */
-	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(VK_keymap); ++i )
 		VK_keymap[i] = SDLK_UNKNOWN;
 
 	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
--- a/src/video/windx5/SDL_dx5events.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/windx5/SDL_dx5events.c	Sun Feb 19 23:46:34 2006 +0000
@@ -703,7 +703,7 @@
 	int i;
 
 	/* Map the DIK scancodes to SDL keysyms */
-	for ( i=0; i<SDL_TABLESIZE(DIK_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(DIK_keymap); ++i )
 		DIK_keymap[i] = 0;
 
 	/* Defined DIK_* constants */
--- a/src/video/wscons/SDL_wsconsevents.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/wscons/SDL_wsconsevents.c	Sun Feb 19 23:46:34 2006 +0000
@@ -100,7 +100,7 @@
   keysym->sym = SDLK_UNKNOWN;
   keysym->mod = KMOD_NONE;
 
-  if (scancode < SDL_TABLESIZE(keymap))
+  if (scancode < SDL_arraysize(keymap))
     keysym->sym = keymap[scancode];
 
   if (keysym->sym == SDLK_UNKNOWN)
@@ -141,7 +141,7 @@
   int i;
 
   /* Make sure unknown keys are mapped correctly */
-  for (i=0; i < SDL_TABLESIZE(keymap); i++) {
+  for (i=0; i < SDL_arraysize(keymap); i++) {
     keymap[i] = SDLK_UNKNOWN;
   }
 
--- a/src/video/x11/SDL_x11events.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/x11/SDL_x11events.c	Sun Feb 19 23:46:34 2006 +0000
@@ -693,7 +693,7 @@
 	int i;
 
 	/* Odd keys used in international keyboards */
-	for ( i=0; i<SDL_TABLESIZE(ODD_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(ODD_keymap); ++i )
 		ODD_keymap[i] = SDLK_UNKNOWN;
 
  	/* Some of these might be mappable to an existing SDLK_ code */
@@ -729,7 +729,7 @@
 #endif
 
 	/* Map the miscellaneous keys */
-	for ( i=0; i<SDL_TABLESIZE(MISC_keymap); ++i )
+	for ( i=0; i<SDL_arraysize(MISC_keymap); ++i )
 		MISC_keymap[i] = SDLK_UNKNOWN;
 
 	/* These X keysyms have 0xFF as the high byte */
--- a/src/video/x11/SDL_x11gl.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/x11/SDL_x11gl.c	Sun Feb 19 23:46:34 2006 +0000
@@ -414,10 +414,10 @@
 	this->gl_config.dll_handle = handle;
 	this->gl_config.driver_loaded = 1;
 	if ( path ) {
-		SDL_strncpy(this->gl_config.driver_path, path,
-			sizeof(this->gl_config.driver_path)-1);
+		SDL_strlcpy(this->gl_config.driver_path, path,
+			SDL_arraysize(this->gl_config.driver_path));
 	} else {
-		SDL_strcpy(this->gl_config.driver_path, "");
+		*this->gl_config.driver_path = '\0';
 	}
 	return 0;
 }
--- a/src/video/x11/SDL_x11modes.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/x11/SDL_x11modes.c	Sun Feb 19 23:46:34 2006 +0000
@@ -493,7 +493,7 @@
         }
         this->hidden->nvisuals = 0;
         if ( ! add_visual_byid(this, SDL_getenv("SDL_VIDEO_X11_VISUALID")) ) {
-                for ( i=0; i<SDL_TABLESIZE(depth_list); ++i ) {
+                for ( i=0; i<SDL_arraysize(depth_list); ++i ) {
                         if ( depth_list[i] > 8 ) {
                                 if ( use_directcolor ) {
                                         add_visual(this, depth_list[i], DirectColor);
--- a/src/video/x11/SDL_x11mouse.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/x11/SDL_x11mouse.c	Sun Feb 19 23:46:34 2006 +0000
@@ -191,14 +191,16 @@
 static void SetMouseAccel(_THIS, const char *accel_param)
 {
 	int i;
+	size_t len;
 	int accel_value[3];
 	char *mouse_param, *mouse_param_buf, *pin;
 
-	mouse_param_buf = (char *)SDL_malloc(SDL_strlen(accel_param)+1);
+	len = SDL_strlen(accel_param)+1;
+	mouse_param_buf = SDL_stack_alloc(char, len);
 	if ( ! mouse_param_buf ) {
 		return;
 	}
-	SDL_strcpy(mouse_param_buf, accel_param);
+	SDL_strlcpy(mouse_param_buf, accel_param, len);
 	mouse_param = mouse_param_buf;
 
 	for ( i=0; (i < 3) && mouse_param; ++i ) {
--- a/src/video/x11/SDL_x11video.c	Sun Feb 19 23:38:57 2006 +0000
+++ b/src/video/x11/SDL_x11video.c	Sun Feb 19 23:46:34 2006 +0000
@@ -275,7 +275,7 @@
 	/* First allow environment variable override */
 	spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
 	if ( spot ) {
-		SDL_strncpy(classname, spot, maxlen);
+		SDL_strlcpy(classname, spot, maxlen);
 		return classname;
 	}
 
@@ -293,16 +293,16 @@
 		linkfile[linksize] = '\0';
 		spot = SDL_strrchr(linkfile, '/');
 		if ( spot ) {
-			SDL_strncpy(classname, spot+1, maxlen);
+			SDL_strlcpy(classname, spot+1, maxlen);
 		} else {
-			SDL_strncpy(classname, linkfile, maxlen);
+			SDL_strlcpy(classname, linkfile, maxlen);
 		}
 		return classname;
 	}
 #endif /* linux */
 
 	/* Finally use the default we've used forever */
-	SDL_strncpy(classname, "SDL_App", maxlen);
+	SDL_strlcpy(classname, "SDL_App", maxlen);
 	return classname;
 }