log: Use malloc for long messages. For short messages, use a stack buffer that is significantly smaller than SDL_MAX_LOG_MESSAGE. The rationale for this is that we don't want to risk blowing the stack, while at the same time we would like to not put pressure on the memory allocator unless absolutely necessary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
diff --git a/src/SDL_log.c b/src/SDL_log.c
index d3896e2..5b7ad67 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -39,6 +39,9 @@
#include <android/log.h>
#endif
+/* The size of the stack buffer to use for rendering log messages. */
+#define SDL_MAX_LOG_MESSAGE_STACK 256
+
#define DEFAULT_PRIORITY SDL_LOG_PRIORITY_CRITICAL
#define DEFAULT_ASSERT_PRIORITY SDL_LOG_PRIORITY_WARN
#define DEFAULT_APPLICATION_PRIORITY SDL_LOG_PRIORITY_INFO
@@ -285,8 +288,10 @@ GetCategoryPrefix(int category)
void
SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
{
- static char message[SDL_MAX_LOG_MESSAGE];
+ char *message = NULL;
+ char stack_buf[SDL_MAX_LOG_MESSAGE_STACK];
size_t len;
+ va_list aq;
/* Nothing to do if we don't have an output function */
if (!SDL_log_function) {
@@ -308,14 +313,24 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list
log_function_mutex = SDL_CreateMutex();
}
- if (log_function_mutex) {
- SDL_LockMutex(log_function_mutex);
- }
+ /* Render into stack buffer */
+ va_copy(aq, ap);
+ len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq);
+ va_end(aq);
- SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
+ /* If message truncated, allocate and re-render */
+ if (len >= sizeof(stack_buf)) {
+ message = (char *)SDL_malloc(SDL_MAX_LOG_MESSAGE);
+ if (!message)
+ return;
+ va_copy(aq, ap);
+ len = SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, aq);
+ va_end(aq);
+ } else {
+ message = stack_buf;
+ }
/* Chop off final endline. */
- len = SDL_strlen(message);
if ((len > 0) && (message[len-1] == '\n')) {
message[--len] = '\0';
if ((len > 0) && (message[len-1] == '\r')) { /* catch "\r\n", too. */
@@ -323,11 +338,20 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list
}
}
+ if (log_function_mutex) {
+ SDL_LockMutex(log_function_mutex);
+ }
+
SDL_log_function(SDL_log_userdata, category, priority, message);
if (log_function_mutex) {
SDL_UnlockMutex(log_function_mutex);
}
+
+ /* Free only if dynamically allocated */
+ if (message != stack_buf) {
+ SDL_free(message);
+ }
}
#if defined(__WIN32__) && !defined(HAVE_STDIO_H) && !defined(__WINRT__)