From d4ffadffcb874e95f81de9b74af52fa974af32cb Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 19 Feb 2006 23:46:34 +0000 Subject: [PATCH] Use only safe string functions --- configure.in | 2 +- include/SDL_config.h.in | 6 +-- include/SDL_stdinc.h | 27 ++++-------- src/SDL_error.c | 9 ++-- src/audio/SDL_audio.c | 3 +- src/audio/SDL_audiodev.c | 7 ++- src/audio/alsa/SDL_alsa_audio.c | 2 +- src/audio/arts/SDL_artsaudio.c | 2 +- src/audio/esd/SDL_esdaudio.c | 2 +- src/cdrom/aix/SDL_syscdrom.c | 20 +++++---- src/cdrom/bsdi/SDL_syscdrom.c | 10 ++--- src/cdrom/freebsd/SDL_syscdrom.c | 10 ++--- src/cdrom/linux/SDL_syscdrom.c | 28 ++++++------ src/cdrom/openbsd/SDL_syscdrom.c | 10 ++--- src/cdrom/os2/SDL_syscdrom.c | 3 +- src/cdrom/osf/SDL_syscdrom.c | 20 +++------ src/cdrom/qnx/SDL_syscdrom.c | 11 +++-- src/cdrom/win32/SDL_syscdrom.c | 3 +- src/joystick/SDL_joystick.c | 4 +- src/joystick/linux/SDL_sysjoystick.c | 26 +++-------- src/joystick/macos/SDL_sysjoystick.c | 2 +- src/joystick/os2/SDL_sysjoystick.c | 2 +- src/loadso/dlopen/SDL_sysloadso.c | 5 ++- src/loadso/macos/SDL_sysloadso.c | 6 +-- src/loadso/macosx/SDL_sysloadso.c | 23 +++++----- src/loadso/win32/SDL_sysloadso.c | 8 ++-- src/main/win32/SDL_win32_main.c | 31 ++++++++------ src/stdlib/SDL_getenv.c | 5 +-- src/stdlib/SDL_string.c | 45 ++++++++++---------- src/video/SDL_video.c | 13 ++---- src/video/Xext/Xv/Xv.c | 6 +-- src/video/Xext/extensions/extutil.h | 4 +- src/video/aalib/SDL_aaevents.c | 2 +- src/video/ataricommon/SDL_atarigl.c | 6 +-- src/video/cybergfx/SDL_amigaevents.c | 2 +- src/video/directfb/SDL_DirectFB_events.c | 2 +- src/video/fbcon/SDL_fbevents.c | 4 +- src/video/ggi/SDL_ggievents.c | 2 +- src/video/maccommon/SDL_macevents.c | 2 +- src/video/macdsp/SDL_dspvideo.c | 2 +- src/video/nanox/SDL_nxevents.c | 2 +- src/video/photon/SDL_ph_events.c | 4 +- src/video/photon/SDL_ph_gl.c | 2 +- src/video/ps2gs/SDL_gsevents.c | 4 +- src/video/riscos/SDL_riscosFullScreenVideo.c | 3 +- src/video/riscos/SDL_riscosevents.c | 2 +- src/video/riscos/SDL_riscostask.c | 43 ++++++++++--------- src/video/riscos/SDL_wimpvideo.c | 3 +- src/video/svga/SDL_svgaevents.c | 2 +- src/video/vgl/SDL_vglevents.c | 2 +- src/video/wincommon/SDL_sysevents.c | 6 +-- src/video/wincommon/SDL_wingl.c | 2 +- src/video/windib/SDL_dibevents.c | 2 +- src/video/windx5/SDL_dx5events.c | 2 +- src/video/wscons/SDL_wsconsevents.c | 4 +- src/video/x11/SDL_x11events.c | 4 +- src/video/x11/SDL_x11gl.c | 6 +-- src/video/x11/SDL_x11modes.c | 2 +- src/video/x11/SDL_x11mouse.c | 6 ++- src/video/x11/SDL_x11video.c | 8 ++-- 60 files changed, 223 insertions(+), 263 deletions(-) diff --git a/configure.in b/configure.in index c324bf220..0d256cb3e 100644 --- a/configure.in +++ b/configure.in @@ -119,7 +119,7 @@ if test x$enable_libc = xyes; then 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 diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in index e2d904ab5..e6b0adafa 100644 --- a/include/SDL_config.h.in +++ b/include/SDL_config.h.in @@ -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 diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index 090433d65..8dbbdc2ab 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -206,6 +206,9 @@ extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, #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 int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t le extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string); #endif -#if HAVE_STRCPY -#define SDL_strcpy strcpy -#else -extern DECLSPEC char * SDLCALL SDL_strcpy(char *dst, const char *src); -#endif - -#if HAVE_STRNCPY -#define SDL_strncpy strncpy -#else -extern DECLSPEC char * SDLCALL SDL_strncpy(char *dst, const char *src, size_t maxlen); -#endif - -#if HAVE_STRCAT -#define SDL_strcat strcat +#if HAVE_STRLCPY +#define SDL_strlcpy strlcpy #else -#define SDL_strcat(dst, src) (SDL_strcpy(dst+SDL_strlen(dst), src), dst) +extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen); #endif -#if HAVE_STRNCAT -#define SDL_strncat strncat +#if HAVE_STRLCAT +#define SDL_strlcat strlcat #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 diff --git a/src/SDL_error.c b/src/SDL_error.c index ea59aeb5e..4a77c42c5 100644 --- a/src/SDL_error.c +++ b/src/SDL_error.c @@ -59,8 +59,7 @@ void SDL_SetError (const char *fmt, ...) /* 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 @@ void SDL_SetError (const char *fmt, ...) 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 @@ Uint8 *SDL_GetErrorMsg(Uint8 *errstr, unsigned int maxlen) /* 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); } diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index 3fa5b12de..9758bc346 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -373,8 +373,7 @@ int SDL_AudioInit(const char *driver_name) 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); diff --git a/src/audio/SDL_audiodev.c b/src/audio/SDL_audiodev.c index 59db59197..1d4c5096c 100644 --- a/src/audio/SDL_audiodev.c +++ b/src/audio/SDL_audiodev.c @@ -89,7 +89,7 @@ int SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic) 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 @@ static int OpenUserDefinedDevice(char *path, int maxlen, int flags) } 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 @@ int SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic) 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; } diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c index fc091f570..98e719539 100644 --- a/src/audio/alsa/SDL_alsa_audio.c +++ b/src/audio/alsa/SDL_alsa_audio.c @@ -124,7 +124,7 @@ static int LoadALSALibrary(void) { 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"); diff --git a/src/audio/arts/SDL_artsaudio.c b/src/audio/arts/SDL_artsaudio.c index b4e950823..240ac4e4b 100644 --- a/src/audio/arts/SDL_artsaudio.c +++ b/src/audio/arts/SDL_artsaudio.c @@ -90,7 +90,7 @@ static int LoadARTSLibrary(void) if ( arts_handle ) { arts_loaded = 1; retval = 0; - for ( i=0; ist_rdev; ++SDL_numcds; #ifdef DEBUG_CDROM @@ -302,9 +301,10 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) 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 @@ static int SDL_SYS_CDOpen(int drive) 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); } } diff --git a/src/cdrom/bsdi/SDL_syscdrom.c b/src/cdrom/bsdi/SDL_syscdrom.c index 0eb35e7da..3872851e7 100644 --- a/src/cdrom/bsdi/SDL_syscdrom.c +++ b/src/cdrom/bsdi/SDL_syscdrom.c @@ -224,12 +224,11 @@ static void AddDrive(char *drive, struct stat *stbuf) /* 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 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) SDLcdrom = NULL; } } while ( SDLcdrom ); - SDL_free(cdpath); + SDL_stack_free(cdpath); } /* If we found our drives, there's nothing left to do */ diff --git a/src/cdrom/freebsd/SDL_syscdrom.c b/src/cdrom/freebsd/SDL_syscdrom.c index b6cde3fc2..badebf48c 100644 --- a/src/cdrom/freebsd/SDL_syscdrom.c +++ b/src/cdrom/freebsd/SDL_syscdrom.c @@ -110,12 +110,11 @@ static void AddDrive(char *drive, struct stat *stbuf) /* 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 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) SDLcdrom = NULL; } } while ( SDLcdrom ); - SDL_free(cdpath); + SDL_stack_free(cdpath); } /* If we found our drives, there's nothing left to do */ diff --git a/src/cdrom/linux/SDL_syscdrom.c b/src/cdrom/linux/SDL_syscdrom.c index d00f40d84..5d2db0656 100644 --- a/src/cdrom/linux/SDL_syscdrom.c +++ b/src/cdrom/linux/SDL_syscdrom.c @@ -167,12 +167,11 @@ static void AddDrive(char *drive, struct stat *stbuf) /* 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 @@ static void CheckMounts(const char *mtab) 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 @@ static void CheckMounts(const char *mtab) 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 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) SDLcdrom = NULL; } } while ( SDLcdrom ); - SDL_free(cdpath); + SDL_stack_free(cdpath); } /* If we found our drives, there's nothing left to do */ diff --git a/src/cdrom/openbsd/SDL_syscdrom.c b/src/cdrom/openbsd/SDL_syscdrom.c index 21353283a..8c28b0ce8 100644 --- a/src/cdrom/openbsd/SDL_syscdrom.c +++ b/src/cdrom/openbsd/SDL_syscdrom.c @@ -115,12 +115,11 @@ static void AddDrive(char *drive, struct stat *stbuf) /* 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 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) SDLcdrom = NULL; } } while ( SDLcdrom ); - SDL_free(cdpath); + SDL_stack_free(cdpath); } /* If we found our drives, there's nothing left to do */ diff --git a/src/cdrom/os2/SDL_syscdrom.c b/src/cdrom/os2/SDL_syscdrom.c index 900381af9..eb8595ad2 100644 --- a/src/cdrom/os2/SDL_syscdrom.c +++ b/src/cdrom/os2/SDL_syscdrom.c @@ -94,13 +94,12 @@ for (i=0; ist_rdev; ++SDL_numcds; #ifdef DEBUG_CDROM @@ -174,9 +172,10 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) #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); } diff --git a/src/cdrom/qnx/SDL_syscdrom.c b/src/cdrom/qnx/SDL_syscdrom.c index 1a5d52107..df74e2e6e 100644 --- a/src/cdrom/qnx/SDL_syscdrom.c +++ b/src/cdrom/qnx/SDL_syscdrom.c @@ -124,13 +124,12 @@ static void AddDrive(char *drive, struct stat *stbuf) /* 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 @@ int SDL_SYS_CDInit(void) 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 @@ int SDL_SYS_CDInit(void) SDLcdrom = NULL; } } while (SDLcdrom); - SDL_free(cdpath); + SDL_stack_free(cdpath); } /* If we found our drives, there's nothing left to do */ diff --git a/src/cdrom/win32/SDL_syscdrom.c b/src/cdrom/win32/SDL_syscdrom.c index 6c1ea2203..9242f5c48 100644 --- a/src/cdrom/win32/SDL_syscdrom.c +++ b/src/cdrom/win32/SDL_syscdrom.c @@ -63,12 +63,11 @@ static void AddDrive(char *drive) 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); diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index aed1e1620..1d6b01e70 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -554,7 +554,7 @@ int SDL_JoystickEventState(int state) switch (state) { case SDL_QUERY: state = SDL_IGNORE; - for ( i=0; i= 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 @@ int SDL_SYS_JoystickInit(void) } } - for ( i=0; inaxes = special_joysticks[i].naxes; @@ -503,7 +491,7 @@ static SDL_bool JS_ConfigJoystick(SDL_Joystick *joystick, int fd) /* 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 ) diff --git a/src/joystick/macos/SDL_sysjoystick.c b/src/joystick/macos/SDL_sysjoystick.c index eb605b200..357f2edb6 100644 --- a/src/joystick/macos/SDL_sysjoystick.c +++ b/src/joystick/macos/SDL_sysjoystick.c @@ -165,7 +165,7 @@ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) 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( diff --git a/src/joystick/os2/SDL_sysjoystick.c b/src/joystick/os2/SDL_sysjoystick.c index 6ceb0d5fb..9d7f7ce1c 100644 --- a/src/joystick/os2/SDL_sysjoystick.c +++ b/src/joystick/os2/SDL_sysjoystick.c @@ -214,7 +214,7 @@ if (numdevs > 0) 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 diff --git a/src/loadso/dlopen/SDL_sysloadso.c b/src/loadso/dlopen/SDL_sysloadso.c index ba2072076..3a084adda 100644 --- a/src/loadso/dlopen/SDL_sysloadso.c +++ b/src/loadso/dlopen/SDL_sysloadso.c @@ -42,9 +42,10 @@ void *SDL_LoadFunction(void *handle, const char *name) { 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 ) { diff --git a/src/loadso/macos/SDL_sysloadso.c b/src/loadso/macos/SDL_sysloadso.c index ccd5d1c1d..be65a54a7 100644 --- a/src/loadso/macos/SDL_sysloadso.c +++ b/src/loadso/macos/SDL_sysloadso.c @@ -42,8 +42,7 @@ void *SDL_LoadObject(const char *sofile) 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 @@ void *SDL_LoadFunction(void *handle, const char *name) 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"; diff --git a/src/loadso/macosx/SDL_sysloadso.c b/src/loadso/macosx/SDL_sysloadso.c index 08b12a539..16f49a3c2 100644 --- a/src/loadso/macosx/SDL_sysloadso.c +++ b/src/loadso/macosx/SDL_sysloadso.c @@ -281,7 +281,7 @@ static void error(const char *str, ...) 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 @@ static NSSymbol *search_linked_libs(const struct mach_header * mh, const char *s } /* 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 @@ static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetErr 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 @@ static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetErr 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 @@ static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetErr 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 @@ static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetErr debug(savedErrorStr); } if (savedErrorStr) - SDL_free((char*)savedErrorStr); + SDL_free(savedErrorStr); return NULL; } return NSAddressOfSymbol(nssym); diff --git a/src/loadso/win32/SDL_sysloadso.c b/src/loadso/win32/SDL_sysloadso.c index 82ac900db..4001f16c1 100644 --- a/src/loadso/win32/SDL_sysloadso.c +++ b/src/loadso/win32/SDL_sysloadso.c @@ -47,7 +47,7 @@ void *SDL_LoadObject(const char *sofile) 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 @@ void *SDL_LoadObject(const char *sofile) 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 @@ void *SDL_LoadFunction(void *handle, const char *name) 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 @@ void *SDL_LoadFunction(void *handle, const char *name) FORMAT_MESSAGE_FROM_SYSTEM), NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - errbuf, SDL_TABLESIZE(errbuf), NULL); + errbuf, SDL_arraysize(errbuf), NULL); loaderror = errbuf; } #endif diff --git a/src/main/win32/SDL_win32_main.c b/src/main/win32/SDL_win32_main.c index 25897fc39..ba9e0206b 100644 --- a/src/main/win32/SDL_win32_main.c +++ b/src/main/win32/SDL_win32_main.c @@ -18,7 +18,6 @@ # define fopen _wfopen # define freopen _wfreopen # define remove(x) DeleteFile(x) -# define strcat wcscat #else # define DIR_SEPERATOR TEXT("/") # include @@ -208,8 +207,7 @@ int console_main(int argc, char *argv[]) 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 WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 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 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) } #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 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) } #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 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 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 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) #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; } diff --git a/src/stdlib/SDL_getenv.c b/src/stdlib/SDL_getenv.c index 4ac0aa869..23f5c6bc8 100644 --- a/src/stdlib/SDL_getenv.c +++ b/src/stdlib/SDL_getenv.c @@ -53,7 +53,7 @@ int SDL_putenv(const char *variable) 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 @@ int SDL_putenv(const char *variable) } /* 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; diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c index 4c82583ca..3a2a9b890 100644 --- a/src/stdlib/SDL_string.c +++ b/src/stdlib/SDL_string.c @@ -294,39 +294,38 @@ size_t SDL_strlen(const char *string) } #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 @@ static size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen) if ( size > maxlen ) { size = maxlen; } - SDL_strncpy(text, num, size); + SDL_strlcpy(text, num, size); return size; } @@ -926,7 +925,7 @@ static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, if ( size > maxlen ) { size = maxlen; } - SDL_strncpy(text, num, size); + SDL_strlcpy(text, num, size); return size; } @@ -941,7 +940,7 @@ static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxl if ( size > maxlen ) { size = maxlen; } - SDL_strncpy(text, num, size); + SDL_strlcpy(text, num, size); return size; } @@ -955,7 +954,7 @@ static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, siz if ( size > maxlen ) { size = maxlen; } - SDL_strncpy(text, num, size); + SDL_strlcpy(text, num, size); return size; } diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 68d95cbb3..98417c2e2 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -283,8 +283,7 @@ int SDL_VideoInit (const char *driver_name, Uint32 flags) 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 @@ void SDL_WM_SetCaption (const char *title, const char *icon) 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); diff --git a/src/video/Xext/Xv/Xv.c b/src/video/Xext/Xv/Xv.c index 6b7743f79..bf6e5c77a 100644 --- a/src/video/Xext/Xv/Xv.c +++ b/src/video/Xext/Xv/Xv.c @@ -234,8 +234,7 @@ SDL_NAME(XvQueryAdaptors)( 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 @@ SDL_NAME(XvQueryEncodings)( SyncHandle(); return(XvBadAlloc); } - strncpy(name, u.string, size); - name[size] = '\0'; + SDL_strlcpy(name, u.string, size); pe->name = name; pe++; diff --git a/src/video/Xext/extensions/extutil.h b/src/video/Xext/extensions/extutil.h index dc9963056..ab8dd13a5 100644 --- a/src/video/Xext/extensions/extutil.h +++ b/src/video/Xext/extensions/extutil.h @@ -36,6 +36,8 @@ in this Software without prior written authorization from The Open Group. #ifndef _EXTUTIL_H_ #define _EXTUTIL_H_ +#include "SDL_stdinc.h" /* For portable string functions */ + #include /* @@ -212,7 +214,7 @@ char *proc (Display *dpy, int code, XExtCodes *codes, char *buf, int n) \ 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; \ } \ diff --git a/src/video/aalib/SDL_aaevents.c b/src/video/aalib/SDL_aaevents.c index d02de8c19..5b71c7330 100644 --- a/src/video/aalib/SDL_aaevents.c +++ b/src/video/aalib/SDL_aaevents.c @@ -122,7 +122,7 @@ void AA_InitOSKeymap(_THIS) const char *std; /* Initialize the AAlib key translation table */ - for ( i=0; igl_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 diff --git a/src/video/cybergfx/SDL_amigaevents.c b/src/video/cybergfx/SDL_amigaevents.c index 9de59d69f..280e0f570 100644 --- a/src/video/cybergfx/SDL_amigaevents.c +++ b/src/video/cybergfx/SDL_amigaevents.c @@ -301,7 +301,7 @@ void amiga_InitKeymap(void) int i; /* Map the miscellaneous keys */ - for ( i=0; igl_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; } diff --git a/src/video/ps2gs/SDL_gsevents.c b/src/video/ps2gs/SDL_gsevents.c index 89b5b89f5..98cb92e11 100644 --- a/src/video/ps2gs/SDL_gsevents.c +++ b/src/video/ps2gs/SDL_gsevents.c @@ -822,7 +822,7 @@ void GS_InitOSKeymap(_THIS) /* Initialize the Linux key translation table */ /* First get the ascii keys and others not well handled */ - for (i=0; ihidden->title, title, 255); - this->hidden->title[255] = 0; + SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title)); } /* Set screen mode diff --git a/src/video/riscos/SDL_riscosevents.c b/src/video/riscos/SDL_riscosevents.c index ed76b9a76..1ab39237a 100644 --- a/src/video/riscos/SDL_riscosevents.c +++ b/src/video/riscos/SDL_riscosevents.c @@ -101,7 +101,7 @@ void RISCOS_InitOSKeymap(_THIS) int i; /* Map the VK keysyms */ - for ( i=0; i$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; } diff --git a/src/video/riscos/SDL_wimpvideo.c b/src/video/riscos/SDL_wimpvideo.c index 7d62ca737..99d7f6be4 100644 --- a/src/video/riscos/SDL_wimpvideo.c +++ b/src/video/riscos/SDL_wimpvideo.c @@ -384,8 +384,7 @@ void WIMP_SetWMCaption(_THIS, const char *title, const char *icon) { _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) { diff --git a/src/video/svga/SDL_svgaevents.c b/src/video/svga/SDL_svgaevents.c index 4ed97b16f..43f1f17ff 100644 --- a/src/video/svga/SDL_svgaevents.c +++ b/src/video/svga/SDL_svgaevents.c @@ -210,7 +210,7 @@ void SVGA_InitOSKeymap(_THIS) int i; /* Initialize the BeOS key translation table */ - for ( i=0; igl_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; } diff --git a/src/video/windib/SDL_dibevents.c b/src/video/windib/SDL_dibevents.c index 1b73b214f..b8e6d73a9 100644 --- a/src/video/windib/SDL_dibevents.c +++ b/src/video/windib/SDL_dibevents.c @@ -251,7 +251,7 @@ void DIB_InitOSKeymap(_THIS) int i; /* Map the VK keysyms */ - for ( i=0; isym = 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 @@ void WSCONS_InitOSKeymap(_THIS) 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; } diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index efbd576a5..8a7774ce5 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -693,7 +693,7 @@ void X11_InitKeymap(void) int i; /* Odd keys used in international keyboards */ - for ( i=0; igl_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; } diff --git a/src/video/x11/SDL_x11modes.c b/src/video/x11/SDL_x11modes.c index 266ac2db9..a52234a1b 100644 --- a/src/video/x11/SDL_x11modes.c +++ b/src/video/x11/SDL_x11modes.c @@ -493,7 +493,7 @@ int X11_GetVideoModes(_THIS) } this->hidden->nvisuals = 0; if ( ! add_visual_byid(this, SDL_getenv("SDL_VIDEO_X11_VISUALID")) ) { - for ( i=0; i 8 ) { if ( use_directcolor ) { add_visual(this, depth_list[i], DirectColor); diff --git a/src/video/x11/SDL_x11mouse.c b/src/video/x11/SDL_x11mouse.c index 41887eb98..9d25e14d8 100644 --- a/src/video/x11/SDL_x11mouse.c +++ b/src/video/x11/SDL_x11mouse.c @@ -191,14 +191,16 @@ void X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y) 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 ) { diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c index b2c1f45bd..a32419b7e 100644 --- a/src/video/x11/SDL_x11video.c +++ b/src/video/x11/SDL_x11video.c @@ -275,7 +275,7 @@ static char *get_classname(char *classname, int maxlen) /* 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 @@ static char *get_classname(char *classname, int maxlen) 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; }