From nobody Mon Apr 29 11:53:51 2024 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 1525967004264189.96986971971558; Thu, 10 May 2018 08:43:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0242430C0972; Thu, 10 May 2018 15:43:22 +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 22E7C9D7B4; Thu, 10 May 2018 15:43: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 F1E374BB79; Thu, 10 May 2018 15:43:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4AFhH1P002838 for ; Thu, 10 May 2018 11:43:17 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5A9349D7B8; Thu, 10 May 2018 15:43:17 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-94.phx2.redhat.com [10.3.116.94]) by smtp.corp.redhat.com (Postfix) with ESMTP id DC3EA9D7B7 for ; Thu, 10 May 2018 15:43:14 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Thu, 10 May 2018 11:43:11 -0400 Message-Id: <20180510154311.12882-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.27 X-loop: libvir-list@redhat.com Subject: [libvirt] [REPOST PATCH] util: Clean up consumers of virJSONValueArraySize 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.84 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Thu, 10 May 2018 15:43:23 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than have virJSONValueArraySize return a -1 when the input is not an array and then splat an error message, let's check for an array before calling and then change the return to be a size_t instead of ssize_t. That means using the helper virJSONValueIsArray as well as using a more generic error message such as "Malformed array". In some cases we can remove stack variables and when we cannot, those variables should be size_t not ssize_t. Alter a few references of if (!value) to be if (value =3D=3D 0) instead as well. Some callers can already assume an array is being worked on based on the previous call, so there's less to do. Signed-off-by: John Ferlan Reviewed-by: J=EF=BF=BDn Tomko --- Original: https://www.redhat.com/archives/libvir-list/2018-April/msg02003.html Reposting as I found a recent upstream changes created some simple merge conflicts and it's never clear if applying what was there can be applied on top even with git am -3, so I figured I would just repost. As noted previously, based off a review of the Coverity work from Peter: https://www.redhat.com/archives/libvir-list/2018-April/msg01613.html src/locking/lock_daemon.c | 7 ++-- src/logging/log_handler.c | 7 ++-- src/network/bridge_driver.c | 7 ++-- src/qemu/qemu_agent.c | 35 ++++++++++-------- src/qemu/qemu_monitor_json.c | 85 ++++++++++++++++++++-------------------= ---- src/rpc/virnetdaemon.c | 7 +--- src/rpc/virnetserver.c | 15 +++----- src/rpc/virnetserverservice.c | 7 ++-- src/util/virjson.c | 5 +-- src/util/virjson.h | 2 +- src/util/virlockspace.c | 16 ++++---- src/util/virstoragefile.c | 3 +- tests/testutilsqemuschema.c | 18 +++------ tools/nss/libvirt_nss.c | 3 +- 14 files changed, 97 insertions(+), 120 deletions(-) diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index 7afff42246..78c33bd29c 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -248,7 +248,6 @@ virLockDaemonNewPostExecRestart(virJSONValuePtr object,= bool privileged) virJSONValuePtr child; virJSONValuePtr lockspaces; size_t i; - ssize_t n; const char *serverNames[] =3D { "virtlockd" }; =20 if (VIR_ALLOC(lockd) < 0) @@ -281,13 +280,13 @@ virLockDaemonNewPostExecRestart(virJSONValuePtr objec= t, bool privileged) goto error; } =20 - if ((n =3D virJSONValueArraySize(lockspaces)) < 0) { + if (!virJSONValueIsArray(lockspaces)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed lockspaces data from JSON file")); + _("Malformed lockspaces array")); goto error; } =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(lockspaces); i++) { virLockSpacePtr lockspace; =20 child =3D virJSONValueArrayGet(lockspaces, i); diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c index 40dfa8ecae..5364e06dff 100644 --- a/src/logging/log_handler.c +++ b/src/logging/log_handler.c @@ -292,7 +292,6 @@ virLogHandlerNewPostExecRestart(virJSONValuePtr object, { virLogHandlerPtr handler; virJSONValuePtr files; - ssize_t n; size_t i; =20 if (!(handler =3D virLogHandlerNew(privileged, @@ -308,13 +307,13 @@ virLogHandlerNewPostExecRestart(virJSONValuePtr objec= t, goto error; } =20 - if ((n =3D virJSONValueArraySize(files)) < 0) { + if (!virJSONValueIsArray(files)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed files data from JSON file")); + _("Malformed files array")); goto error; } =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(files); i++) { virLogHandlerLogFilePtr file; virJSONValuePtr child =3D virJSONValueArrayGet(files, i); =20 diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index ece7e41d35..4b33e12b30 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -4124,7 +4124,7 @@ networkGetDHCPLeases(virNetworkPtr net, size_t i, j; size_t nleases =3D 0; int rv =3D -1; - ssize_t size =3D 0; + size_t size =3D 0; int custom_lease_file_len =3D 0; bool need_results =3D !!leases; long long currtime =3D 0; @@ -4179,11 +4179,12 @@ networkGetDHCPLeases(virNetworkPtr net, goto error; } =20 - if ((size =3D virJSONValueArraySize(leases_array)) < 0) { + if (!virJSONValueIsArray(leases_array)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("couldn't fetch array of leases")); + _("Malformed lease_entries array")); goto error; } + size =3D virJSONValueArraySize(leases_array); } =20 currtime =3D (long long)time(NULL); diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index 4df1bde458..eeede645ef 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -1490,7 +1490,7 @@ qemuAgentGetVCPUs(qemuAgentPtr mon, virJSONValuePtr cmd; virJSONValuePtr reply =3D NULL; virJSONValuePtr data =3D NULL; - ssize_t ndata; + size_t ndata; =20 if (!(cmd =3D qemuAgentMakeCommand("guest-get-vcpus", NULL))) return -1; @@ -1505,6 +1505,12 @@ qemuAgentGetVCPUs(qemuAgentPtr mon, goto cleanup; } =20 + if (!virJSONValueIsArray(data)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Malformed guest-get-vcpus data array")); + goto cleanup; + } + ndata =3D virJSONValueArraySize(data); =20 if (VIR_ALLOC_N(*info, ndata) < 0) @@ -1869,15 +1875,14 @@ qemuAgentGetFSInfo(qemuAgentPtr mon, virDomainFSInf= oPtr **info, goto cleanup; } =20 - if (virJSONValueGetType(data) !=3D VIR_JSON_TYPE_ARRAY) { + if (!virJSONValueIsArray(data)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("guest-get-fsinfo return information was not " - "an array")); + _("Malformed guest-get-fsinfo data array")); goto cleanup; } =20 ndata =3D virJSONValueArraySize(data); - if (!ndata) { + if (ndata =3D=3D 0) { ret =3D 0; *info =3D NULL; goto cleanup; @@ -1928,14 +1933,14 @@ qemuAgentGetFSInfo(qemuAgentPtr mon, virDomainFSInf= oPtr **info, goto cleanup; } =20 - if (virJSONValueGetType(entry) !=3D VIR_JSON_TYPE_ARRAY) { + if (!virJSONValueIsArray(entry)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("guest-get-fsinfo 'disk' data was not an arra= y")); + _("Malformed guest-get-fsinfo 'disk' data array= ")); goto cleanup; } =20 ndisk =3D virJSONValueArraySize(entry); - if (!ndisk) + if (ndisk =3D=3D 0) continue; if (VIR_ALLOC_N(info_ret[i]->devAlias, ndisk) < 0) goto cleanup; @@ -2035,7 +2040,6 @@ qemuAgentGetInterfaces(qemuAgentPtr mon, { int ret =3D -1; size_t i, j; - ssize_t size =3D -1; virJSONValuePtr cmd =3D NULL; virJSONValuePtr reply =3D NULL; virJSONValuePtr ret_array =3D NULL; @@ -2065,17 +2069,16 @@ qemuAgentGetInterfaces(qemuAgentPtr mon, goto cleanup; } =20 - if ((size =3D virJSONValueArraySize(ret_array)) < 0) { + if (!virJSONValueIsArray(ret_array)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("qemu agent didn't return an array of interfaces"= )); goto cleanup; } =20 - for (i =3D 0; i < size; i++) { + for (i =3D 0; i < virJSONValueArraySize(ret_array); i++) { virJSONValuePtr tmp_iface =3D virJSONValueArrayGet(ret_array, i); virJSONValuePtr ip_addr_arr =3D NULL; const char *hwaddr, *ifname_s, *name =3D NULL; - ssize_t ip_addr_arr_size; virDomainInterfacePtr iface =3D NULL; =20 /* Shouldn't happen but doesn't hurt to check neither */ @@ -2131,14 +2134,16 @@ qemuAgentGetInterfaces(qemuAgentPtr mon, if (!ip_addr_arr) continue; =20 - if ((ip_addr_arr_size =3D virJSONValueArraySize(ip_addr_arr)) < 0) - /* Mmm, empty 'ip-address'? */ + if (!virJSONValueIsArray(ip_addr_arr)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Malformed ip-addresses array")); goto error; + } =20 /* If current iface already exists, continue with the count */ addrs_count =3D iface->naddrs; =20 - for (j =3D 0; j < ip_addr_arr_size; j++) { + for (j =3D 0; j < virJSONValueArraySize(ip_addr_arr); j++) { const char *type, *addr; virJSONValuePtr ip_addr_obj =3D virJSONValueArrayGet(ip_addr_a= rr, j); virDomainIPAddressPtr ip_addr; diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 8176175894..e0ea553c41 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -1627,9 +1627,9 @@ qemuMonitorJSONExtractCPUInfo(virJSONValuePtr data, struct qemuMonitorQueryCpusEntry *cpus =3D NULL; int ret =3D -1; size_t i; - ssize_t ncpus; + size_t ncpus; =20 - if ((ncpus =3D virJSONValueArraySize(data)) <=3D 0) + if ((ncpus =3D virJSONValueArraySize(data)) =3D=3D 0) return -2; =20 if (VIR_ALLOC_N(cpus, ncpus) < 0) @@ -3595,7 +3595,7 @@ qemuMonitorJSONQueryRxFilterParse(virJSONValuePtr msg, int ret =3D -1; const char *tmp; virJSONValuePtr returnArray, entry, table, element; - ssize_t nTable; + size_t nTable; size_t i; virNetDevRxFilterPtr fil =3D virNetDevRxFilterNew(); =20 @@ -3656,12 +3656,13 @@ qemuMonitorJSONQueryRxFilterParse(virJSONValuePtr m= sg, goto cleanup; } if ((!(table =3D virJSONValueObjectGet(entry, "unicast-table"))) || - ((nTable =3D virJSONValueArraySize(table)) < 0)) { + (!virJSONValueIsArray(table))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing or invalid 'unicast-table' array " "in query-rx-filter response")); goto cleanup; } + nTable =3D virJSONValueArraySize(table); if (VIR_ALLOC_N(fil->unicast.table, nTable)) goto cleanup; for (i =3D 0; i < nTable; i++) { @@ -3697,12 +3698,13 @@ qemuMonitorJSONQueryRxFilterParse(virJSONValuePtr m= sg, goto cleanup; } if ((!(table =3D virJSONValueObjectGet(entry, "multicast-table"))) || - ((nTable =3D virJSONValueArraySize(table)) < 0)) { + (!virJSONValueIsArray(table))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing or invalid 'multicast-table' array " "in query-rx-filter response")); goto cleanup; } + nTable =3D virJSONValueArraySize(table); if (VIR_ALLOC_N(fil->multicast.table, nTable)) goto cleanup; for (i =3D 0; i < nTable; i++) { @@ -3731,12 +3733,13 @@ qemuMonitorJSONQueryRxFilterParse(virJSONValuePtr m= sg, goto cleanup; } if ((!(table =3D virJSONValueObjectGet(entry, "vlan-table"))) || - ((nTable =3D virJSONValueArraySize(table)) < 0)) { + (!virJSONValueIsArray(table))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing or invalid 'vlan-table' array " "in query-rx-filter response")); goto cleanup; } + nTable =3D virJSONValueArraySize(table); if (VIR_ALLOC_N(fil->vlan.table, nTable)) goto cleanup; for (i =3D 0; i < nTable; i++) { @@ -4575,7 +4578,7 @@ qemuMonitorJSONGetAllBlockJobInfo(qemuMonitorPtr mon) virJSONValuePtr cmd =3D NULL; virJSONValuePtr reply =3D NULL; virJSONValuePtr data; - ssize_t nr_results; + size_t nr_results; size_t i; virHashTablePtr blockJobs =3D NULL; =20 @@ -4591,12 +4594,7 @@ qemuMonitorJSONGetAllBlockJobInfo(qemuMonitorPtr mon) goto cleanup; } =20 - if ((nr_results =3D virJSONValueArraySize(data)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("unable to determine array size")); - goto cleanup; - } - + nr_results =3D virJSONValueArraySize(data); if (!(blockJobs =3D virHashCreate(nr_results, virHashValueFree))) goto cleanup; =20 @@ -5114,7 +5112,7 @@ int qemuMonitorJSONGetMachines(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; qemuMonitorMachineInfoPtr *infolist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *machines =3D NULL; @@ -5206,7 +5204,7 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; qemuMonitorCPUDefInfoPtr *cpulist =3D NULL; - int n =3D 0; + size_t n =3D 0; size_t i; =20 *cpus =3D NULL; @@ -5257,7 +5255,7 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon, if (virJSONValueObjectHasKey(child, "unavailable-features")) { virJSONValuePtr blockers; size_t j; - int len; + size_t len; =20 blockers =3D virJSONValueObjectGetArray(child, "unavailable-features"); @@ -5494,7 +5492,7 @@ int qemuMonitorJSONGetCommands(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; char **commandlist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *commands =3D NULL; @@ -5550,7 +5548,7 @@ int qemuMonitorJSONGetEvents(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; char **eventlist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *events =3D NULL; @@ -5614,7 +5612,7 @@ qemuMonitorJSONGetCommandLineOptionParameters(qemuMon= itorPtr mon, virJSONValuePtr data =3D NULL; virJSONValuePtr array =3D NULL; char **paramlist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *params =3D NULL; @@ -5646,17 +5644,17 @@ qemuMonitorJSONGetCommandLineOptionParameters(qemuM= onitorPtr mon, "return data")); goto cleanup; } - qemuMonitorSetOptions(mon, array); - } =20 - if ((n =3D virJSONValueArraySize(array)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("query-command-line-options reply data was not " - "an array")); - goto cleanup; + if (!virJSONValueIsArray(array)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Malformed query-cmmand-line-options array")); + goto cleanup; + } + + qemuMonitorSetOptions(mon, array); } =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(array); i++) { virJSONValuePtr child =3D virJSONValueArrayGet(array, i); const char *tmp; =20 @@ -5681,12 +5679,12 @@ qemuMonitorJSONGetCommandLineOptionParameters(qemuM= onitorPtr mon, if (found) *found =3D true; =20 - if ((n =3D virJSONValueArraySize(data)) < 0) { + if (!virJSONValueIsArray(data)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("query-command-line-options parameter data was no= t " - "an array")); + _("Malformed query-cmmand-line-options parameters a= rray")); goto cleanup; } + n =3D virJSONValueArraySize(data); =20 /* null-terminated list */ if (VIR_ALLOC_N(paramlist, n + 1) < 0) @@ -5776,7 +5774,7 @@ int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; char **typelist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *types =3D NULL; @@ -5832,7 +5830,7 @@ int qemuMonitorJSONGetObjectListPaths(qemuMonitorPtr = mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; qemuMonitorJSONListPathPtr *pathlist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *paths =3D NULL; @@ -6062,7 +6060,7 @@ int qemuMonitorJSONGetDeviceProps(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; char **proplist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *props =3D NULL; @@ -6161,7 +6159,7 @@ qemuMonitorJSONGetMigrationCapabilities(qemuMonitorPt= r mon, virJSONValuePtr caps; char **list =3D NULL; size_t i; - ssize_t n; + size_t n; =20 *capabilities =3D NULL; =20 @@ -6272,7 +6270,7 @@ qemuMonitorJSONGetGICCapabilities(qemuMonitorPtr mon, virJSONValuePtr caps; virGICCapability *list =3D NULL; size_t i; - ssize_t n; + size_t n; =20 *capabilities =3D NULL; =20 @@ -6495,7 +6493,7 @@ qemuMonitorJSONGetStringArray(qemuMonitorPtr mon, con= st char *qmpCmd, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; char **list =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *array =3D NULL; @@ -6892,14 +6890,11 @@ qemuMonitorJSONParseCPUx86Features(virJSONValuePtr = data) virCPUDataPtr cpudata =3D NULL; virCPUx86CPUID cpuid; size_t i; - ssize_t n; - - n =3D virJSONValueArraySize(data); =20 if (!(cpudata =3D virCPUDataNew(VIR_ARCH_X86_64))) goto error; =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(data); i++) { if (qemuMonitorJSONParseCPUx86FeatureWord(virJSONValueArrayGet(dat= a, i), &cpuid) < 0 || virCPUx86DataAddCPUID(cpudata, &cpuid) < 0) @@ -6960,7 +6955,7 @@ qemuMonitorJSONCheckCPUx86(qemuMonitorPtr mon) virJSONValuePtr reply =3D NULL; virJSONValuePtr data; size_t i; - ssize_t n; + size_t n; int ret =3D -1; =20 if (!(cmd =3D qemuMonitorJSONMakeCommand("qom-list", @@ -7098,7 +7093,7 @@ qemuMonitorJSONGetIOThreads(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data; qemuMonitorIOThreadInfoPtr *infolist =3D NULL; - ssize_t n =3D 0; + size_t n =3D 0; size_t i; =20 *iothreads =3D NULL; @@ -7180,7 +7175,6 @@ qemuMonitorJSONGetMemoryDeviceInfo(qemuMonitorPtr mon, virJSONValuePtr reply =3D NULL; virJSONValuePtr data =3D NULL; qemuMonitorMemoryDeviceInfoPtr meminfo =3D NULL; - ssize_t n; size_t i; =20 if (!(cmd =3D qemuMonitorJSONMakeCommand("query-memory-devices", NULL)= )) @@ -7198,9 +7192,8 @@ qemuMonitorJSONGetMemoryDeviceInfo(qemuMonitorPtr mon, goto cleanup; =20 data =3D virJSONValueObjectGetArray(reply, "return"); - n =3D virJSONValueArraySize(data); =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(data); i++) { virJSONValuePtr elem =3D virJSONValueArrayGet(data, i); const char *type; =20 @@ -7666,7 +7659,7 @@ qemuMonitorJSONGetHotpluggableCPUs(qemuMonitorPtr mon, size_t *nentries) { struct qemuMonitorQueryHotpluggableCpusEntry *info =3D NULL; - ssize_t ninfo =3D 0; + size_t ninfo =3D 0; int ret =3D -1; size_t i; virJSONValuePtr data; diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c index eb0b822545..31b687de12 100644 --- a/src/rpc/virnetdaemon.c +++ b/src/rpc/virnetdaemon.c @@ -322,12 +322,7 @@ virNetDaemonNewPostExecRestart(virJSONValuePtr object, goto error; } else if (virJSONValueIsArray(servers)) { size_t i; - ssize_t n =3D virJSONValueArraySize(servers); - if (n < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Server count %zd should be positive"), n); - goto error; - } + size_t n =3D virJSONValueArraySize(servers); if (n > nDefServerNames) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Server count %zd greater than default name c= ount %zu"), diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index f4105b1394..5aeb188900 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -411,7 +411,6 @@ virNetServerPtr virNetServerNewPostExecRestart(virJSONV= aluePtr object, virJSONValuePtr clients; virJSONValuePtr services; size_t i; - ssize_t n; unsigned int min_workers; unsigned int max_workers; unsigned int priority_workers; @@ -492,14 +491,13 @@ virNetServerPtr virNetServerNewPostExecRestart(virJSO= NValuePtr object, goto error; } =20 - n =3D virJSONValueArraySize(services); - if (n < 0) { + if (!virJSONValueIsArray(services)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed services data in JSON document")); + _("Malformed services array")); goto error; } =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(services); i++) { virNetServerServicePtr service; virJSONValuePtr child =3D virJSONValueArrayGet(services, i); if (!child) { @@ -525,14 +523,13 @@ virNetServerPtr virNetServerNewPostExecRestart(virJSO= NValuePtr object, goto error; } =20 - n =3D virJSONValueArraySize(clients); - if (n < 0) { + if (!virJSONValueIsArray(clients)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed clients data in JSON document")); + _("Malformed clients array")); goto error; } =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(clients); i++) { virNetServerClientPtr client; virJSONValuePtr child =3D virJSONValueArrayGet(clients, i); if (!child) { diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c index fb19d09dda..23fc23cab4 100644 --- a/src/rpc/virnetserverservice.c +++ b/src/rpc/virnetserverservice.c @@ -325,7 +325,7 @@ virNetServerServicePtr virNetServerServiceNewPostExecRe= start(virJSONValuePtr obj virNetServerServicePtr svc; virJSONValuePtr socks; size_t i; - ssize_t n; + size_t n; unsigned int max; =20 if (virNetServerServiceInitialize() < 0) @@ -358,12 +358,13 @@ virNetServerServicePtr virNetServerServiceNewPostExec= Restart(virJSONValuePtr obj goto error; } =20 - if ((n =3D virJSONValueArraySize(socks)) < 0) { + if (!virJSONValueIsArray(socks)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("socks field in JSON was not an array")); + _("Malformed socks array")); goto error; } =20 + n =3D virJSONValueArraySize(socks); if (VIR_ALLOC_N(svc->socks, n) < 0) goto error; svc->nsocks =3D n; diff --git a/src/util/virjson.c b/src/util/virjson.c index dfe00d9280..bb4e3c257b 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -971,12 +971,9 @@ virJSONValueIsArray(virJSONValuePtr array) } =20 =20 -ssize_t +size_t virJSONValueArraySize(const virJSONValue *array) { - if (array->type !=3D VIR_JSON_TYPE_ARRAY) - return -1; - return array->data.array.nvalues; } =20 diff --git a/src/util/virjson.h b/src/util/virjson.h index 0f098892b4..e4a82bdbc8 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -81,7 +81,7 @@ virJSONValuePtr virJSONValueObjectGetByType(virJSONValueP= tr object, bool virJSONValueIsObject(virJSONValuePtr object); =20 bool virJSONValueIsArray(virJSONValuePtr array); -ssize_t virJSONValueArraySize(const virJSONValue *array); +size_t virJSONValueArraySize(const virJSONValue *array); virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int = element); virJSONValuePtr virJSONValueArraySteal(virJSONValuePtr object, unsigned in= t element); typedef int (*virJSONArrayIteratorFunc)(size_t pos, diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c index 41af0cdb6d..3364c843aa 100644 --- a/src/util/virlockspace.c +++ b/src/util/virlockspace.c @@ -293,7 +293,6 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONV= aluePtr object) { virLockSpacePtr lockspace; virJSONValuePtr resources; - ssize_t n; size_t i; =20 VIR_DEBUG("object=3D%p", object); @@ -324,19 +323,19 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSO= NValuePtr object) goto error; } =20 - if ((n =3D virJSONValueArraySize(resources)) < 0) { + if (!virJSONValueIsArray(resources)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed resources value in JSON document")); + _("Malformed resources array")); goto error; } =20 - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(resources); i++) { virJSONValuePtr child =3D virJSONValueArrayGet(resources, i); virLockSpaceResourcePtr res; const char *tmp; virJSONValuePtr owners; size_t j; - ssize_t m; + size_t m; =20 if (VIR_ALLOC(res) < 0) goto error; @@ -396,18 +395,19 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSO= NValuePtr object) goto error; } =20 - if ((m =3D virJSONValueArraySize(owners)) < 0) { + if (!virJSONValueIsArray(owners)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Malformed owners value in JSON document")); + _("Malformed owners array")); virLockSpaceResourceFree(res); goto error; } =20 - res->nOwners =3D m; + m =3D virJSONValueArraySize(owners); if (VIR_ALLOC_N(res->owners, res->nOwners) < 0) { virLockSpaceResourceFree(res); goto error; } + res->nOwners =3D m; =20 for (j =3D 0; j < res->nOwners; j++) { unsigned long long int owner; diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index eae1dc01dc..275ab9b54b 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -2928,8 +2928,7 @@ virStorageSourceParseBackingJSONGluster(virStorageSou= rcePtr src, return -1; =20 nservers =3D virJSONValueArraySize(server); - - if (nservers < 1) { + if (nservers =3D=3D 0) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("at least 1 server is necessary in " "JSON backing definition for gluster volume")); diff --git a/tests/testutilsqemuschema.c b/tests/testutilsqemuschema.c index 21f5d119e8..46bbc4f1e5 100644 --- a/tests/testutilsqemuschema.c +++ b/tests/testutilsqemuschema.c @@ -101,11 +101,9 @@ testQEMUSchemaStealObjectMemberByName(const char *name, { virJSONValuePtr member; virJSONValuePtr ret =3D NULL; - size_t n; size_t i; =20 - n =3D virJSONValueArraySize(members); - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(members); i++) { member =3D virJSONValueArrayGet(members, i); =20 if (STREQ_NULLABLE(name, virJSONValueObjectGetString(member, "name= "))) { @@ -188,7 +186,6 @@ testQEMUSchemaValidateObjectMergeVariant(virJSONValuePt= r root, virHashTablePtr schema, virBufferPtr debug) { - size_t n; size_t i; virJSONValuePtr variants =3D NULL; virJSONValuePtr variant; @@ -203,8 +200,7 @@ testQEMUSchemaValidateObjectMergeVariant(virJSONValuePt= r root, return -2; } =20 - n =3D virJSONValueArraySize(variants); - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(variants); i++) { variant =3D virJSONValueArrayGet(variants, i); =20 if (STREQ_NULLABLE(variantname, @@ -342,7 +338,6 @@ testQEMUSchemaValidateEnum(virJSONValuePtr obj, const char *objstr; virJSONValuePtr values =3D NULL; virJSONValuePtr value; - size_t n; size_t i; =20 if (virJSONValueGetType(obj) !=3D VIR_JSON_TYPE_STRING) { @@ -358,8 +353,7 @@ testQEMUSchemaValidateEnum(virJSONValuePtr obj, return -2; } =20 - n =3D virJSONValueArraySize(values); - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(values); i++) { value =3D virJSONValueArrayGet(values, i); =20 if (STREQ_NULLABLE(objstr, virJSONValueGetString(value))) { @@ -383,7 +377,6 @@ testQEMUSchemaValidateArray(virJSONValuePtr objs, const char *elemtypename =3D virJSONValueObjectGetString(root, "elemen= t-type"); virJSONValuePtr elementschema; virJSONValuePtr obj; - size_t n; size_t i; =20 if (virJSONValueGetType(objs) !=3D VIR_JSON_TYPE_ARRAY) { @@ -401,8 +394,7 @@ testQEMUSchemaValidateArray(virJSONValuePtr objs, virBufferAddLit(debug, "[\n"); virBufferAdjustIndent(debug, 3); =20 - n =3D virJSONValueArraySize(objs); - for (i =3D 0; i < n; i++) { + for (i =3D 0; i < virJSONValueArraySize(objs); i++) { obj =3D virJSONValueArrayGet(objs, i); =20 if (testQEMUSchemaValidateRecurse(obj, elementschema, schema, debu= g) < 0) @@ -423,8 +415,8 @@ testQEMUSchemaValidateAlternate(virJSONValuePtr obj, { virJSONValuePtr members; virJSONValuePtr member; - size_t n; size_t i; + size_t n; const char *membertype; virJSONValuePtr memberschema; int indent; diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c index 62fe589bed..ec73ea575a 100644 --- a/tools/nss/libvirt_nss.c +++ b/tools/nss/libvirt_nss.c @@ -309,8 +309,7 @@ findLease(const char *name, } VIR_DIR_CLOSE(dir); =20 - if ((nleases =3D virJSONValueArraySize(leases_array)) < 0) - goto cleanup; + nleases =3D virJSONValueArraySize(leases_array); DEBUG("Read %zd leases", nleases); =20 #if !defined(LIBVIRT_NSS_GUEST) --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list