kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit fa69f2a49c397c0a843157804d3b76b3fc0c93e2
parent 0542d70a71c08a741319ac7c36b9cdf53ffeade2
Author: Joris Vink <joris@coders.se>
Date:   Fri, 22 Aug 2014 14:40:19 +0200

Attempt vsnprintf() up to BUFSIZ in kore_buf_appendv().

If the result was too large fall back to vasprintf instead.

Diffstat:
src/buf.c | 17+++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/buf.c b/src/buf.c @@ -56,14 +56,23 @@ void kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args) { int l; - char *b; + char *b, sb[BUFSIZ]; - l = vasprintf(&b, fmt, args); + l = vsnprintf(sb, sizeof(sb), fmt, args); if (l == -1) - fatal("kore_buf_appendv(): error or truncation"); + fatal("kore_buf_appendv(): vsnprintf error"); + + if ((size_t)l >= sizeof(sb)) { + l = vasprintf(&b, fmt, args); + if (l == -1) + fatal("kore_buf_appendv(): error or truncation"); + } else { + b = sb; + } kore_buf_append(buf, (u_int8_t *)b, l); - free(b); + if (b != sb) + free(b); } void