backport fix for bug #2049: SDL-1.2
authorOzkan Sezer <sezeroz@gmail.com>
Sat, 24 Mar 2018 22:39:46 +0300
branchSDL-1.2
changeset 1197170dd6a38cd76
parent 11970 4b06648ea2d9
child 11972 0b5ac2b90ab1
backport fix for bug #2049:

Make SDL error string formatting deal with nasty corner cases.
Ryan C. Gordon <icculus@icculus.org>
src/SDL_error.c
     1.1 --- a/src/SDL_error.c	Sat Mar 24 22:36:38 2018 +0300
     1.2 +++ b/src/SDL_error.c	Sat Mar 24 22:39:46 2018 +0300
     1.3 @@ -115,7 +115,7 @@
     1.4  /* This function has a bit more overhead than most error functions
     1.5     so that it supports internationalization and thread-safe errors.
     1.6  */
     1.7 -char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
     1.8 +char *SDL_GetErrorMsg(char *errstr, int maxlen)
     1.9  {
    1.10  	SDL_error *error;
    1.11  
    1.12 @@ -154,23 +154,31 @@
    1.13  				    case 'x':
    1.14  				    case 'X':
    1.15  					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
    1.16 -					msg += len;
    1.17 -					maxlen -= len;
    1.18 +					if (len > 0) {
    1.19 +						msg += len;
    1.20 +						maxlen -= len;
    1.21 +					}
    1.22  					break;
    1.23  				    case 'f':
    1.24  					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
    1.25 -					msg += len;
    1.26 -					maxlen -= len;
    1.27 +					if (len > 0) {
    1.28 +						msg += len;
    1.29 +						maxlen -= len;
    1.30 +					}
    1.31  					break;
    1.32  				    case 'p':
    1.33  					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
    1.34 -					msg += len;
    1.35 -					maxlen -= len;
    1.36 +					if (len > 0) {
    1.37 +						msg += len;
    1.38 +						maxlen -= len;
    1.39 +					}
    1.40  					break;
    1.41  				    case 's':
    1.42  					len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
    1.43 -					msg += len;
    1.44 -					maxlen -= len;
    1.45 +					if (len > 0) {
    1.46 +						msg += len;
    1.47 +						maxlen -= len;
    1.48 +					}
    1.49  					break;
    1.50  				}
    1.51  			} else {
    1.52 @@ -178,6 +186,12 @@
    1.53  				maxlen -= 1;
    1.54  			}
    1.55  		}
    1.56 +
    1.57 +		/* slide back if we've overshot the end of our buffer. */
    1.58 +		if (maxlen < 0) {
    1.59 +			msg -= (-maxlen) + 1;
    1.60 +		}
    1.61 +
    1.62  		*msg = 0;	/* NULL terminate the string */
    1.63  	}
    1.64  	return(errstr);