From bbb7f046ff430d33267487cb6f8a0e24d2eab832 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Sun, 2 Jun 2019 10:17:22 +0200 Subject: common: Fix vasprintf emulation va_list must be saved when calling vsnprintf() in a loop. --- common/compat.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/compat.c b/common/compat.c index 5f47534..621f03a 100644 --- a/common/compat.c +++ b/common/compat.c @@ -570,6 +570,7 @@ vasprintf (char **strp, char *nbuf; int guess = 128; int length = 0; + va_list orig, aq; int ret; if (fmt == NULL) { @@ -577,17 +578,21 @@ vasprintf (char **strp, return -1; } + va_copy (orig, ap); for (;;) { nbuf = realloc (buf, guess); if (!nbuf) { free (buf); + va_end (orig); return -1; } buf = nbuf; length = guess; - ret = vsnprintf (buf, length, fmt, ap); + va_copy (aq, orig); + ret = vsnprintf (buf, length, fmt, aq); + va_end (aq); if (ret < 0) guess *= 2; @@ -598,6 +603,7 @@ vasprintf (char **strp, else break; } + va_end (orig); *strp = buf; return ret; -- cgit v1.1