From nobody Mon Sep 16 19:08:36 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 1539865653581474.2043385619578; Thu, 18 Oct 2018 05:27:33 -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 5A92580462; Thu, 18 Oct 2018 12:27:31 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 137EC46E70; Thu, 18 Oct 2018 12:27:31 +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 85ECD18005B4; Thu, 18 Oct 2018 12:27:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9ICRCt6025933 for ; Thu, 18 Oct 2018 08:27:12 -0400 Received: by smtp.corp.redhat.com (Postfix) id 87CBF60922; Thu, 18 Oct 2018 12:27:12 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0C898611BC for ; Thu, 18 Oct 2018 12:27:11 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Oct 2018 14:26:45 +0200 Message-Id: <62952878a9048576caebbe8c3245d1b14f7d3358.1539865556.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [snmp PATCH 07/20] libvirtSnmp: Modernize insertGuest 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.28]); Thu, 18 Oct 2018 12:27:32 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Firstly, this style of comparison makes my eyes bleed: if (-1 =3D=3D some_int) Secondly, the retval is initialized to zero and then in every error path it is set to -1. Le sigh. Thirdly, virDomainGetName() can't fail at the point we are calling it (it can fail iff passed virDomainPtr is invalid, but that can't be the case). Signed-off-by: Michal Privoznik --- src/libvirtSnmp.c | 68 +++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c index 589fd03..1e7e345 100644 --- a/src/libvirtSnmp.c +++ b/src/libvirtSnmp.c @@ -87,54 +87,47 @@ showError(virConnectPtr conn) static int insertGuest(netsnmp_container *container, virDomainPtr domain) { - int ret =3D 0; virDomainInfo info; libvirtGuestTable_rowreq_ctx *row_ctx =3D NULL; - const char *name =3D NULL; - unsigned char uuid[16]; + const char *name; + unsigned char uuid[VIR_UUID_BUFLEN]; =20 - if (-1 =3D=3D virDomainGetInfo(domain, &info)) { + if (virDomainGetInfo(domain, &info) < 0) { printf("Failed to get domain info\n"); showError(conn); - ret =3D -1; - goto out; + goto error; } =20 /* create new row in the container */ - row_ctx =3D libvirtGuestTable_allocate_rowreq_ctx(NULL); - if (!row_ctx) { + if (!(row_ctx =3D libvirtGuestTable_allocate_rowreq_ctx(NULL))) { snmp_log(LOG_ERR, "Error creating row"); - ret =3D -1; - goto out; + goto error; } =20 /* set the index of the row */ - ret =3D virDomainGetUUID(domain, uuid); - if (ret) { - snmp_log(LOG_ERR, "Cannot get UUID"); - libvirtGuestTable_release_rowreq_ctx(row_ctx); - ret =3D -1; - goto out; + if (virDomainGetUUID(domain, uuid) < 0) { + printf("Failed to get domain UUID\n"); + showError(conn); + goto error; } - if (MFD_SUCCESS !=3D libvirtGuestTable_indexes_set(row_ctx, (char*) uu= id, - sizeof(uuid))) { + + if (libvirtGuestTable_indexes_set(row_ctx, + (char*) uuid, + sizeof(uuid)) !=3D MFD_SUCCESS) { snmp_log(LOG_ERR, "Error setting row index"); - libvirtGuestTable_release_rowreq_ctx(row_ctx); - ret =3D -1; - goto out; + goto error; } =20 /* set the data */ - name =3D virDomainGetName(domain); - if (name) - row_ctx->data.libvirtGuestName =3D strdup(name); - else - row_ctx->data.libvirtGuestName =3D strdup(""); - if (!row_ctx->data.libvirtGuestName) { + if (!(name =3D virDomainGetName(domain))) { + printf("Failed to get domain name\n"); + showError(conn); + goto error; + } + + if (!(row_ctx->data.libvirtGuestName =3D strdup(name))) { snmp_log(LOG_ERR, "Not enough memory for domain name '%s'", name); - libvirtGuestTable_release_rowreq_ctx(row_ctx); - ret =3D -1; - goto out; + goto error; } =20 row_ctx->data.libvirtGuestState =3D info.state; @@ -147,16 +140,17 @@ insertGuest(netsnmp_container *container, virDomainPt= r domain) =20 row_ctx->data.libvirtGuestRowStatus =3D ROWSTATUS_ACTIVE; =20 - ret =3D CONTAINER_INSERT(container, row_ctx); - if (ret) { + if (CONTAINER_INSERT(container, row_ctx) < 0) { snmp_log(LOG_ERR, "Cannot insert domain '%s' to container", name); - libvirtGuestTable_release_rowreq_ctx(row_ctx); - ret =3D -1; - goto out; + goto error; } =20 - out: - return ret; + return 0; + + error: + if (row_ctx) + libvirtGuestTable_release_rowreq_ctx(row_ctx); + return -1; } =20 /* --=20 2.18.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list