From nobody Mon Feb 9 01:22:47 2026 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.zoho.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; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 14881951268611011.1031557592752; Mon, 27 Feb 2017 03:32:06 -0800 (PST) Received: from localhost ([::1]:51823 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ciJXB-0007bQ-Cp for importer@patchew.org; Mon, 27 Feb 2017 06:32:05 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44661) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ciJMW-00067i-NX for qemu-devel@nongnu.org; Mon, 27 Feb 2017 06:21:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ciJMU-00012F-8m for qemu-devel@nongnu.org; Mon, 27 Feb 2017 06:21:04 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45000) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ciJMN-0000u3-28; Mon, 27 Feb 2017 06:20:55 -0500 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 471A880F96; Mon, 27 Feb 2017 11:20:55 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-55.ams2.redhat.com [10.36.116.55]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D759C1DCC0; Mon, 27 Feb 2017 11:20:54 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 38DFA113860A; Mon, 27 Feb 2017 12:20:50 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 27 Feb 2017 12:20:40 +0100 Message-Id: <1488194450-28056-15-git-send-email-armbru@redhat.com> In-Reply-To: <1488194450-28056-1-git-send-email-armbru@redhat.com> References: <1488194450-28056-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.74 on 10.5.11.28 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 27 Feb 2017 11:20:55 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 14/24] check-qjson: Test errors from qobject_from_json() 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: , Cc: kwolf@redhat.com, "mdroth@linux.vnet.ibm.commdroth"@linux.vnet.ibm.com, pkrempa@redhat.com, qemu-block@nongnu.org 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" Pass &error_abort with known-good input. Else pass &err and check what comes back. This demonstrates that the parser fails silently for many errors. Signed-off-by: Markus Armbruster Reviewed-by: Kevin Wolf --- tests/check-qjson.c | 88 ++++++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/tests/check-qjson.c b/tests/check-qjson.c index aa63758..963dd46 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -10,8 +10,10 @@ * See the COPYING.LIB file in the top-level directory. * */ + #include "qemu/osdep.h" =20 +#include "qapi/error.h" #include "qapi/qmp/types.h" #include "qapi/qmp/qjson.h" #include "qemu-common.h" @@ -53,7 +55,7 @@ static void escaped_string(void) QObject *obj; QString *str; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); str =3D qobject_to_qstring(obj); g_assert(str); g_assert_cmpstr(qstring_get_str(str), =3D=3D, test_cases[i].decode= d); @@ -85,7 +87,7 @@ static void simple_string(void) QObject *obj; QString *str; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); str =3D qobject_to_qstring(obj); g_assert(str); g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) =3D= =3D 0); @@ -116,7 +118,7 @@ static void single_quote_string(void) QObject *obj; QString *str; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); str =3D qobject_to_qstring(obj); g_assert(str); g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) =3D= =3D 0); @@ -809,7 +811,7 @@ static void utf8_string(void) utf8_in =3D test_cases[i].utf8_in ?: test_cases[i].utf8_out; json_out =3D test_cases[i].json_out ?: test_cases[i].json_in; =20 - obj =3D qobject_from_json(json_in, NULL); + obj =3D qobject_from_json(json_in, utf8_out ? &error_abort : NULL); if (utf8_out) { str =3D qobject_to_qstring(obj); g_assert(str); @@ -836,7 +838,7 @@ static void utf8_string(void) * FIXME Enable once these bugs have been fixed. */ if (0 && json_out !=3D json_in) { - obj =3D qobject_from_json(json_out, NULL); + obj =3D qobject_from_json(json_out, &error_abort); str =3D qobject_to_qstring(obj); g_assert(str); g_assert_cmpstr(qstring_get_str(str), =3D=3D, utf8_out); @@ -886,7 +888,8 @@ static void simple_number(void) for (i =3D 0; test_cases[i].encoded; i++) { QInt *qint; =20 - qint =3D qobject_to_qint(qobject_from_json(test_cases[i].encoded, = NULL)); + qint =3D qobject_to_qint(qobject_from_json(test_cases[i].encoded, + &error_abort)); g_assert(qint); g_assert(qint_get_int(qint) =3D=3D test_cases[i].decoded); if (test_cases[i].skip =3D=3D 0) { @@ -920,7 +923,7 @@ static void float_number(void) QObject *obj; QFloat *qfloat; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); qfloat =3D qobject_to_qfloat(obj); g_assert(qfloat); g_assert(qfloat_get_double(qfloat) =3D=3D test_cases[i].decoded); @@ -965,7 +968,7 @@ static void keyword_literal(void) QObject *null; QString *str; =20 - obj =3D qobject_from_json("true", NULL); + obj =3D qobject_from_json("true", &error_abort); qbool =3D qobject_to_qbool(obj); g_assert(qbool); g_assert(qbool_get_bool(qbool) =3D=3D true); @@ -976,7 +979,7 @@ static void keyword_literal(void) =20 QDECREF(qbool); =20 - obj =3D qobject_from_json("false", NULL); + obj =3D qobject_from_json("false", &error_abort); qbool =3D qobject_to_qbool(obj); g_assert(qbool); g_assert(qbool_get_bool(qbool) =3D=3D false); @@ -998,7 +1001,7 @@ static void keyword_literal(void) g_assert(qbool_get_bool(qbool) =3D=3D true); QDECREF(qbool); =20 - obj =3D qobject_from_json("null", NULL); + obj =3D qobject_from_json("null", &error_abort); g_assert(obj !=3D NULL); g_assert(qobject_type(obj) =3D=3D QTYPE_QNULL); =20 @@ -1134,13 +1137,13 @@ static void simple_dict(void) QObject *obj; QString *str; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) =3D= =3D 1); =20 str =3D qobject_to_json(obj); qobject_decref(obj); =20 - obj =3D qobject_from_json(qstring_get_str(str), NULL); + obj =3D qobject_from_json(qstring_get_str(str), &error_abort); g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) =3D= =3D 1); qobject_decref(obj); QDECREF(str); @@ -1192,7 +1195,7 @@ static void large_dict(void) QObject *obj; =20 gen_test_json(gstr, 10, 100); - obj =3D qobject_from_json(gstr->str, NULL); + obj =3D qobject_from_json(gstr->str, &error_abort); g_assert(obj !=3D NULL); =20 qobject_decref(obj); @@ -1243,13 +1246,13 @@ static void simple_list(void) QObject *obj; QString *str; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) =3D= =3D 1); =20 str =3D qobject_to_json(obj); qobject_decref(obj); =20 - obj =3D qobject_from_json(qstring_get_str(str), NULL); + obj =3D qobject_from_json(qstring_get_str(str), &error_abort); g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) =3D= =3D 1); qobject_decref(obj); QDECREF(str); @@ -1305,13 +1308,13 @@ static void simple_whitespace(void) QObject *obj; QString *str; =20 - obj =3D qobject_from_json(test_cases[i].encoded, NULL); + obj =3D qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) =3D= =3D 1); =20 str =3D qobject_to_json(obj); qobject_decref(obj); =20 - obj =3D qobject_from_json(qstring_get_str(str), NULL); + obj =3D qobject_from_json(qstring_get_str(str), &error_abort); g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) =3D= =3D 1); =20 qobject_decref(obj); @@ -1332,7 +1335,7 @@ static void simple_varargs(void) {}})), {}})); =20 - embedded_obj =3D qobject_from_json("[32, 42]", NULL); + 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); @@ -1344,68 +1347,87 @@ static void simple_varargs(void) static void empty_input(void) { const char *empty =3D ""; - - QObject *obj =3D qobject_from_json(empty, NULL); + QObject *obj =3D qobject_from_json(empty, &error_abort); g_assert(obj =3D=3D NULL); } =20 static void unterminated_string(void) { - QObject *obj =3D qobject_from_json("\"abc", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("\"abc", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void unterminated_sq_string(void) { - QObject *obj =3D qobject_from_json("'abc", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("'abc", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void unterminated_escape(void) { - QObject *obj =3D qobject_from_json("\"abc\\\"", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("\"abc\\\"", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void unterminated_array(void) { - QObject *obj =3D qobject_from_json("[32", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("[32", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void unterminated_array_comma(void) { - QObject *obj =3D qobject_from_json("[32,", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("[32,", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void invalid_array_comma(void) { - QObject *obj =3D qobject_from_json("[32,}", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("[32,}", &err); + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 static void unterminated_dict(void) { - QObject *obj =3D qobject_from_json("{'abc':32", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("{'abc':32", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void unterminated_dict_comma(void) { - QObject *obj =3D qobject_from_json("{'abc':32,", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("{'abc':32,", &err); + g_assert(!err); /* BUG */ g_assert(obj =3D=3D NULL); } =20 static void invalid_dict_comma(void) { - QObject *obj =3D qobject_from_json("{'abc':32,}", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("{'abc':32,}", &err); + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 static void unterminated_literal(void) { - QObject *obj =3D qobject_from_json("nul", NULL); + Error *err =3D NULL; + QObject *obj =3D qobject_from_json("nul", &err); + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 @@ -1421,15 +1443,17 @@ static char *make_nest(char *buf, size_t cnt) =20 static void limits_nesting(void) { + Error *err =3D NULL; enum { max_nesting =3D 1024 }; /* see qobject/json-streamer.c */ char buf[2 * (max_nesting + 1) + 1]; QObject *obj; =20 - obj =3D qobject_from_json(make_nest(buf, max_nesting), NULL); + obj =3D qobject_from_json(make_nest(buf, max_nesting), &error_abort); g_assert(obj !=3D NULL); qobject_decref(obj); =20 - obj =3D qobject_from_json(make_nest(buf, max_nesting + 1), NULL); + obj =3D qobject_from_json(make_nest(buf, max_nesting + 1), &err); + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 --=20 2.7.4