From nobody Wed Nov 5 00:13:26 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1531394899125829.2635395914717; Thu, 12 Jul 2018 04:28:19 -0700 (PDT) Received: from localhost ([::1]:58736 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fdZli-0002YT-48 for importer@patchew.org; Thu, 12 Jul 2018 07:28:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47870) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fdZWO-00077g-5r for qemu-devel@nongnu.org; Thu, 12 Jul 2018 07:12:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fdZWL-0006pR-CP for qemu-devel@nongnu.org; Thu, 12 Jul 2018 07:12:28 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41930 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fdZWL-0006oB-02 for qemu-devel@nongnu.org; Thu, 12 Jul 2018 07:12:25 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A2B5F34667 for ; Thu, 12 Jul 2018 11:12:24 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-125.ams2.redhat.com [10.36.116.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5B6811C66F; Thu, 12 Jul 2018 11:12:24 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id D25DA1132D6B; Thu, 12 Jul 2018 13:12:21 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 12 Jul 2018 13:12:06 +0200 Message-Id: <20180712111221.20326-6-armbru@redhat.com> In-Reply-To: <20180712111221.20326-1-armbru@redhat.com> References: <20180712111221.20326-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 12 Jul 2018 11:12:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 12 Jul 2018 11:12:24 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'armbru@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 05/20] qobject: Replace qobject_from_jsonf() by qobject_from_jsonf_nofail() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Commit ab45015a968 "qobject: Let qobject_from_jsonf() fail instead of abort" removed only one of two ways qobject_from_jsonf() can abort. Wrong. Its rationale for letting it fail is that all remaining users cope fine with failure. Well, they're just fine with aborting, too; it's what they do on failure. Simply reverting the broken commit would bring back the unfortunate asymmetry between qobject_from_jsonf() and qobject_from_jsonv(): one aborts, the other returns null. So also rename it to qobject_from_jsonf_nofail(). Signed-off-by: Markus Armbruster Reviewed-by: Thomas Huth --- include/qapi/qmp/qjson.h | 6 ++++-- qobject/qjson.c | 8 +++++++- tests/check-qjson.c | 15 ++++++++------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/qapi/qmp/qjson.h b/include/qapi/qmp/qjson.h index 43b2ce2f33..dc509d51ae 100644 --- a/include/qapi/qmp/qjson.h +++ b/include/qapi/qmp/qjson.h @@ -15,11 +15,13 @@ #define QJSON_H =20 QObject *qobject_from_json(const char *string, Error **errp); -QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2); QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp) GCC_FMT_ATTR(1, 0); =20 -QDict *qdict_from_jsonf_nofail(const char *string, ...) GCC_FMT_ATTR(1, 2); +QObject *qobject_from_jsonf_nofail(const char *string, ...) + GCC_FMT_ATTR(1, 2); +QDict *qdict_from_jsonf_nofail(const char *string, ...) + GCC_FMT_ATTR(1, 2); =20 QString *qobject_to_json(const QObject *obj); QString *qobject_to_json_pretty(const QObject *obj); diff --git a/qobject/qjson.c b/qobject/qjson.c index 2f6a590e44..4a9dcff343 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -59,7 +59,12 @@ QObject *qobject_from_json(const char *string, Error **e= rrp) return qobject_from_jsonv(string, NULL, errp); } =20 -QObject *qobject_from_jsonf(const char *string, ...) +/* + * Parse @string as JSON value with %-escapes interpolated. + * Abort on error. Do not use with untrusted @string. + * Return the resulting QObject. It is never null. + */ +QObject *qobject_from_jsonf_nofail(const char *string, ...) { QObject *obj; va_list ap; @@ -68,6 +73,7 @@ QObject *qobject_from_jsonf(const char *string, ...) obj =3D qobject_from_jsonv(string, &ap, &error_abort); va_end(ap); =20 + assert(obj); return obj; } =20 diff --git a/tests/check-qjson.c b/tests/check-qjson.c index da582df3e9..eaf5d20663 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -865,7 +865,8 @@ static void vararg_string(void) QString *str; =20 str =3D qobject_to(QString, - qobject_from_jsonf("%s", test_cases[i].decoded)); + qobject_from_jsonf_nofail("%s", + test_cases[i].decoded)); g_assert(str); g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) =3D= =3D 0); =20 @@ -998,17 +999,17 @@ static void vararg_number(void) double valuef =3D 2.323423423; int64_t val; =20 - qnum =3D qobject_to(QNum, qobject_from_jsonf("%d", value)); + qnum =3D qobject_to(QNum, qobject_from_jsonf_nofail("%d", value)); g_assert(qnum_get_try_int(qnum, &val)); g_assert_cmpint(val, =3D=3D, value); qobject_unref(qnum); =20 - qnum =3D qobject_to(QNum, qobject_from_jsonf("%lld", value_ll)); + qnum =3D qobject_to(QNum, qobject_from_jsonf_nofail("%lld", value_ll)); g_assert(qnum_get_try_int(qnum, &val)); g_assert_cmpint(val, =3D=3D, value_ll); qobject_unref(qnum); =20 - qnum =3D qobject_to(QNum, qobject_from_jsonf("%f", valuef)); + qnum =3D qobject_to(QNum, qobject_from_jsonf_nofail("%f", valuef)); g_assert(qnum_get_double(qnum) =3D=3D valuef); qobject_unref(qnum); } @@ -1042,13 +1043,13 @@ static void keyword_literal(void) =20 qobject_unref(qbool); =20 - qbool =3D qobject_to(QBool, qobject_from_jsonf("%i", false)); + qbool =3D qobject_to(QBool, qobject_from_jsonf_nofail("%i", false)); g_assert(qbool); g_assert(qbool_get_bool(qbool) =3D=3D false); qobject_unref(qbool); =20 /* Test that non-zero values other than 1 get collapsed to true */ - qbool =3D qobject_to(QBool, qobject_from_jsonf("%i", 2)); + qbool =3D qobject_to(QBool, qobject_from_jsonf_nofail("%i", 2)); g_assert(qbool); g_assert(qbool_get_bool(qbool) =3D=3D true); qobject_unref(qbool); @@ -1298,7 +1299,7 @@ static void simple_varargs(void) embedded_obj =3D qobject_from_json("[32, 42]", &error_abort); g_assert(embedded_obj !=3D NULL); =20 - obj =3D qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj); + obj =3D qobject_from_jsonf_nofail("[%d, 2, %p]", 1, embedded_obj); g_assert(qlit_equal_qobject(&decoded, obj)); =20 qobject_unref(obj); --=20 2.17.1