diff --git a/src/ctx.c b/src/ctx.c index 48e87ad..5a24687 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -249,30 +249,38 @@ void xmpp_log(const xmpp_ctx_t * const ctx, { int oldret, ret; char smbuf[1024]; - char *buf; - va_list copy; + char *bigbuf = NULL; + va_list ap_copy; - buf = smbuf; - va_copy(copy, ap); - ret = xmpp_vsnprintf(buf, 1023, fmt, ap); + va_copy(ap_copy, ap); + ret = xmpp_vsnprintf(smbuf, 1023, fmt, ap_copy); + va_end(ap_copy); if (ret > 1023) { - va_copy(ap, copy); - buf = (char *)xmpp_alloc(ctx, ret + 1); - if (!buf) { - buf = NULL; - xmpp_error(ctx, "log", "Failed allocating memory for log message."); - return; - } - oldret = ret; - ret = xmpp_vsnprintf(buf, ret + 1, fmt, ap); - if (ret > oldret) { - xmpp_error(ctx, "log", "Unexpected error"); - return; - } + bigbuf = (char *)xmpp_alloc(ctx, ret + 1); + if (!bigbuf) { + bigbuf = NULL; + xmpp_error(ctx, "log", "Failed allocating memory for log message."); + return; + } + oldret = ret; + va_copy(ap_copy, ap); + ret = xmpp_vsnprintf(bigbuf, ret + 1, fmt, ap_copy); + va_end(ap_copy); + + if (ret > oldret) { + xmpp_error(ctx, "log", "Unexpected error"); + return; + } + + if (ctx->log->handler) { + ctx->log->handler(ctx->log->userdata, level, area, bigbuf); + } + xmpp_free(ctx, bigbuf); + } else { + if (ctx->log->handler) { + ctx->log->handler(ctx->log->userdata, level, area, smbuf); + } } - - if (ctx->log->handler) - ctx->log->handler(ctx->log->userdata, level, area, buf); } /** Write to the log at the ERROR level.