diff --git a/src/stdio/vasprintf.c b/src/stdio/vasprintf.c index 08251bc2..d55fe32f 100644 --- a/src/stdio/vasprintf.c +++ b/src/stdio/vasprintf.c @@ -5,11 +5,16 @@ int vasprintf(char **s, const char *fmt, va_list ap) { - va_list ap2; - va_copy(ap2, ap); - int l = vsnprintf(0, 0, fmt, ap2); - va_end(ap2); + size_t l; + *s = 0; + FILE *f = open_memstream(s, &l); + if (!f) + return -1; - if (l<0 || !(*s=malloc(l+1U))) return -1; - return vsnprintf(*s, l+1U, fmt, ap); + if ((l = vfprintf(f, fmt, ap)) == -1) { + free(*s); + *s = 0; + } + fclose(f); + return l; }