From b8a66973ac79f44c86736eddb6589d5749ad11e6 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 31 Mar 2018 11:25:49 +0200 Subject: Fix clog: own error allocating from static buffer --- intern/clog/clog.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'intern') diff --git a/intern/clog/clog.c b/intern/clog/clog.c index 66267dd6df2..e99633a2f81 100644 --- a/intern/clog/clog.c +++ b/intern/clog/clog.c @@ -29,6 +29,7 @@ # include #endif +/* Only other dependency (could use regular malloc too). */ #include "MEM_guardedalloc.h" /* own include. */ @@ -102,15 +103,21 @@ static void clg_str_free(CLogStringBuf *cstr) static void clg_str_reserve(CLogStringBuf *cstr, const uint len) { if (len > cstr->len_alloc) { - if (cstr->is_alloc == false) { - cstr->is_alloc = true; - cstr->data = NULL; - } cstr->len_alloc *= 2; if (len > cstr->len_alloc) { cstr->len_alloc = len; } - cstr->data = MEM_reallocN(cstr->data, len); + + if (cstr->is_alloc) { + cstr->data = MEM_reallocN(cstr->data, cstr->len_alloc); + } + else { + /* Copy the static buffer. */ + char *data = MEM_mallocN(cstr->len_alloc, __func__); + memcpy(data, cstr->data, cstr->len); + cstr->data = data; + cstr->is_alloc = true; + } cstr->len_alloc = len; } } @@ -400,6 +407,8 @@ void CLG_logf( fwrite(cstr.data, cstr.len, 1, lg->ctx->output); fflush(lg->ctx->output); + clg_str_free(&cstr); + if (severity == CLG_SEVERITY_FATAL) { clg_ctx_fatal_action(lg->ctx, lg->ctx->output); } -- cgit v1.2.3