src/SDL_error.c
author Bastien Nocera <hadess@hadess.net>
Sat, 01 Oct 2016 10:36:24 -0700
changeset 10396 fa4e35434a9c
parent 9998 f67cf37e9cd4
child 10403 a0c76d344583
permissions -rw-r--r--
Fix "format not a string literal" errors
With GCC 6.1.

https://bugzilla.libsdl.org/show_bug.cgi?id=3375
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "./SDL_internal.h"
    22 
    23 /* Simple error handling in SDL */
    24 
    25 #include "SDL_log.h"
    26 #include "SDL_error.h"
    27 #include "SDL_error_c.h"
    28 
    29 
    30 /* Routine to get the thread-specific error variable */
    31 #if SDL_THREADS_DISABLED
    32 /* The default (non-thread-safe) global error variable */
    33 static SDL_error SDL_global_error;
    34 #define SDL_GetErrBuf() (&SDL_global_error)
    35 #else
    36 extern SDL_error *SDL_GetErrBuf(void);
    37 #endif /* SDL_THREADS_DISABLED */
    38 
    39 #define SDL_ERRBUFIZE   1024
    40 
    41 /* Private functions */
    42 
    43 static const char *
    44 SDL_LookupString(const char *key)
    45 {
    46     /* FIXME: Add code to lookup key in language string hash-table */
    47     return key;
    48 }
    49 
    50 /* Public functions */
    51 
    52 int
    53 SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
    54 {
    55     va_list ap;
    56     SDL_error *error;
    57 
    58     /* Ignore call if invalid format pointer was passed */
    59     if (fmt == NULL) return -1;
    60 
    61     /* Copy in the key, mark error as valid */
    62     error = SDL_GetErrBuf();
    63     error->error = 1;
    64     SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
    65 
    66     va_start(ap, fmt);
    67     error->argc = 0;
    68     while (*fmt) {
    69         if (*fmt++ == '%') {
    70             while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
    71                 ++fmt;
    72             }
    73             switch (*fmt++) {
    74             case 0:            /* Malformed format string.. */
    75                 --fmt;
    76                 break;
    77             case 'c':
    78             case 'i':
    79             case 'd':
    80             case 'u':
    81             case 'o':
    82             case 'x':
    83             case 'X':
    84                 error->args[error->argc++].value_i = va_arg(ap, int);
    85                 break;
    86             case 'f':
    87                 error->args[error->argc++].value_f = va_arg(ap, double);
    88                 break;
    89             case 'p':
    90                 error->args[error->argc++].value_ptr = va_arg(ap, void *);
    91                 break;
    92             case 's':
    93                 {
    94                     int i = error->argc;
    95                     const char *str = va_arg(ap, const char *);
    96                     if (str == NULL)
    97                         str = "(null)";
    98                     SDL_strlcpy((char *) error->args[i].buf, str,
    99                                 ERR_MAX_STRLEN);
   100                     error->argc++;
   101                 }
   102                 break;
   103             default:
   104                 break;
   105             }
   106             if (error->argc >= ERR_MAX_ARGS) {
   107                 break;
   108             }
   109         }
   110     }
   111     va_end(ap);
   112 
   113     /* If we are in debug mode, print out an error message */
   114     SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());
   115 
   116     return -1;
   117 }
   118 
   119 #pragma GCC diagnostic push
   120 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
   121 /* This function has a bit more overhead than most error functions
   122    so that it supports internationalization and thread-safe errors.
   123 */
   124 static char *
   125 SDL_GetErrorMsg(char *errstr, int maxlen)
   126 {
   127     SDL_error *error;
   128 
   129     /* Clear the error string */
   130     *errstr = '\0';
   131     --maxlen;
   132 
   133     /* Get the thread-safe error, and print it out */
   134     error = SDL_GetErrBuf();
   135     if (error->error) {
   136         const char *fmt;
   137         char *msg = errstr;
   138         int len;
   139         int argi;
   140 
   141         fmt = SDL_LookupString(error->key);
   142         argi = 0;
   143         while (*fmt && (maxlen > 0)) {
   144             if (*fmt == '%') {
   145                 char tmp[32], *spot = tmp;
   146                 *spot++ = *fmt++;
   147                 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
   148                        && spot < (tmp + SDL_arraysize(tmp) - 2)) {
   149                     *spot++ = *fmt++;
   150                 }
   151                 *spot++ = *fmt++;
   152                 *spot++ = '\0';
   153                 switch (spot[-2]) {
   154                 case '%':
   155                     *msg++ = '%';
   156                     maxlen -= 1;
   157                     break;
   158                 case 'c':
   159                 case 'i':
   160                 case 'd':
   161                 case 'u':
   162                 case 'o':
   163                 case 'x':
   164                 case 'X':
   165                     len =
   166                         SDL_snprintf(msg, maxlen, tmp,
   167                                      error->args[argi++].value_i);
   168                     if (len > 0) {
   169                         msg += len;
   170                         maxlen -= len;
   171                     }
   172                     break;
   173 
   174                 case 'f':
   175                     len =
   176                         SDL_snprintf(msg, maxlen, tmp,
   177                                      error->args[argi++].value_f);
   178                     if (len > 0) {
   179                         msg += len;
   180                         maxlen -= len;
   181                     }
   182                     break;
   183 
   184                 case 'p':
   185                     len =
   186                         SDL_snprintf(msg, maxlen, tmp,
   187                                      error->args[argi++].value_ptr);
   188                     if (len > 0) {
   189                         msg += len;
   190                         maxlen -= len;
   191                     }
   192                     break;
   193 
   194                 case 's':
   195                     len =
   196                         SDL_snprintf(msg, maxlen, tmp,
   197                                      SDL_LookupString(error->args[argi++].
   198                                                       buf));
   199                     if (len > 0) {
   200                         msg += len;
   201                         maxlen -= len;
   202                     }
   203                     break;
   204 
   205                 }
   206             } else {
   207                 *msg++ = *fmt++;
   208                 maxlen -= 1;
   209             }
   210         }
   211 
   212         /* slide back if we've overshot the end of our buffer. */
   213         if (maxlen < 0) {
   214             msg -= (-maxlen) + 1;
   215         }
   216 
   217         *msg = 0;               /* NULL terminate the string */
   218     }
   219     return (errstr);
   220 }
   221 #pragma GCC diagnostic pop
   222 
   223 /* Available for backwards compatibility */
   224 const char *
   225 SDL_GetError(void)
   226 {
   227     static char errmsg[SDL_ERRBUFIZE];
   228 
   229     return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
   230 }
   231 
   232 void
   233 SDL_ClearError(void)
   234 {
   235     SDL_error *error;
   236 
   237     error = SDL_GetErrBuf();
   238     error->error = 0;
   239 }
   240 
   241 /* Very common errors go here */
   242 int
   243 SDL_Error(SDL_errorcode code)
   244 {
   245     switch (code) {
   246     case SDL_ENOMEM:
   247         return SDL_SetError("Out of memory");
   248     case SDL_EFREAD:
   249         return SDL_SetError("Error reading from datastream");
   250     case SDL_EFWRITE:
   251         return SDL_SetError("Error writing to datastream");
   252     case SDL_EFSEEK:
   253         return SDL_SetError("Error seeking in datastream");
   254     case SDL_UNSUPPORTED:
   255         return SDL_SetError("That operation is not supported");
   256     default:
   257         return SDL_SetError("Unknown SDL error");
   258     }
   259 }
   260 
   261 #ifdef TEST_ERROR
   262 int
   263 main(int argc, char *argv[])
   264 {
   265     char buffer[BUFSIZ + 1];
   266 
   267     SDL_SetError("Hi there!");
   268     printf("Error 1: %s\n", SDL_GetError());
   269     SDL_ClearError();
   270     SDL_memset(buffer, '1', BUFSIZ);
   271     buffer[BUFSIZ] = 0;
   272     SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
   273     printf("Error 2: %s\n", SDL_GetError());
   274     exit(0);
   275 }
   276 #endif
   277 
   278 /* vi: set ts=4 sw=4 expandtab: */