From nobody Wed May 14 12:07:28 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1523982202596769.4726661456623; Tue, 17 Apr 2018 09:23:22 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4E9853132585; Tue, 17 Apr 2018 16:23:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 22D457ED46; Tue, 17 Apr 2018 16:23:21 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 10BB84CAAC; Tue, 17 Apr 2018 16:23:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3HGND1A001862 for ; Tue, 17 Apr 2018 12:23:13 -0400 Received: by smtp.corp.redhat.com (Postfix) id E12A919E8C; Tue, 17 Apr 2018 16:23:13 +0000 (UTC) Received: from unknown54ee7586bd10.attlocal.net.com (ovpn-116-30.phx2.redhat.com [10.3.116.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2DDC19E8F for ; Tue, 17 Apr 2018 16:23:13 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Tue, 17 Apr 2018 12:22:16 -0400 Message-Id: <20180417162218.14378-6-jferlan@redhat.com> In-Reply-To: <20180417162218.14378-1-jferlan@redhat.com> References: <20180417162218.14378-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 5/7] tests: Add checks for possible errors X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 17 Apr 2018 16:23:21 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" If virJSONValueObjectGetArray fails to find "members" in @localroot, then using @rootmembers in subsequent calls which assume that it was successful will not go well. So add a check that it was successfully fetched and an error if not. Similarly virJSONValueArraySize returns an 'ssize_t', so the callers should use the right type and they should check the return value against -1 and print an error if something is wrong. Found by Coverity Signed-off-by: John Ferlan --- tests/testutilsqemuschema.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/tests/testutilsqemuschema.c b/tests/testutilsqemuschema.c index 21f5d119e8..79c526ac7c 100644 --- a/tests/testutilsqemuschema.c +++ b/tests/testutilsqemuschema.c @@ -97,14 +97,19 @@ struct testQEMUSchemaValidateObjectMemberData { =20 static virJSONValuePtr testQEMUSchemaStealObjectMemberByName(const char *name, - virJSONValuePtr members) + virJSONValuePtr members, + virBufferPtr debug) { virJSONValuePtr member; virJSONValuePtr ret =3D NULL; - size_t n; + ssize_t n; size_t i; =20 - n =3D virJSONValueArraySize(members); + if ((n =3D virJSONValueArraySize(members)) < 0) { + virBufferAddLit(debug, "ERROR: members size < 0"); + return NULL; + } + for (i =3D 0; i < n; i++) { member =3D virJSONValueArrayGet(members, i); =20 @@ -132,7 +137,7 @@ testQEMUSchemaValidateObjectMember(const char *key, virBufferStrcat(data->debug, key, ": ", NULL); =20 /* lookup 'member' entry for key */ - if (!(keymember =3D testQEMUSchemaStealObjectMemberByName(key, data->r= ootmembers))) { + if (!(keymember =3D testQEMUSchemaStealObjectMemberByName(key, data->r= ootmembers, data->debug))) { virBufferAddLit(data->debug, "ERROR: attribute not in schema"); goto cleanup; } @@ -188,7 +193,7 @@ testQEMUSchemaValidateObjectMergeVariant(virJSONValuePt= r root, virHashTablePtr schema, virBufferPtr debug) { - size_t n; + ssize_t n; size_t i; virJSONValuePtr variants =3D NULL; virJSONValuePtr variant; @@ -203,7 +208,11 @@ testQEMUSchemaValidateObjectMergeVariant(virJSONValueP= tr root, return -2; } =20 - n =3D virJSONValueArraySize(variants); + if ((n =3D virJSONValueArraySize(variants)) < 0) { + virBufferAddLit(debug, "ERROR: 'variants' array size < 0"); + return -2; + } + for (i =3D 0; i < n; i++) { variant =3D virJSONValueArrayGet(variants, i); =20 @@ -307,7 +316,10 @@ testQEMUSchemaValidateObject(virJSONValuePtr obj, =20 =20 /* validate members */ - data.rootmembers =3D virJSONValueObjectGetArray(localroot, "members"); + if (!(data.rootmembers =3D virJSONValueObjectGetArray(localroot, "memb= ers"))) { + virBufferAddLit(debug, "ERROR: missing attribute 'members'\n"); + goto cleanup; + } if (virJSONValueObjectForeachKeyValue(obj, testQEMUSchemaValidateObjectMemb= er, &data) < 0) @@ -342,7 +354,7 @@ testQEMUSchemaValidateEnum(virJSONValuePtr obj, const char *objstr; virJSONValuePtr values =3D NULL; virJSONValuePtr value; - size_t n; + ssize_t n; size_t i; =20 if (virJSONValueGetType(obj) !=3D VIR_JSON_TYPE_STRING) { @@ -358,7 +370,10 @@ testQEMUSchemaValidateEnum(virJSONValuePtr obj, return -2; } =20 - n =3D virJSONValueArraySize(values); + if ((n =3D virJSONValueArraySize(values)) < 0) { + virBufferAddLit(debug, "ERROR: enum values array size < 0"); + return -2; + } for (i =3D 0; i < n; i++) { value =3D virJSONValueArrayGet(values, i); =20 @@ -423,7 +438,7 @@ testQEMUSchemaValidateAlternate(virJSONValuePtr obj, { virJSONValuePtr members; virJSONValuePtr member; - size_t n; + ssize_t n; size_t i; const char *membertype; virJSONValuePtr memberschema; @@ -439,7 +454,10 @@ testQEMUSchemaValidateAlternate(virJSONValuePtr obj, virBufferAdjustIndent(debug, 3); indent =3D virBufferGetIndent(debug, false); =20 - n =3D virJSONValueArraySize(members); + if ((n =3D virJSONValueArraySize(members)) < 0) { + virBufferAddLit(debug, "ERROR: alternate schema 'members' array si= ze < 0"); + return -2; + } for (i =3D 0; i < n; i++) { membertype =3D NULL; =20 --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list