From ad155c765b4c0c8df9555ebde908301a24d408db Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Sun, 1 Jul 2018 09:50:47 +0300 Subject: [PATCH] make sure SDL_vsnprintf() nul terminates if it is using _vsnprintf (bug #3769: backported commit 5e1341f8c467 for windows and watcom.) --- include/SDL_stdinc.h | 4 ++-- src/stdlib/SDL_string.c | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index d6d29fb06..0df464784 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -575,13 +575,13 @@ extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...); #endif -#ifdef HAVE_SNPRINTF +#if defined(HAVE_SNPRINTF) && !(defined(__WATCOMC__) || defined(_WIN32)) #define SDL_snprintf snprintf #else extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...); #endif -#ifdef HAVE_VSNPRINTF +#if defined(HAVE_VSNPRINTF) && !(defined(__WATCOMC__) || defined(_WIN32)) #define SDL_vsnprintf vsnprintf #else extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap); diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c index 25f1e6983..77bf2eba9 100644 --- a/src/stdlib/SDL_string.c +++ b/src/stdlib/SDL_string.c @@ -1013,7 +1013,7 @@ int SDL_sscanf(const char *text, const char *fmt, ...) } #endif -#ifndef HAVE_SNPRINTF +#if defined(__WATCOMC__) || defined(_WIN32) || !defined(HAVE_SNPRINTF) int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...) { va_list ap; @@ -1027,7 +1027,15 @@ int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...) } #endif -#ifndef HAVE_VSNPRINTF +#if (defined(__WATCOMC__) || defined(_WIN32)) && defined(HAVE_LIBC) +int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) +{ + int retval = _vsnprintf(text, maxlen, fmt, ap); + if (maxlen > 0) text[maxlen-1] = '\0'; + if (retval < 0) retval = (int) maxlen; + return retval; +} +#elif !defined(HAVE_VSNPRINTF) static size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen) { char num[130];