[PATCH 10/15] conf: Use VIR_AUTOFREE for storage_conf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Let's make use of the auto __cleanup capabilities cleaning up any
now unnecessary goto paths.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 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 = -1;
-    xmlNodePtr relnode, authnode, *nodeset = NULL;
+    xmlNodePtr relnode, authnode;
     xmlNodePtr adapternode;
     int nsource;
     size_t i;
     virStoragePoolOptionsPtr options;
     VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
-    char *name = NULL;
-    char *port = NULL;
-    char *ver = NULL;
+    VIR_AUTOFREE(char *) port = NULL;
+    VIR_AUTOFREE(char *) ver = NULL;
+    VIR_AUTOFREE(xmlNodePtr *) nodeset = NULL;
     int n;
 
     relnode = ctxt->node;
@@ -478,7 +478,9 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
     }
 
     if (options->formatFromString) {
-        char *format = virXPathString("string(./format/@type)", ctxt);
+        VIR_AUTOFREE(char *) format = NULL;
+
+        format = virXPathString("string(./format/@type)", ctxt);
         if (format == NULL)
             source->format = 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);
     }
 
     if ((n = virXPathNodeSet("./host", ctxt, &nodeset)) < 0)
@@ -502,13 +502,12 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
         source->nhost = n;
 
         for (i = 0; i < source->nhost; i++) {
-            name = virXMLPropString(nodeset[i], "name");
-            if (name == NULL) {
+            source->hosts[i].name = 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 = name;
 
             port = 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;
 
     for (i = 0; i < nsource; i++) {
-        char *partsep;
+        VIR_AUTOFREE(char *) partsep = NULL;
         virStoragePoolSourceDevice dev = { .path = NULL };
         dev.path = virXMLPropString(nodeset[i], "path");
 
@@ -550,10 +548,8 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
                                _("invalid part_separator setting '%s'"),
                                partsep);
                 virStoragePoolSourceDeviceClear(&dev);
-                VIR_FREE(partsep);
                 goto cleanup;
             }
-            VIR_FREE(partsep);
         }
 
         if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0) {
@@ -612,8 +608,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
  cleanup:
     ctxt->node = relnode;
 
-    VIR_FREE(port);
-    VIR_FREE(nodeset);
     return ret;
 }
 
@@ -660,7 +654,7 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
                         virStoragePermsPtr perms,
                         const char *permxpath)
 {
-    char *mode;
+    VIR_AUTOFREE(char *) mode = NULL;
     long long val;
     int ret = -1;
     xmlNodePtr relnode;
@@ -683,13 +677,11 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
         int tmp;
 
         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 = tmp;
-        VIR_FREE(mode);
     } else {
         perms->mode = (mode_t) -1;
     }
@@ -740,9 +732,9 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
     VIR_AUTOPTR(virStoragePoolDef) def = NULL;
     virStoragePoolDefPtr ret = NULL;
     xmlNodePtr source_node;
-    char *type = NULL;
-    char *uuid = NULL;
-    char *target_path = NULL;
+    VIR_AUTOFREE(char *) type = NULL;
+    VIR_AUTOFREE(char *) uuid = NULL;
+    VIR_AUTOFREE(char *) target_path = NULL;
 
     if (VIR_ALLOC(def) < 0)
         return NULL;
@@ -751,23 +743,23 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
     if (type == NULL) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("storage pool missing type attribute"));
-        goto cleanup;
+        return NULL;
     }
 
     if ((def->type = virStoragePoolTypeFromString(type)) < 0) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("unknown storage pool type %s"), type);
-        goto cleanup;
+        return NULL;
     }
 
     if ((options = virStoragePoolOptionsForPoolType(def->type)) == NULL)
-        goto cleanup;
+        return NULL;
 
     source_node = 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 = options->defaultFormat;
@@ -777,17 +769,18 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
     if (def->name == NULL &&
         options->flags & VIR_STORAGE_POOL_SOURCE_NAME &&
         VIR_STRDUP(def->name, def->source.name) < 0)
-        goto cleanup;
+        return NULL;
+
     if (def->name == NULL) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("missing pool source name element"));
-        goto cleanup;
+        return NULL;
     }
 
     if (strchr(def->name, '/')) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("name %s cannot contain '/'"), def->name);
-        goto cleanup;
+        return NULL;
     }
 
     uuid = 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;
         }
     }
 
@@ -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;
         }
     }
 
@@ -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 == NULL) {
             /* source name defaults to pool name */
             if (VIR_STRDUP(def->source.name, def->name) < 0)
-                goto cleanup;
+                return NULL;
         }
     }
 
     if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
         (virStorageAdapterValidate(&def->source.adapter)) < 0)
-            goto cleanup;
+            return NULL;
 
     /* If DEVICE is the only source type, then its required */
     if (options->flags == 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;
         }
     }
 
@@ -846,32 +839,32 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
     if (!(options->flags & VIR_STORAGE_POOL_SOURCE_NETWORK)) {
         if (def->type == VIR_STORAGE_POOL_LOGICAL) {
             if (virAsprintf(&target_path, "/dev/%s", def->source.name) < 0)
-                goto cleanup;
+                return NULL;
         } else if (def->type == VIR_STORAGE_POOL_ZFS) {
             if (virAsprintf(&target_path, "/dev/zvol/%s", def->source.name) < 0)
-                goto cleanup;
+                return NULL;
         } else {
             target_path = 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 = virFileSanitizePath(target_path);
         if (!def->target.path)
-            goto cleanup;
+            return NULL;
 
         if (virStorageDefParsePerms(ctxt, &def->target.perms,
                                     "./target/permissions") < 0)
-            goto cleanup;
+            return NULL;
     }
 
     if (def->type == VIR_STORAGE_POOL_ISCSI_DIRECT &&
         !def->source.initiator.iqn) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("missing initiator IQN"));
-        goto cleanup;
+        return NULL;
     }
 
     /* Make a copy of all the callback pointers here for easier use,
@@ -879,14 +872,10 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
     def->ns = options->ns;
     if (def->ns.parse &&
         (def->ns.parse)(ctxt, &def->namespaceData) < 0)
-        goto cleanup;
+        return NULL;
 
     VIR_STEAL_PTR(ret, def);
 
- cleanup:
-    VIR_FREE(uuid);
-    VIR_FREE(type);
-    VIR_FREE(target_path);
     return ret;
 }
 
@@ -1169,13 +1158,13 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
     VIR_AUTOPTR(virStorageVolDef) def = NULL;
     virStorageVolDefPtr ret = NULL;
     virStorageVolOptionsPtr options;
-    char *type = NULL;
-    char *allocation = NULL;
-    char *capacity = NULL;
-    char *unit = NULL;
-    char *backingStore = NULL;
+    VIR_AUTOFREE(char *) type = NULL;
+    VIR_AUTOFREE(char *) allocation = NULL;
+    VIR_AUTOFREE(char *) capacity = NULL;
+    VIR_AUTOFREE(char *) unit = NULL;
+    VIR_AUTOFREE(char *) backingStore = NULL;
+    VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
     xmlNodePtr node;
-    xmlNodePtr *nodes = NULL;
     size_t i;
     int n;
 
@@ -1195,7 +1184,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
     if (def->name == NULL) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("missing volume name element"));
-        goto cleanup;
+        return NULL;
     }
 
     /* Normally generated by pool refresh, but useful for unit tests */
@@ -1207,13 +1196,13 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
         if ((def->type = virStorageVolTypeFromString(type)) < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("unknown volume type '%s'"), type);
-            goto cleanup;
+            return NULL;
         }
     }
 
     if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) {
         if (VIR_ALLOC(def->target.backingStore) < 0)
-            goto cleanup;
+            return NULL;
 
         def->target.backingStore->type = VIR_STORAGE_TYPE_FILE;
 
@@ -1221,7 +1210,9 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
         backingStore = NULL;
 
         if (options->formatFromString) {
-            char *format = virXPathString("string(./backingStore/format/@type)", ctxt);
+            VIR_AUTOFREE(char *) format = NULL;
+
+            format = virXPathString("string(./backingStore/format/@type)", ctxt);
             if (format == NULL)
                 def->target.backingStore->format = options->defaultFormat;
             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);
         }
 
         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;
     }
 
     capacity = virXPathString("string(./capacity)", ctxt);
     unit = 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 element"));
-        goto cleanup;
+        return NULL;
     }
     VIR_FREE(unit);
 
@@ -1260,7 +1249,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
     if (allocation) {
         unit = virXPathString("string(./allocation/@unit)", ctxt);
         if (virStorageSize(unit, allocation, &def->target.allocation) < 0)
-            goto cleanup;
+            return NULL;
         def->target.has_allocation = true;
     } else {
         def->target.allocation = def->target.capacity;
@@ -1268,7 +1257,9 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
 
     def->target.path = virXPathString("string(./target/path)", ctxt);
     if (options->formatFromString) {
-        char *format = virXPathString("string(./target/format/@type)", ctxt);
+        VIR_AUTOFREE(char *) format = NULL;
+
+        format = virXPathString("string(./target/format/@type)", ctxt);
         if (format == NULL)
             def->target.format = 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);
     }
 
     if (VIR_ALLOC(def->target.perms) < 0)
-        goto cleanup;
+        return NULL;
     if (virStorageDefParsePerms(ctxt, def->target.perms,
                                 "./target/permissions") < 0)
-        goto cleanup;
+        return NULL;
 
     node = virXPathNode("./target/encryption", ctxt);
     if (node != NULL) {
         def->target.encryption = virStorageEncryptionParseNode(node, ctxt);
         if (def->target.encryption == NULL)
-            goto cleanup;
+            return NULL;
     }
 
     def->target.compat = virXPathString("string(./target/compat)", ctxt);
     if (virStorageFileCheckCompat(def->target.compat) < 0)
-        goto cleanup;
+        return NULL;
 
     if (virXPathNode("./target/nocow", ctxt))
         def->target.nocow = true;
 
     if (virXPathNode("./target/features", ctxt)) {
         if ((n = virXPathNodeSet("./target/features/*", ctxt, &nodes)) < 0)
-            goto cleanup;
+            return NULL;
 
         if (!def->target.compat && VIR_STRDUP(def->target.compat, "1.1") < 0)
-            goto cleanup;
+            return NULL;
 
         if (!(def->target.features = virBitmapNew(VIR_STORAGE_FILE_FEATURE_LAST)))
-            goto cleanup;
+            return NULL;
 
         for (i = 0; i < n; i++) {
             int f = virStorageFileFeatureTypeFromString((const char*)nodes[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,
     }
 
     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;
 }
 
@@ -1616,32 +1597,27 @@ virStoragePoolSaveState(const char *stateFile,
                         virStoragePoolDefPtr def)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    int ret = -1;
-    char *xml;
+    VIR_AUTOFREE(char *) xml = NULL;
 
     virBufferAddLit(&buf, "<poolstate>\n");
     virBufferAdjustIndent(&buf, 2);
 
     if (virStoragePoolDefFormatBuf(&buf, def) < 0)
-        goto error;
+        return -1;
 
     virBufferAdjustIndent(&buf, -2);
     virBufferAddLit(&buf, "</poolstate>\n");
 
     if (virBufferCheckError(&buf) < 0)
-        goto error;
+        return -1;
 
     if (!(xml = virBufferContentAndReset(&buf)))
-        goto error;
+        return -1;
 
     if (virStoragePoolSaveXML(stateFile, def, xml))
-        goto error;
-
-    ret = 0;
+        return -1;
 
- error:
-    VIR_FREE(xml);
-    return ret;
+    return 0;
 }
 
 
@@ -1649,8 +1625,7 @@ int
 virStoragePoolSaveConfig(const char *configFile,
                          virStoragePoolDefPtr def)
 {
-    char *xml;
-    int ret = -1;
+    VIR_AUTOFREE(char *) xml = NULL;
 
     if (!(xml = virStoragePoolDefFormat(def))) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -1658,13 +1633,7 @@ virStoragePoolSaveConfig(const char *configFile,
         return -1;
     }
 
-    if (virStoragePoolSaveXML(configFile, def, xml))
-        goto cleanup;
-
-    ret = 0;
- cleanup:
-    VIR_FREE(xml);
-    return ret;
+    return virStoragePoolSaveXML(configFile, def, xml);
 }
 
 
-- 
2.20.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux