From 17ff1d94e6274fd7a88c14de542897b6ccd19f29 Mon Sep 17 00:00:00 2001 From: Palana Date: Thu, 23 Apr 2015 21:56:22 +0200 Subject: [PATCH] util: Calculate buffer size for dstr_vprintf (C99) --- libobs/util/dstr.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libobs/util/dstr.c b/libobs/util/dstr.c index 4e89d3319..245ea19da 100644 --- a/libobs/util/dstr.c +++ b/libobs/util/dstr.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "c99defs.h" #include "dstr.h" @@ -504,15 +505,23 @@ void dstr_catf(struct dstr *dst, const char *format, ...) void dstr_vprintf(struct dstr *dst, const char *format, va_list args) { - dstr_ensure_capacity(dst, 4096); - vsnprintf(dst->array, 4095, format, args); + va_list args_cp; + va_copy(args_cp, args); + + int len = vsnprintf(NULL, 0, format, args_cp); + va_end(args_cp); + + if (len < 0) len = 4095; + + dstr_ensure_capacity(dst, ((size_t)len) + 1); + len = vsnprintf(dst->array, ((size_t)len) + 1, format, args); if (!*dst->array) { dstr_free(dst); return; } - dst->len = strlen(dst->array); + dst->len = len < 0 ? strlen(dst->array) : (size_t)len; } void dstr_vcatf(struct dstr *dst, const char *format, va_list args)