changeset 5228:ab0d7cecc0f6

Fixed stack overflow on Windows
author Sam Lantinga <slouken@libsdl.org>
date Mon, 07 Feb 2011 20:05:52 -0800
parents c66b2a778b7e
children 2178ffe17222
files src/SDL_log.c
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/SDL_log.c	Mon Feb 07 17:44:07 2011 -0800
+++ b/src/SDL_log.c	Mon Feb 07 20:05:52 2011 -0800
@@ -233,7 +233,7 @@
 void
 SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
 {
-    char message[SDL_MAX_LOG_MESSAGE];
+    char *message;
 
     /* Make sure we don't exceed array bounds */
     if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
@@ -245,17 +245,26 @@
         return;
     }
 
-    SDL_vsnprintf(message, SDL_arraysize(message), fmt, ap);
+    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
+    if (!message) {
+        return;
+    }
+    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
 
 #if defined(__WIN32__)
+    /* Way too many allocations here, urgh */
     {
-        char output[32+SDL_MAX_LOG_MESSAGE];
+        char *output;
+        size_t length;
         LPTSTR tstr;
 
-        SDL_snprintf(output, SDL_arraysize(output), "%s: %s", SDL_priority_prefixes[priority], message);
+        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1;
+        output = SDL_stack_alloc(char, length);
+        SDL_snprintf(output, length, "%s: %s", SDL_priority_prefixes[priority], message);
         tstr = WIN_UTF8ToString(output);
         OutputDebugString(tstr);
         SDL_free(tstr);
+        SDL_stack_free(output);
     }
 #elif defined(__ANDROID__)
     {
@@ -268,6 +277,7 @@
 #if HAVE_STDIO_H
     fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
 #endif
+    SDL_stack_free(message);
 }
 
 /* vi: set ts=4 sw=4 expandtab: */