Fixed stack overflow on Windows
authorSam Lantinga
Mon, 07 Feb 2011 20:05:52 -0800
changeset 5223ab0d7cecc0f6
parent 5222 c66b2a778b7e
child 5224 2178ffe17222
Fixed stack overflow on Windows
src/SDL_log.c
     1.1 --- a/src/SDL_log.c	Mon Feb 07 17:44:07 2011 -0800
     1.2 +++ b/src/SDL_log.c	Mon Feb 07 20:05:52 2011 -0800
     1.3 @@ -233,7 +233,7 @@
     1.4  void
     1.5  SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
     1.6  {
     1.7 -    char message[SDL_MAX_LOG_MESSAGE];
     1.8 +    char *message;
     1.9  
    1.10      /* Make sure we don't exceed array bounds */
    1.11      if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
    1.12 @@ -245,17 +245,26 @@
    1.13          return;
    1.14      }
    1.15  
    1.16 -    SDL_vsnprintf(message, SDL_arraysize(message), fmt, ap);
    1.17 +    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
    1.18 +    if (!message) {
    1.19 +        return;
    1.20 +    }
    1.21 +    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
    1.22  
    1.23  #if defined(__WIN32__)
    1.24 +    /* Way too many allocations here, urgh */
    1.25      {
    1.26 -        char output[32+SDL_MAX_LOG_MESSAGE];
    1.27 +        char *output;
    1.28 +        size_t length;
    1.29          LPTSTR tstr;
    1.30  
    1.31 -        SDL_snprintf(output, SDL_arraysize(output), "%s: %s", SDL_priority_prefixes[priority], message);
    1.32 +        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1;
    1.33 +        output = SDL_stack_alloc(char, length);
    1.34 +        SDL_snprintf(output, length, "%s: %s", SDL_priority_prefixes[priority], message);
    1.35          tstr = WIN_UTF8ToString(output);
    1.36          OutputDebugString(tstr);
    1.37          SDL_free(tstr);
    1.38 +        SDL_stack_free(output);
    1.39      }
    1.40  #elif defined(__ANDROID__)
    1.41      {
    1.42 @@ -268,6 +277,7 @@
    1.43  #if HAVE_STDIO_H
    1.44      fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
    1.45  #endif
    1.46 +    SDL_stack_free(message);
    1.47  }
    1.48  
    1.49  /* vi: set ts=4 sw=4 expandtab: */