src/SDL_error.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 17 Nov 2005 03:04:47 +0000
changeset 1172 f69f4d25fb20
parent 769 b8d311d90021
child 1312 c9b51268668f
permissions -rw-r--r--
Don't crash if a NULL is passed for a "%s" parameter to SDL_SetError(),
instead replace it with the string "(null)", like glibc's printf() would do.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Simple error handling in SDL */
slouken@0
    29
slouken@0
    30
#include <stdio.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <stdarg.h>
slouken@0
    33
#include <string.h>
slouken@0
    34
slouken@0
    35
#include "SDL_types.h"
slouken@0
    36
#include "SDL_getenv.h"
slouken@0
    37
#include "SDL_error.h"
slouken@0
    38
#include "SDL_error_c.h"
slouken@0
    39
#ifndef DISABLE_THREADS
slouken@0
    40
#include "SDL_thread_c.h"
slouken@0
    41
#endif
slouken@0
    42
slouken@0
    43
#ifdef DISABLE_THREADS
slouken@0
    44
/* The default (non-thread-safe) global error variable */
slouken@0
    45
static SDL_error SDL_global_error;
slouken@0
    46
slouken@0
    47
#define SDL_GetErrBuf()	(&SDL_global_error)
slouken@0
    48
#endif /* DISABLE_THREADS */
slouken@0
    49
slouken@1
    50
#ifdef __CYGWIN__
slouken@1
    51
#define DISABLE_STDIO
slouken@1
    52
#endif
slouken@1
    53
slouken@0
    54
#define SDL_ERRBUFIZE	1024
slouken@0
    55
slouken@0
    56
/* Private functions */
slouken@0
    57
slouken@0
    58
static void SDL_LookupString(const Uint8 *key, Uint16 *buf, int buflen)
slouken@0
    59
{
slouken@0
    60
	/* FIXME: Add code to lookup key in language string hash-table */
slouken@0
    61
slouken@0
    62
	/* Key not found in language string hash-table */
slouken@0
    63
	while ( *key && (--buflen > 0) ) {
slouken@0
    64
		*buf++ = *key++;
slouken@0
    65
	}
slouken@0
    66
	*buf = 0;	/* NULL terminate string */
slouken@0
    67
}
slouken@0
    68
slouken@0
    69
/* Public functions */
slouken@0
    70
slouken@0
    71
void SDL_SetError (const char *fmt, ...)
slouken@0
    72
{
slouken@0
    73
	va_list ap;
slouken@0
    74
	SDL_error *error;
slouken@0
    75
slouken@0
    76
	/* Copy in the key, mark error as valid */
slouken@0
    77
	error = SDL_GetErrBuf();
slouken@0
    78
	error->error = 1;
slouken@0
    79
	strncpy((char *)error->key, fmt, sizeof(error->key));
slouken@0
    80
	error->key[sizeof(error->key)-1] = '\0';
slouken@0
    81
slouken@0
    82
	va_start(ap, fmt);
slouken@0
    83
	error->argc = 0;
slouken@0
    84
	while ( *fmt ) {
slouken@0
    85
		if ( *fmt++ == '%' ) {
slouken@0
    86
			switch (*fmt++) {
slouken@0
    87
			    case 0:  /* Malformed format string.. */
slouken@0
    88
				--fmt;
slouken@0
    89
				break;
slouken@0
    90
#if 0	/* What is a character anyway?  (UNICODE issues) */
slouken@0
    91
			    case 'c':
slouken@0
    92
				error->args[error->argc++].value_c =
slouken@0
    93
						va_arg(ap, unsigned char);
slouken@0
    94
				break;
slouken@0
    95
#endif
slouken@0
    96
			    case 'd':
slouken@0
    97
				error->args[error->argc++].value_i =
slouken@0
    98
							va_arg(ap, int);
slouken@0
    99
				break;
slouken@0
   100
			    case 'f':
slouken@0
   101
				error->args[error->argc++].value_f =
slouken@0
   102
							va_arg(ap, double);
slouken@0
   103
				break;
slouken@0
   104
			    case 'p':
slouken@0
   105
				error->args[error->argc++].value_ptr =
slouken@0
   106
							va_arg(ap, void *);
slouken@0
   107
				break;
slouken@0
   108
			    case 's':
slouken@0
   109
				{
slouken@0
   110
				  int index = error->argc;
icculus@1172
   111
				  char *str = va_arg(ap, char *);
icculus@1172
   112
				  if (str == NULL)
icculus@1172
   113
				      str = "(null)";
icculus@1172
   114
				  strncpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
slouken@0
   115
				  error->args[index].buf[ERR_MAX_STRLEN-1] = 0;
slouken@0
   116
				  error->argc++;
slouken@0
   117
				}
slouken@0
   118
				break;
slouken@0
   119
			    default:
slouken@0
   120
				break;
slouken@0
   121
			}
slouken@0
   122
			if ( error->argc >= ERR_MAX_ARGS ) {
slouken@0
   123
				break;
slouken@0
   124
			}
slouken@0
   125
		}
slouken@0
   126
	}
slouken@0
   127
	va_end(ap);
slouken@0
   128
slouken@1
   129
#ifndef DISABLE_STDIO
slouken@0
   130
	/* If we are in debug mode, print out an error message */
slouken@0
   131
#ifdef DEBUG_ERROR
slouken@0
   132
	fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
slouken@0
   133
#else
slouken@0
   134
	if ( getenv("SDL_DEBUG") ) {
slouken@0
   135
		fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
slouken@0
   136
	}
slouken@0
   137
#endif
slouken@1
   138
#endif /* !DISABLE_STDIO */
slouken@0
   139
}
slouken@0
   140
slouken@0
   141
/* Print out an integer value to a UNICODE buffer */
slouken@0
   142
static int PrintInt(Uint16 *str, unsigned int maxlen, int value)
slouken@0
   143
{
slouken@0
   144
	char tmp[128];
slouken@0
   145
	int len, i;
slouken@0
   146
slouken@0
   147
	sprintf(tmp, "%d", value);
slouken@0
   148
	len = 0;
slouken@0
   149
	if ( strlen(tmp) < maxlen ) {
slouken@0
   150
		for ( i=0; tmp[i]; ++i ) {
slouken@0
   151
			*str++ = tmp[i];
slouken@0
   152
			++len;
slouken@0
   153
		}
slouken@0
   154
	}
slouken@0
   155
	return(len);
slouken@0
   156
}
slouken@0
   157
/* Print out a double value to a UNICODE buffer */
slouken@0
   158
static int PrintDouble(Uint16 *str, unsigned int maxlen, double value)
slouken@0
   159
{
slouken@0
   160
	char tmp[128];
slouken@0
   161
	int len, i;
slouken@0
   162
slouken@0
   163
	sprintf(tmp, "%f", value);
slouken@0
   164
	len = 0;
slouken@0
   165
	if ( strlen(tmp) < maxlen ) {
slouken@0
   166
		for ( i=0; tmp[i]; ++i ) {
slouken@0
   167
			*str++ = tmp[i];
slouken@0
   168
			++len;
slouken@0
   169
		}
slouken@0
   170
	}
slouken@0
   171
	return(len);
slouken@0
   172
}
slouken@0
   173
/* Print out a pointer value to a UNICODE buffer */
slouken@0
   174
static int PrintPointer(Uint16 *str, unsigned int maxlen, void *value)
slouken@0
   175
{
slouken@0
   176
	char tmp[128];
slouken@0
   177
	int len, i;
slouken@0
   178
slouken@0
   179
	sprintf(tmp, "%p", value);
slouken@0
   180
	len = 0;
slouken@0
   181
	if ( strlen(tmp) < maxlen ) {
slouken@0
   182
		for ( i=0; tmp[i]; ++i ) {
slouken@0
   183
			*str++ = tmp[i];
slouken@0
   184
			++len;
slouken@0
   185
		}
slouken@0
   186
	}
slouken@0
   187
	return(len);
slouken@0
   188
}
slouken@0
   189
slouken@0
   190
/* This function has a bit more overhead than most error functions
slouken@0
   191
   so that it supports internationalization and thread-safe errors.
slouken@0
   192
*/
slouken@0
   193
Uint16 *SDL_GetErrorMsgUNICODE(Uint16 *errstr, unsigned int maxlen)
slouken@0
   194
{
slouken@0
   195
	SDL_error *error;
slouken@0
   196
slouken@0
   197
	/* Clear the error string */
slouken@0
   198
	*errstr = 0; --maxlen;
slouken@0
   199
slouken@0
   200
	/* Get the thread-safe error, and print it out */
slouken@0
   201
	error = SDL_GetErrBuf();
slouken@0
   202
	if ( error->error ) {
slouken@0
   203
		Uint16 translated[ERR_MAX_STRLEN], *fmt, *msg;
slouken@0
   204
		int len;
slouken@0
   205
		int argi;
slouken@0
   206
slouken@0
   207
		/* Print out the UNICODE error message */
slouken@0
   208
		SDL_LookupString(error->key, translated, sizeof(translated));
slouken@0
   209
		msg = errstr;
slouken@0
   210
		argi = 0;
slouken@0
   211
		for ( fmt=translated; *fmt && (maxlen > 0); ) {
slouken@0
   212
			if ( *fmt == '%' ) {
slouken@0
   213
				switch (fmt[1]) {
slouken@0
   214
				    case 'S':	/* Special SKIP operand */
slouken@0
   215
					argi += (fmt[2] - '0');
slouken@0
   216
					++fmt;
slouken@0
   217
					break;
slouken@0
   218
				    case '%':
slouken@0
   219
					*msg++ = '%';
slouken@0
   220
					maxlen -= 1;
slouken@0
   221
					break;
slouken@0
   222
#if 0	/* What is a character anyway?  (UNICODE issues) */
slouken@0
   223
				    case 'c':
slouken@0
   224
                                        *msg++ = (unsigned char)
slouken@0
   225
					         error->args[argi++].value_c;
slouken@0
   226
					maxlen -= 1;
slouken@0
   227
					break;
slouken@0
   228
#endif
slouken@0
   229
				    case 'd':
slouken@0
   230
					len = PrintInt(msg, maxlen,
slouken@0
   231
						error->args[argi++].value_i);
slouken@0
   232
					msg += len;
slouken@0
   233
					maxlen -= len;
slouken@0
   234
					break;
slouken@0
   235
				    case 'f':
slouken@0
   236
					len = PrintDouble(msg, maxlen,
slouken@0
   237
						error->args[argi++].value_f);
slouken@0
   238
					msg += len;
slouken@0
   239
					maxlen -= len;
slouken@0
   240
					break;
slouken@0
   241
				    case 'p':
slouken@0
   242
					len = PrintPointer(msg, maxlen,
slouken@0
   243
						error->args[argi++].value_ptr);
slouken@0
   244
					msg += len;
slouken@0
   245
					maxlen -= len;
slouken@0
   246
					break;
slouken@0
   247
				    case 's': /* UNICODE string */
slouken@0
   248
					{ Uint16 buf[ERR_MAX_STRLEN], *str;
slouken@0
   249
					  SDL_LookupString(error->args[argi++].buf, buf, sizeof(buf));
slouken@0
   250
					  str = buf;
slouken@0
   251
					  while ( *str && (maxlen > 0) ) {
slouken@0
   252
						*msg++ = *str++;
slouken@0
   253
						maxlen -= 1;
slouken@0
   254
					  }
slouken@0
   255
					}
slouken@0
   256
					break;
slouken@0
   257
				}
slouken@0
   258
				fmt += 2;
slouken@0
   259
			} else {
slouken@0
   260
				*msg++ = *fmt++;
slouken@0
   261
				maxlen -= 1;
slouken@0
   262
			}
slouken@0
   263
		}
slouken@0
   264
		*msg = 0;	/* NULL terminate the string */
slouken@0
   265
	}
slouken@0
   266
	return(errstr);
slouken@0
   267
}
slouken@0
   268
slouken@0
   269
Uint8 *SDL_GetErrorMsg(Uint8 *errstr, unsigned int maxlen)
slouken@0
   270
{
slouken@0
   271
	Uint16 *errstr16;
slouken@0
   272
	unsigned int i;
slouken@0
   273
slouken@0
   274
	/* Allocate the UNICODE buffer */
slouken@0
   275
	errstr16 = (Uint16 *)malloc(maxlen * (sizeof *errstr16));
slouken@0
   276
	if ( ! errstr16 ) {
slouken@0
   277
		strncpy((char *)errstr, "Out of memory", maxlen);
slouken@0
   278
		errstr[maxlen-1] = '\0';
slouken@0
   279
		return(errstr);
slouken@0
   280
	}
slouken@0
   281
slouken@0
   282
	/* Get the error message */
slouken@0
   283
	SDL_GetErrorMsgUNICODE(errstr16, maxlen);
slouken@0
   284
slouken@0
   285
	/* Convert from UNICODE to Latin1 encoding */
slouken@0
   286
	for ( i=0; i<maxlen; ++i ) {
slouken@0
   287
		errstr[i] = (Uint8)errstr16[i];
slouken@0
   288
	}
slouken@0
   289
slouken@0
   290
	/* Free UNICODE buffer (if necessary) */
slouken@0
   291
	free(errstr16);
slouken@0
   292
slouken@0
   293
	return(errstr);
slouken@0
   294
}
slouken@0
   295
slouken@0
   296
/* Available for backwards compatibility */
slouken@0
   297
char *SDL_GetError (void)
slouken@0
   298
{
slouken@0
   299
	static char errmsg[SDL_ERRBUFIZE];
slouken@0
   300
slouken@0
   301
	return((char *)SDL_GetErrorMsg((unsigned char *)errmsg, SDL_ERRBUFIZE));
slouken@0
   302
}
slouken@0
   303
slouken@0
   304
void SDL_ClearError(void)
slouken@0
   305
{
slouken@0
   306
	SDL_error *error;
slouken@0
   307
slouken@0
   308
	error = SDL_GetErrBuf();
slouken@0
   309
	error->error = 0;
slouken@0
   310
}
slouken@0
   311
slouken@0
   312
/* Very common errors go here */
slouken@0
   313
void SDL_Error(SDL_errorcode code)
slouken@0
   314
{
slouken@0
   315
	switch (code) {
slouken@0
   316
		case SDL_ENOMEM:
slouken@0
   317
			SDL_SetError("Out of memory");
slouken@0
   318
			break;
slouken@0
   319
		case SDL_EFREAD:
slouken@0
   320
			SDL_SetError("Error reading from datastream");
slouken@0
   321
			break;
slouken@0
   322
		case SDL_EFWRITE:
slouken@0
   323
			SDL_SetError("Error writing to datastream");
slouken@0
   324
			break;
slouken@0
   325
		case SDL_EFSEEK:
slouken@0
   326
			SDL_SetError("Error seeking in datastream");
slouken@0
   327
			break;
slouken@0
   328
		default:
slouken@0
   329
			SDL_SetError("Unknown SDL error");
slouken@0
   330
			break;
slouken@0
   331
	}
slouken@0
   332
}
slouken@0
   333
slouken@0
   334
#ifdef TEST_ERROR
slouken@0
   335
int main(int argc, char *argv[])
slouken@0
   336
{
slouken@0
   337
	char buffer[BUFSIZ+1];
slouken@0
   338
slouken@0
   339
	SDL_SetError("Hi there!");
slouken@0
   340
	printf("Error 1: %s\n", SDL_GetError());
slouken@0
   341
	SDL_ClearError();
slouken@0
   342
	memset(buffer, '1', BUFSIZ);
slouken@0
   343
	buffer[BUFSIZ] = 0;
slouken@0
   344
	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
slouken@0
   345
	printf("Error 2: %s\n", SDL_GetError());
slouken@0
   346
	exit(0);
slouken@0
   347
}
slouken@0
   348
#endif