log: Check for integer overflow.

This commit is contained in:
Eddy Jansson 2022-05-10 14:52:14 +02:00 committed by Sam Lantinga
parent ec8fa57750
commit f7280dcebb
1 changed files with 8 additions and 4 deletions

View File

@ -293,7 +293,8 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list
{ {
char *message = NULL; char *message = NULL;
char stack_buf[SDL_MAX_LOG_MESSAGE_STACK]; char stack_buf[SDL_MAX_LOG_MESSAGE_STACK];
size_t len; size_t len_plus_term;
int len;
va_list aq; va_list aq;
/* Nothing to do if we don't have an output function */ /* Nothing to do if we don't have an output function */
@ -321,14 +322,17 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list
len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq); len = SDL_vsnprintf(stack_buf, sizeof(stack_buf), fmt, aq);
va_end(aq); va_end(aq);
if (len < 0)
return;
/* If message truncated, allocate and re-render */ /* If message truncated, allocate and re-render */
if (len >= sizeof(stack_buf)) { if (len >= sizeof(stack_buf) && SDL_size_add_overflow(len, 1, &len_plus_term) == 0) {
/* Allocate exactly what we need, including the zero-terminator */ /* Allocate exactly what we need, including the zero-terminator */
message = (char *)SDL_malloc(len + 1); message = (char *)SDL_malloc(len_plus_term);
if (!message) if (!message)
return; return;
va_copy(aq, ap); va_copy(aq, ap);
len = SDL_vsnprintf(message, len + 1, fmt, aq); len = SDL_vsnprintf(message, len_plus_term, fmt, aq);
va_end(aq); va_end(aq);
} else { } else {
message = stack_buf; message = stack_buf;