From nobody Sun Feb 8 16:30:53 2026 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 1549460558615627.4860698106019; Wed, 6 Feb 2019 05:42:38 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3B0B258E59; Wed, 6 Feb 2019 13:42:36 +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 D905326DC0; Wed, 6 Feb 2019 13:42:35 +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 6709F3D3A0; Wed, 6 Feb 2019 13:42:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x16Dg97b001999 for ; Wed, 6 Feb 2019 08:42:09 -0500 Received: by smtp.corp.redhat.com (Postfix) id 77EE65C26D; Wed, 6 Feb 2019 13:42:09 +0000 (UTC) Received: from unknown0050b6a41c42.attlocal.net.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F2DE5C5FD for ; Wed, 6 Feb 2019 13:42:09 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Wed, 6 Feb 2019 08:41:42 -0500 Message-Id: <20190206134147.18032-11-jferlan@redhat.com> In-Reply-To: <20190206134147.18032-1-jferlan@redhat.com> References: <20190206134147.18032-1-jferlan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 10/15] conf: Use VIR_AUTOFREE for storage_conf 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: , 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.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 06 Feb 2019 13:42:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Let's make use of the auto __cleanup capabilities cleaning up any now unnecessary goto paths. Signed-off-by: John Ferlan Reviewed-by: Erik Skultety --- src/conf/storage_conf.c | 179 +++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 105 deletions(-) diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index 4fb5fb9f57..5f1122422a 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -453,15 +453,15 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, xmlNodePtr node) { int ret =3D -1; - xmlNodePtr relnode, authnode, *nodeset =3D NULL; + xmlNodePtr relnode, authnode; xmlNodePtr adapternode; int nsource; size_t i; virStoragePoolOptionsPtr options; VIR_AUTOPTR(virStorageAuthDef) authdef =3D NULL; - char *name =3D NULL; - char *port =3D NULL; - char *ver =3D NULL; + VIR_AUTOFREE(char *) port =3D NULL; + VIR_AUTOFREE(char *) ver =3D NULL; + VIR_AUTOFREE(xmlNodePtr *) nodeset =3D NULL; int n; =20 relnode =3D ctxt->node; @@ -478,7 +478,9 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, } =20 if (options->formatFromString) { - char *format =3D virXPathString("string(./format/@type)", ctxt); + VIR_AUTOFREE(char *) format =3D NULL; + + format =3D virXPathString("string(./format/@type)", ctxt); if (format =3D=3D NULL) source->format =3D options->defaultFormat; else @@ -487,10 +489,8 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, if (source->format < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown pool format type %s"), format); - VIR_FREE(format); goto cleanup; } - VIR_FREE(format); } =20 if ((n =3D virXPathNodeSet("./host", ctxt, &nodeset)) < 0) @@ -502,13 +502,12 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, source->nhost =3D n; =20 for (i =3D 0; i < source->nhost; i++) { - name =3D virXMLPropString(nodeset[i], "name"); - if (name =3D=3D NULL) { + source->hosts[i].name =3D virXMLPropString(nodeset[i], "name"); + if (!source->hosts[i].name) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing storage pool host name")); goto cleanup; } - source->hosts[i].name =3D name; =20 port =3D virXMLPropString(nodeset[i], "port"); if (port) { @@ -518,7 +517,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, port); goto cleanup; } - VIR_FREE(port); } } } @@ -532,7 +530,7 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, goto cleanup; =20 for (i =3D 0; i < nsource; i++) { - char *partsep; + VIR_AUTOFREE(char *) partsep =3D NULL; virStoragePoolSourceDevice dev =3D { .path =3D NULL }; dev.path =3D virXMLPropString(nodeset[i], "path"); =20 @@ -550,10 +548,8 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, _("invalid part_separator setting '%s'"), partsep); virStoragePoolSourceDeviceClear(&dev); - VIR_FREE(partsep); goto cleanup; } - VIR_FREE(partsep); } =20 if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0)= { @@ -612,8 +608,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, cleanup: ctxt->node =3D relnode; =20 - VIR_FREE(port); - VIR_FREE(nodeset); return ret; } =20 @@ -660,7 +654,7 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt, virStoragePermsPtr perms, const char *permxpath) { - char *mode; + VIR_AUTOFREE(char *) mode =3D NULL; long long val; int ret =3D -1; xmlNodePtr relnode; @@ -683,13 +677,11 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt, int tmp; =20 if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) { - VIR_FREE(mode); virReportError(VIR_ERR_XML_ERROR, "%s", _("malformed octal mode")); goto error; } perms->mode =3D tmp; - VIR_FREE(mode); } else { perms->mode =3D (mode_t) -1; } @@ -740,9 +732,9 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) VIR_AUTOPTR(virStoragePoolDef) def =3D NULL; virStoragePoolDefPtr ret =3D NULL; xmlNodePtr source_node; - char *type =3D NULL; - char *uuid =3D NULL; - char *target_path =3D NULL; + VIR_AUTOFREE(char *) type =3D NULL; + VIR_AUTOFREE(char *) uuid =3D NULL; + VIR_AUTOFREE(char *) target_path =3D NULL; =20 if (VIR_ALLOC(def) < 0) return NULL; @@ -751,23 +743,23 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) if (type =3D=3D NULL) { virReportError(VIR_ERR_XML_ERROR, "%s", _("storage pool missing type attribute")); - goto cleanup; + return NULL; } =20 if ((def->type =3D virStoragePoolTypeFromString(type)) < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown storage pool type %s"), type); - goto cleanup; + return NULL; } =20 if ((options =3D virStoragePoolOptionsForPoolType(def->type)) =3D=3D N= ULL) - goto cleanup; + return NULL; =20 source_node =3D virXPathNode("./source", ctxt); if (source_node) { if (virStoragePoolDefParseSource(ctxt, &def->source, def->type, source_node) < 0) - goto cleanup; + return NULL; } else { if (options->formatFromString) def->source.format =3D options->defaultFormat; @@ -777,17 +769,18 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) if (def->name =3D=3D NULL && options->flags & VIR_STORAGE_POOL_SOURCE_NAME && VIR_STRDUP(def->name, def->source.name) < 0) - goto cleanup; + return NULL; + if (def->name =3D=3D NULL) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing pool source name element")); - goto cleanup; + return NULL; } =20 if (strchr(def->name, '/')) { virReportError(VIR_ERR_XML_ERROR, _("name %s cannot contain '/'"), def->name); - goto cleanup; + return NULL; } =20 uuid =3D virXPathString("string(./uuid)", ctxt); @@ -795,13 +788,13 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) if (virUUIDGenerate(def->uuid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("unable to generate uuid")); - goto cleanup; + return NULL; } } else { if (virUUIDParse(uuid, def->uuid) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("malformed uuid element")); - goto cleanup; + return NULL; } } =20 @@ -809,7 +802,7 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) if (!def->source.nhost) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing storage pool source host name")); - goto cleanup; + return NULL; } } =20 @@ -817,27 +810,27 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) if (!def->source.dir) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing storage pool source path")); - goto cleanup; + return NULL; } } if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME) { if (def->source.name =3D=3D NULL) { /* source name defaults to pool name */ if (VIR_STRDUP(def->source.name, def->name) < 0) - goto cleanup; + return NULL; } } =20 if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) && (virStorageAdapterValidate(&def->source.adapter)) < 0) - goto cleanup; + return NULL; =20 /* If DEVICE is the only source type, then its required */ if (options->flags =3D=3D VIR_STORAGE_POOL_SOURCE_DEVICE) { if (!def->source.ndevice) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing storage pool source device name")); - goto cleanup; + return NULL; } } =20 @@ -846,32 +839,32 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) { if (def->type =3D=3D VIR_STORAGE_POOL_LOGICAL) { if (virAsprintf(&target_path, "/dev/%s", def->source.name) < 0) - goto cleanup; + return NULL; } else if (def->type =3D=3D VIR_STORAGE_POOL_ZFS) { if (virAsprintf(&target_path, "/dev/zvol/%s", def->source.name= ) < 0) - goto cleanup; + return NULL; } else { target_path =3D virXPathString("string(./target/path)", ctxt); if (!target_path) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing storage pool target path")); - goto cleanup; + return NULL; } } def->target.path =3D virFileSanitizePath(target_path); if (!def->target.path) - goto cleanup; + return NULL; =20 if (virStorageDefParsePerms(ctxt, &def->target.perms, "./target/permissions") < 0) - goto cleanup; + return NULL; } =20 if (def->type =3D=3D VIR_STORAGE_POOL_ISCSI_DIRECT && !def->source.initiator.iqn) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("missing initiator IQN")); - goto cleanup; + return NULL; } =20 /* Make a copy of all the callback pointers here for easier use, @@ -879,14 +872,10 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) def->ns =3D options->ns; if (def->ns.parse && (def->ns.parse)(ctxt, &def->namespaceData) < 0) - goto cleanup; + return NULL; =20 VIR_STEAL_PTR(ret, def); =20 - cleanup: - VIR_FREE(uuid); - VIR_FREE(type); - VIR_FREE(target_path); return ret; } =20 @@ -1169,13 +1158,13 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, VIR_AUTOPTR(virStorageVolDef) def =3D NULL; virStorageVolDefPtr ret =3D NULL; virStorageVolOptionsPtr options; - char *type =3D NULL; - char *allocation =3D NULL; - char *capacity =3D NULL; - char *unit =3D NULL; - char *backingStore =3D NULL; + VIR_AUTOFREE(char *) type =3D NULL; + VIR_AUTOFREE(char *) allocation =3D NULL; + VIR_AUTOFREE(char *) capacity =3D NULL; + VIR_AUTOFREE(char *) unit =3D NULL; + VIR_AUTOFREE(char *) backingStore =3D NULL; + VIR_AUTOFREE(xmlNodePtr *) nodes =3D NULL; xmlNodePtr node; - xmlNodePtr *nodes =3D NULL; size_t i; int n; =20 @@ -1195,7 +1184,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if (def->name =3D=3D NULL) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing volume name element")); - goto cleanup; + return NULL; } =20 /* Normally generated by pool refresh, but useful for unit tests */ @@ -1207,13 +1196,13 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if ((def->type =3D virStorageVolTypeFromString(type)) < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown volume type '%s'"), type); - goto cleanup; + return NULL; } } =20 if ((backingStore =3D virXPathString("string(./backingStore/path)", ct= xt))) { if (VIR_ALLOC(def->target.backingStore) < 0) - goto cleanup; + return NULL; =20 def->target.backingStore->type =3D VIR_STORAGE_TYPE_FILE; =20 @@ -1221,7 +1210,9 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, backingStore =3D NULL; =20 if (options->formatFromString) { - char *format =3D virXPathString("string(./backingStore/format/= @type)", ctxt); + VIR_AUTOFREE(char *) format =3D NULL; + + format =3D virXPathString("string(./backingStore/format/@type)= ", ctxt); if (format =3D=3D NULL) def->target.backingStore->format =3D options->defaultForma= t; else @@ -1230,29 +1221,27 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if (def->target.backingStore->format < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown volume format type %s"), format); - VIR_FREE(format); - goto cleanup; + return NULL; } - VIR_FREE(format); } =20 if (VIR_ALLOC(def->target.backingStore->perms) < 0) - goto cleanup; + return NULL; if (virStorageDefParsePerms(ctxt, def->target.backingStore->perms, "./backingStore/permissions") < 0) - goto cleanup; + return NULL; } =20 capacity =3D virXPathString("string(./capacity)", ctxt); unit =3D virXPathString("string(./capacity/@unit)", ctxt); if (capacity) { if (virStorageSize(unit, capacity, &def->target.capacity) < 0) - goto cleanup; + return NULL; } else if (!(flags & VIR_VOL_XML_PARSE_NO_CAPACITY) && !((flags & VIR_VOL_XML_PARSE_OPT_CAPACITY) && virStorageSourceHasBacking(&def->target))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing capacity elemen= t")); - goto cleanup; + return NULL; } VIR_FREE(unit); =20 @@ -1260,7 +1249,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if (allocation) { unit =3D virXPathString("string(./allocation/@unit)", ctxt); if (virStorageSize(unit, allocation, &def->target.allocation) < 0) - goto cleanup; + return NULL; def->target.has_allocation =3D true; } else { def->target.allocation =3D def->target.capacity; @@ -1268,7 +1257,9 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, =20 def->target.path =3D virXPathString("string(./target/path)", ctxt); if (options->formatFromString) { - char *format =3D virXPathString("string(./target/format/@type)", c= txt); + VIR_AUTOFREE(char *) format =3D NULL; + + format =3D virXPathString("string(./target/format/@type)", ctxt); if (format =3D=3D NULL) def->target.format =3D options->defaultFormat; else @@ -1277,41 +1268,39 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if (def->target.format < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown volume format type %s"), format); - VIR_FREE(format); - goto cleanup; + return NULL; } - VIR_FREE(format); } =20 if (VIR_ALLOC(def->target.perms) < 0) - goto cleanup; + return NULL; if (virStorageDefParsePerms(ctxt, def->target.perms, "./target/permissions") < 0) - goto cleanup; + return NULL; =20 node =3D virXPathNode("./target/encryption", ctxt); if (node !=3D NULL) { def->target.encryption =3D virStorageEncryptionParseNode(node, ctx= t); if (def->target.encryption =3D=3D NULL) - goto cleanup; + return NULL; } =20 def->target.compat =3D virXPathString("string(./target/compat)", ctxt); if (virStorageFileCheckCompat(def->target.compat) < 0) - goto cleanup; + return NULL; =20 if (virXPathNode("./target/nocow", ctxt)) def->target.nocow =3D true; =20 if (virXPathNode("./target/features", ctxt)) { if ((n =3D virXPathNodeSet("./target/features/*", ctxt, &nodes)) <= 0) - goto cleanup; + return NULL; =20 if (!def->target.compat && VIR_STRDUP(def->target.compat, "1.1") <= 0) - goto cleanup; + return NULL; =20 if (!(def->target.features =3D virBitmapNew(VIR_STORAGE_FILE_FEATU= RE_LAST))) - goto cleanup; + return NULL; =20 for (i =3D 0; i < n; i++) { int f =3D virStorageFileFeatureTypeFromString((const char*)nod= es[i]->name); @@ -1319,7 +1308,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if (f < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unsupported = feature %s"), (const char*)nodes[i]->name); - goto cleanup; + return NULL; } ignore_value(virBitmapSetBit(def->target.features, f)); } @@ -1327,14 +1316,6 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, } =20 VIR_STEAL_PTR(ret, def); - - cleanup: - VIR_FREE(nodes); - VIR_FREE(allocation); - VIR_FREE(capacity); - VIR_FREE(unit); - VIR_FREE(type); - VIR_FREE(backingStore); return ret; } =20 @@ -1616,32 +1597,27 @@ virStoragePoolSaveState(const char *stateFile, virStoragePoolDefPtr def) { virBuffer buf =3D VIR_BUFFER_INITIALIZER; - int ret =3D -1; - char *xml; + VIR_AUTOFREE(char *) xml =3D NULL; =20 virBufferAddLit(&buf, "\n"); virBufferAdjustIndent(&buf, 2); =20 if (virStoragePoolDefFormatBuf(&buf, def) < 0) - goto error; + return -1; =20 virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "\n"); =20 if (virBufferCheckError(&buf) < 0) - goto error; + return -1; =20 if (!(xml =3D virBufferContentAndReset(&buf))) - goto error; + return -1; =20 if (virStoragePoolSaveXML(stateFile, def, xml)) - goto error; - - ret =3D 0; + return -1; =20 - error: - VIR_FREE(xml); - return ret; + return 0; } =20 =20 @@ -1649,8 +1625,7 @@ int virStoragePoolSaveConfig(const char *configFile, virStoragePoolDefPtr def) { - char *xml; - int ret =3D -1; + VIR_AUTOFREE(char *) xml =3D NULL; =20 if (!(xml =3D virStoragePoolDefFormat(def))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -1658,13 +1633,7 @@ virStoragePoolSaveConfig(const char *configFile, return -1; } =20 - if (virStoragePoolSaveXML(configFile, def, xml)) - goto cleanup; - - ret =3D 0; - cleanup: - VIR_FREE(xml); - return ret; + return virStoragePoolSaveXML(configFile, def, xml); } =20 =20 --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list