summaryrefslogtreecommitdiff
path: root/src/stdlib
diff options
context:
space:
mode:
authorOzkan Sezer <sezeroz@gmail.com>2018-05-10 09:02:39 +0300
committerOzkan Sezer <sezeroz@gmail.com>2018-05-10 09:02:39 +0300
commit948b038acff1dc300734d9d5b1dc1c6dfb73be08 (patch)
tree04de6125d2ba7727311ca6bfa9be7a63c0266e05 /src/stdlib
parentd50653152c51dbf4c4502c3827195a238e63a874 (diff)
downloadsdl-948b038acff1dc300734d9d5b1dc1c6dfb73be08.tar.gz
make sure SDL_vsnprintf() nul terminates if it is using _vsnprintf
The change makes sure that SDL_vsnprintf() nul terminates if it is using _vsnprintf() for the job. I made this patch for Watcom, whose _vsnprintf() doesn't guarantee nul termination. The preprocessor check can be extended to windows in general too, if required. Closes bug #3769.
Diffstat (limited to 'src/stdlib')
-rw-r--r--src/stdlib/SDL_string.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 444ae6d52..5cbcc4bba 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -1319,7 +1319,18 @@ SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_
return retval;
}
-#ifdef HAVE_VSNPRINTF
+#if defined(HAVE_LIBC) && defined(__WATCOMC__)
+/* _vsnprintf() doesn't ensure nul termination */
+int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
+{
+ int retval;
+ if (!fmt) fmt = "";
+ 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)
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
{
if (!fmt) {