From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34868) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YzOmD-00014I-2d for qemu-devel@nongnu.org; Mon, 01 Jun 2015 08:25:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YzOm9-00081w-0D for qemu-devel@nongnu.org; Mon, 01 Jun 2015 08:25:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55871) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YzOm8-00081S-S8 for qemu-devel@nongnu.org; Mon, 01 Jun 2015 08:25:04 -0400 Date: Mon, 1 Jun 2015 14:25:01 +0200 From: "Michael S. Tsirkin" Message-ID: <1433161230-29421-45-git-send-email-mst@redhat.com> References: <1433161230-29421-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1433161230-29421-1-git-send-email-mst@redhat.com> Subject: [Qemu-devel] [PULL v2 44/60] acpi: Simplify printing to dynamic string List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , John Snow , Markus Armbruster , Igor Mammedov From: Markus Armbruster build_append_namestringv() and aml_string() first calculate the resulting string's length with vsnprintf(NULL, ...), then allocate, then print for real. Simply use g_strdup_vprintf() or g_vasprintf() instead. Signed-off-by: Markus Armbruster Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin Reviewed-by: John Snow Reviewed-by: Igor Mammedov --- hw/acpi/aml-build.c | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 323b7bc..4cc0c61 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -19,6 +19,7 @@ * with this program; if not, see . */ +#include #include #include #include @@ -59,7 +60,6 @@ static void build_append_array(GArray *array, GArray *val) static void build_append_nameseg(GArray *array, const char *seg) { - /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */ int len; len = strlen(seg); @@ -73,22 +73,12 @@ build_append_nameseg(GArray *array, const char *seg) static void GCC_FMT_ATTR(2, 0) build_append_namestringv(GArray *array, const char *format, va_list ap) { - /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */ char *s; - int len; - va_list va_len; char **segs; char **segs_iter; int seg_count = 0; - va_copy(va_len, ap); - len = vsnprintf(NULL, 0, format, va_len); - va_end(va_len); - len += 1; - s = g_new(typeof(*s), len); - - len = vsnprintf(s, len, format, ap); - + s = g_strdup_vprintf(format, ap); segs = g_strsplit(s, ".", 0); g_free(s); @@ -753,22 +743,15 @@ Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name) Aml *aml_string(const char *name_format, ...) { Aml *var = aml_opcode(0x0D /* StringPrefix */); - va_list ap, va_len; + va_list ap; char *s; int len; va_start(ap, name_format); - va_copy(va_len, ap); - len = vsnprintf(NULL, 0, name_format, va_len); - va_end(va_len); - len += 1; - s = g_new0(typeof(*s), len); - - len = vsnprintf(s, len, name_format, ap); + len = g_vasprintf(&s, name_format, ap); va_end(ap); - g_array_append_vals(var->buf, s, len); - build_append_byte(var->buf, 0x0); /* NullChar */ + g_array_append_vals(var->buf, s, len + 1); g_free(s); return var; -- MST