Received: from eggs.gnu.org ([2001:4830:134:3::10]:35856) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cCAoy-00046t-Aq for qemu-devel@nongnu.org; Wed, 30 Nov 2016 14:45:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cCAou-00025J-Me for qemu-devel@nongnu.org; Wed, 30 Nov 2016 14:45:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52908) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cCAou-00024j-8d for qemu-devel@nongnu.org; Wed, 30 Nov 2016 14:45:32 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 22296C04B95F for ; Wed, 30 Nov 2016 19:45:31 +0000 (UTC) From: Eric Blake Date: Wed, 30 Nov 2016 13:44:48 -0600 Message-Id: <1480535094-23831-31-git-send-email-eblake@redhat.com> In-Reply-To: <1480535094-23831-1-git-send-email-eblake@redhat.com> References: <1480535094-23831-1-git-send-email-eblake@redhat.com> Subject: [Qemu-devel] [PATCH 30/36] qtest: Avoid dynamic JSON in qmp_fd_sendv() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: armbru@redhat.com, pbonzini@redhat.com The only format specifier left in any client of qmp() and friends is %s. Rather than having to make our JSON parser support varargs, and use a conversion from string to QObject and back to string, we can instead just directly build the string and substitute %s ourselves. With this, the final caller of qobject_from_jsonv() is eliminated, and followup patches can simplify the parser. Signed-off-by: Eric Blake --- Too bad glibc's strchrnul() is not universal; also, this patch could avoid "%.*s" format shenanigans (with the nastiness of converting ptrdiff_t to int) if we want to first enhance QString to support a length-limited append primitive. --- tests/libqtest.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/tests/libqtest.c b/tests/libqtest.c index 22bf0ad..a9559d8 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -427,27 +427,37 @@ QDict *qtest_qmp_receive(QTestState *s) */ static void qmp_fd_sendv(int fd, const char *fmt, va_list ap) { - va_list ap_copy; - QObject *qobj; + QString *qstr; - /* Going through qobject ensures we escape strings properly. - * This seemingly unnecessary copy is required in case va_list - * is an array type. + if (!strchr(fmt, '%')) { + return qmp_fd_send(fd, fmt); + } + + /* Construct a single string, turning %s in fmt into proper JSON + * strings. For now, we don't support any other format specifiers. */ - va_copy(ap_copy, ap); - qobj = qobject_from_jsonv(fmt, &ap_copy); - va_end(ap_copy); + qstr = qstring_new(); + while (*fmt) { + /* Too bad strchrnul() is not universal */ + const char *p = strchr(fmt, '%'); - /* No need to send anything for an empty QObject. */ - if (qobj) { - QString *qstr = qobject_to_json(qobj); - const char *str = qstring_get_str(qstr); + if (!p) { + p = strchr(fmt, '\0'); + } - qmp_fd_send(fd, str); - - QDECREF(qstr); - qobject_decref(qobj); + assert(p - fmt < INT_MAX); + qstring_append_printf(qstr, "%.*s", (int)(p - fmt), fmt); + if (*p) { + assert(p[1] == 's'); + p += 2; + qstring_append_json_string(qstr, va_arg(ap, const char *)); + } + fmt = p; } + + qmp_fd_send(fd, qstring_get_str(qstr)); + + QDECREF(qstr); } QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap) -- 2.7.4