From 93b0beaee50099b9ef45295cd6ea5d4ef7bf976d Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Sat, 24 Mar 2018 22:39:46 +0300 Subject: [PATCH] backport fix for bug #2049: Make SDL error string formatting deal with nasty corner cases. Ryan C. Gordon --- src/SDL_error.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/SDL_error.c b/src/SDL_error.c index 0f06bd53a..82fd69f7e 100644 --- a/src/SDL_error.c +++ b/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,23 +154,31 @@ 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 { @@ -178,6 +186,12 @@ char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen) 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);