make sure SDL_vsnprintf() nul terminates if it is using _vsnprintf
authorOzkan Sezer <sezeroz@gmail.com>
Thu, 10 May 2018 09:02:39 +0300
changeset 119805e1341f8c467
parent 11961 a9203ed58516
child 11981 9e46f3dd75fd
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.
src/stdlib/SDL_string.c
     1.1 --- a/src/stdlib/SDL_string.c	Thu May 10 08:28:00 2018 +0300
     1.2 +++ b/src/stdlib/SDL_string.c	Thu May 10 09:02:39 2018 +0300
     1.3 @@ -1319,7 +1319,18 @@
     1.4      return retval;
     1.5  }
     1.6  
     1.7 -#ifdef HAVE_VSNPRINTF
     1.8 +#if defined(HAVE_LIBC) && defined(__WATCOMC__)
     1.9 +/* _vsnprintf() doesn't ensure nul termination */
    1.10 +int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
    1.11 +{
    1.12 +    int retval;
    1.13 +    if (!fmt) fmt = "";
    1.14 +    retval = _vsnprintf(text, maxlen, fmt, ap);
    1.15 +    if (maxlen > 0) text[maxlen-1] = '\0';
    1.16 +    if (retval < 0) retval = (int) maxlen;
    1.17 +    return retval;
    1.18 +}
    1.19 +#elif defined(HAVE_VSNPRINTF)
    1.20  int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
    1.21  {
    1.22      if (!fmt) {