ctx: fixed memory leak in xmpp_log()

Also fixed 2 bugs:
  - only 1022 bytes of the message is printed when length is 1023.
  - va_end() must be called after va_copy() otherwise we get
    undefined behaviour according to C99.
This commit is contained in:
Dmitry Podgorny
2014-09-10 01:03:34 +03:00
parent db14f2bd4a
commit ee4f6d4e3c

View File

@@ -252,29 +252,34 @@ void xmpp_log(const xmpp_ctx_t * const ctx,
char *buf;
va_list copy;
buf = smbuf;
va_copy(copy, ap);
ret = xmpp_vsnprintf(buf, 1023, fmt, ap);
if (ret > 1023) {
ret = xmpp_vsnprintf(smbuf, sizeof(smbuf), fmt, ap);
if (ret >= (int)sizeof(smbuf)) {
buf = (char *)xmpp_alloc(ctx, ret + 1);
if (!buf) {
buf = NULL;
xmpp_error(ctx, "log", "Failed allocating memory for log message.");
va_end(copy);
va_end(copy);
return;
}
oldret = ret;
ret = xmpp_vsnprintf(buf, ret + 1, fmt, copy);
if (ret > oldret) {
xmpp_error(ctx, "log", "Unexpected error");
xmpp_free(ctx, buf);
va_end(copy);
return;
}
} else {
va_end(copy);
buf = smbuf;
}
va_end(copy);
if (ctx->log->handler)
ctx->log->handler(ctx->log->userdata, level, area, buf);
if (buf != smbuf)
xmpp_free(ctx, buf);
}
/** Write to the log at the ERROR level.