Skip to content
Permalink
Browse files

backport fix for bug #2049:

Make SDL error string formatting deal with nasty corner cases.
Ryan C. Gordon <icculus@icculus.org>
  • Loading branch information
sezero committed Mar 24, 2018
1 parent 6d2ff22 commit 93b0beaee50099b9ef45295cd6ea5d4ef7bf976d
Showing with 23 additions and 9 deletions.
  1. +23 −9 src/SDL_error.c
@@ -115,7 +115,7 @@ void SDL_SetError (const char *fmt, ...)
/* This function has a bit more overhead than most error functions
so that it supports internationalization and thread-safe errors.
*/
char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
char *SDL_GetErrorMsg(char *errstr, int maxlen)
{
SDL_error *error;

@@ -154,30 +154,44 @@ char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
case 'x':
case 'X':
len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
msg += len;
maxlen -= len;
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
case 'f':
len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
msg += len;
maxlen -= len;
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
case 'p':
len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
msg += len;
maxlen -= len;
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
case 's':
len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
msg += len;
maxlen -= len;
if (len > 0) {
msg += len;
maxlen -= len;
}
break;
}
} else {
*msg++ = *fmt++;
maxlen -= 1;
}
}

/* slide back if we've overshot the end of our buffer. */
if (maxlen < 0) {
msg -= (-maxlen) + 1;
}

*msg = 0; /* NULL terminate the string */
}
return(errstr);

0 comments on commit 93b0bea

Please sign in to comment.