Fix clog: own error allocating from static buffer
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Only other dependency (could use regular malloc too). */
|
||||||
#include "MEM_guardedalloc.h"
|
#include "MEM_guardedalloc.h"
|
||||||
|
|
||||||
/* own include. */
|
/* own include. */
|
||||||
@@ -102,15 +103,21 @@ static void clg_str_free(CLogStringBuf *cstr)
|
|||||||
static void clg_str_reserve(CLogStringBuf *cstr, const uint len)
|
static void clg_str_reserve(CLogStringBuf *cstr, const uint len)
|
||||||
{
|
{
|
||||||
if (len > cstr->len_alloc) {
|
if (len > cstr->len_alloc) {
|
||||||
if (cstr->is_alloc == false) {
|
|
||||||
cstr->is_alloc = true;
|
|
||||||
cstr->data = NULL;
|
|
||||||
}
|
|
||||||
cstr->len_alloc *= 2;
|
cstr->len_alloc *= 2;
|
||||||
if (len > cstr->len_alloc) {
|
if (len > cstr->len_alloc) {
|
||||||
cstr->len_alloc = len;
|
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;
|
cstr->len_alloc = len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -400,6 +407,8 @@ void CLG_logf(
|
|||||||
fwrite(cstr.data, cstr.len, 1, lg->ctx->output);
|
fwrite(cstr.data, cstr.len, 1, lg->ctx->output);
|
||||||
fflush(lg->ctx->output);
|
fflush(lg->ctx->output);
|
||||||
|
|
||||||
|
clg_str_free(&cstr);
|
||||||
|
|
||||||
if (severity == CLG_SEVERITY_FATAL) {
|
if (severity == CLG_SEVERITY_FATAL) {
|
||||||
clg_ctx_fatal_action(lg->ctx, lg->ctx->output);
|
clg_ctx_fatal_action(lg->ctx, lg->ctx->output);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user