From: Martin Mares Date: Fri, 18 Oct 2013 15:25:38 +0000 (+0200) Subject: Fastbuf: Fix possible memory leak in new bprintf() X-Git-Tag: v5.99~70 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=7bbac4cd20d6ece66541ee94556052f5ef4bd0dd;p=libucw.git Fastbuf: Fix possible memory leak in new bprintf() --- diff --git a/ucw/ff-printf.c b/ucw/ff-printf.c index 903508cf..09a6d432 100644 --- a/ucw/ff-printf.c +++ b/ucw/ff-printf.c @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -32,21 +33,32 @@ vbprintf(struct fastbuf *b, const char *msg, va_list args) return len; } + /* + * We need a temporary buffer. If it is small, let's use stack. + * Otherwise, we malloc it, but we have to be careful, since we + * might be running inside a transaction and bwrite() could + * throw an exception. + * + * FIXME: This deserves a more systematic solution, the same + * problem is likely to happen at other places, too. + */ int bufsize = len + 1; - bool need_free = 0; + struct resource *res = NULL; + byte *extra_buffer = NULL; if (bufsize <= 256) buf = alloca(bufsize); + else if (rp_current()) + buf = res_malloc(bufsize, &res); else - { - buf = xmalloc(bufsize); - need_free = 1; - } + buf = extra_buffer = xmalloc(bufsize); vsnprintf(buf, bufsize, msg, args); bwrite(b, buf, len); - if (need_free) - xfree(buf); + if (res) + res_free(res); + else if (extra_buffer) + xfree(extra_buffer); return len; }