[libvirt] [PATCH v4 10/13] Adapt to VIR_STRDUP and VIR_STRNDUP in src/xen/*

Michal Privoznik mprivozn at redhat.com
Mon May 20 17:55:36 UTC 2013


---
 src/xen/block_stats.c    | 16 ++++++------
 src/xen/xen_driver.c     |  9 +++----
 src/xen/xen_hypervisor.c | 19 ++++----------
 src/xen/xen_inotify.c    | 15 +++--------
 src/xen/xend_internal.c  | 68 ++++++++++++------------------------------------
 src/xen/xm_internal.c    |  9 +++----
 src/xen/xs_internal.c    | 44 ++++++++++++-------------------
 7 files changed, 56 insertions(+), 124 deletions(-)

diff --git a/src/xen/block_stats.c b/src/xen/block_stats.c
index ded8d7f..5952971 100644
--- a/src/xen/block_stats.c
+++ b/src/xen/block_stats.c
@@ -292,14 +292,14 @@ xenLinuxDomainDeviceID(int domid, const char *path)
      * /sys/devices/xen-backend/(vbd|tap)-{domid}-{devid}/statistics/{...}
      */
 
-    if (strlen(path) >= 5 && STRPREFIX(path, "/dev/"))
-        mod_path = strdup(path);
-    else
-        ignore_value(virAsprintf(&mod_path, "/dev/%s", path));
-
-    if (!mod_path) {
-        virReportOOMError();
-        return -1;
+    if (strlen(path) >= 5 && STRPREFIX(path, "/dev/")) {
+        if (VIR_STRDUP(mod_path, path) < 0)
+            return -1;
+    } else {
+        if (virAsprintf(&mod_path, "/dev/%s", path) < 0) {
+            virReportOOMError();
+            return -1;
+        }
     }
 
     retval = -1;
diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
index cc54f7a..84a9fcc 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -408,10 +408,8 @@ xenUnifiedConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int f
     priv->opened[XEN_UNIFIED_INOTIFY_OFFSET] = 1;
 #endif
 
-    if (!(priv->saveDir = strdup(XEN_SAVE_DIR))) {
-        virReportOOMError();
+    if (VIR_STRDUP(priv->saveDir, XEN_SAVE_DIR) < 0)
         goto error;
-    }
 
     if (virFileMakePath(priv->saveDir) < 0) {
         VIR_ERROR(_("Errored to create save dir '%s': %s"), priv->saveDir,
@@ -2165,8 +2163,8 @@ xenUnifiedAddDomainInfo(xenUnifiedDomainInfoListPtr list,
 
     if (VIR_ALLOC(info) < 0)
         goto memory_error;
-    if (!(info->name = strdup(name)))
-        goto memory_error;
+    if (VIR_STRDUP(info->name, name) < 0)
+        goto error;
 
     memcpy(info->uuid, uuid, VIR_UUID_BUFLEN);
     info->id = id;
@@ -2182,6 +2180,7 @@ xenUnifiedAddDomainInfo(xenUnifiedDomainInfoListPtr list,
     return 0;
 memory_error:
     virReportOOMError();
+error:
     if (info)
         VIR_FREE(info->name);
     VIR_FREE(info);
diff --git a/src/xen/xen_hypervisor.c b/src/xen/xen_hypervisor.c
index 68a3ef4..590575e 100644
--- a/src/xen/xen_hypervisor.c
+++ b/src/xen/xen_hypervisor.c
@@ -1141,16 +1141,12 @@ xenHypervisorGetSchedulerType(virDomainPtr domain, int *nparams)
 
         switch (op.u.getschedulerid.sched_id){
             case XEN_SCHEDULER_SEDF:
-                schedulertype = strdup("sedf");
-                if (schedulertype == NULL)
-                    virReportOOMError();
+                ignore_value(VIR_STRDUP(schedulertype, "sedf"));
                 if (nparams)
                     *nparams = XEN_SCHED_SEDF_NPARAM;
                 break;
             case XEN_SCHEDULER_CREDIT:
-                schedulertype = strdup("credit");
-                if (schedulertype == NULL)
-                    virReportOOMError();
+                ignore_value(VIR_STRDUP(schedulertype, "credit"));
                 if (nparams)
                     *nparams = XEN_SCHED_CRED_NPARAM;
                 break;
@@ -2534,14 +2530,9 @@ xenHypervisorDomainGetOSType(virDomainPtr dom)
         return NULL;
     }
 
-    if (XEN_GETDOMAININFO_FLAGS(dominfo) & DOMFLAGS_HVM)
-        ostype = strdup("hvm");
-    else
-        ostype = strdup("linux");
-
-    if (ostype == NULL)
-        virReportOOMError();
-
+    ignore_value(VIR_STRDUP(ostype,
+                            XEN_GETDOMAININFO_FLAGS(dominfo) & DOMFLAGS_HVM ?
+                            "hvm" : "linux"));
     return ostype;
 }
 
diff --git a/src/xen/xen_inotify.c b/src/xen/xen_inotify.c
index e13c572..0b2c622 100644
--- a/src/xen/xen_inotify.c
+++ b/src/xen/xen_inotify.c
@@ -39,7 +39,7 @@
 #include "virlog.h"
 #include "viruuid.h"
 #include "virfile.h"
-
+#include "virstring.h"
 #include "xm_internal.h" /* for xenXMDomainConfigParse */
 
 #define VIR_FROM_THIS VIR_FROM_XEN_INOTIFY
@@ -58,12 +58,9 @@ xenInotifyXenCacheLookup(virConnectPtr conn,
         return -1;
     }
 
-    *name = strdup(entry->def->name);
     memcpy(uuid, entry->def->uuid, VIR_UUID_BUFLEN);
-
-    if (!*name) {
+    if (VIR_STRDUP(*name, entry->def->name) < 0) {
         VIR_DEBUG("Error getting dom from def");
-        virReportOOMError();
         return -1;
     }
     return 0;
@@ -103,11 +100,8 @@ xenInotifyXendDomainsDirLookup(virConnectPtr conn,
            list info */
         for (i = 0 ; i < priv->configInfoList->count ; i++) {
             if (!memcmp(rawuuid, priv->configInfoList->doms[i]->uuid, VIR_UUID_BUFLEN)) {
-                *name = strdup(priv->configInfoList->doms[i]->name);
-                if (!*name) {
-                    virReportOOMError();
+                if (VIR_STRDUP(*name, priv->configInfoList->doms[i]->name) < 0)
                     return -1;
-                }
                 memcpy(uuid, priv->configInfoList->doms[i]->uuid, VIR_UUID_BUFLEN);
                 VIR_DEBUG("Found dom on list");
                 return 0;
@@ -118,8 +112,7 @@ xenInotifyXendDomainsDirLookup(virConnectPtr conn,
         return -1;
     }
 
-    if (!(*name = strdup(dom->name))) {
-        virReportOOMError();
+    if (VIR_STRDUP(*name, dom->name) < 0) {
         virDomainFree(dom);
         return -1;
     }
diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c
index aec57f5..96758cc 100644
--- a/src/xen/xend_internal.c
+++ b/src/xen/xend_internal.c
@@ -761,8 +761,7 @@ xenDaemonListDomainsOld(virConnectPtr xend)
          _for_i = _for_i->u.s.cdr, node = _for_i->u.s.car) {
         if (node->kind != SEXPR_VALUE)
             continue;
-        ret[i] = strdup(node->u.value);
-        if (!ret[i])
+        if (VIR_STRDUP(ret[i], node->u.value) < 0)
             goto no_memory;
         i++;
     }
@@ -1384,14 +1383,8 @@ xenDaemonDomainGetOSType(virDomainPtr domain)
     if (root == NULL)
         return NULL;
 
-    if (sexpr_lookup(root, "domain/image/hvm")) {
-        type = strdup("hvm");
-    } else {
-        type = strdup("linux");
-    }
-
-    if (type == NULL)
-        virReportOOMError();
+    ignore_value(VIR_STRDUP(type,
+                            sexpr_lookup(root, "domain/image/hvm") ? "hvm" : "linux"));
 
     sexpr_free(root);
 
@@ -2100,12 +2093,7 @@ xenDaemonLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
         else
             id = -1;
 
-        if (domname) {
-            name = strdup(domname);
-
-            if (name == NULL)
-                virReportOOMError();
-        }
+        ignore_value(VIR_STRDUP(name, domname));
 
         sexpr_free(root);
     }
@@ -2260,12 +2248,9 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain,
                               priv->xendConfigVersion, 1) < 0)
             goto cleanup;
 
-        if (dev->data.disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
-            if (!(target = strdup(dev->data.disk->dst))) {
-                virReportOOMError();
-                goto cleanup;
-            }
-        }
+        if (dev->data.disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM &&
+            VIR_STRDUP(target, dev->data.disk->dst) < 0)
+            goto cleanup;
         break;
 
     case VIR_DOMAIN_DEVICE_NET:
@@ -2279,10 +2264,8 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain,
         char macStr[VIR_MAC_STRING_BUFLEN];
         virMacAddrFormat(&dev->data.net->mac, macStr);
 
-        if (!(target = strdup(macStr))) {
-            virReportOOMError();
+        if (VIR_STRDUP(target, macStr) < 0)
             goto cleanup;
-        }
         break;
 
     case VIR_DOMAIN_DEVICE_HOSTDEV:
@@ -2586,12 +2569,9 @@ xenDaemonDomainSetAutostart(virDomainPtr domain, int autostart)
 
         /* Change the autostart value in place, then define the new sexpr */
         VIR_FREE(autonode->u.s.car->u.value);
-        autonode->u.s.car->u.value = (autostart ? strdup("start")
-                                                : strdup("ignore"));
-        if (!(autonode->u.s.car->u.value)) {
-            virReportOOMError();
+        if (VIR_STRDUP(autonode->u.s.car->u.value,
+                       autostart ? "start" : "ignore") < 0)
             goto error;
-        }
 
         if (sexpr2string(root, &buffer) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -2750,9 +2730,7 @@ xenDaemonDomainMigratePerform(virDomainPtr domain,
             virURIFree(uriptr);
             return -1;
         }
-        hostname = strdup(uriptr->server);
-        if (!hostname) {
-            virReportOOMError();
+        if (VIR_STRDUP(hostname, uriptr->server) < 0) {
             virURIFree(uriptr);
             return -1;
         }
@@ -2772,19 +2750,13 @@ xenDaemonDomainMigratePerform(virDomainPtr domain,
 
         /* Get the hostname. */
         n = p - uri; /* n = Length of hostname in bytes. */
-        hostname = strdup(uri);
-        if (!hostname) {
-            virReportOOMError();
+        if (VIR_STRDUP(hostname, uri) < 0)
             return -1;
-        }
         hostname[n] = '\0';
     }
     else {                      /* "hostname" (or IP address) */
-        hostname = strdup(uri);
-        if (!hostname) {
-            virReportOOMError();
+        if (VIR_STRDUP(hostname, uri) < 0)
             return -1;
-        }
     }
 
     VIR_DEBUG("hostname = %s, port = %s", hostname, port);
@@ -2938,10 +2910,8 @@ xenDaemonListDefinedDomains(virConnectPtr conn,
         if (node->kind != SEXPR_VALUE)
             continue;
 
-        if ((names[ret++] = strdup(node->u.value)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(names[ret++], node->u.value) < 0)
             goto error;
-        }
 
         if (ret >= maxnames)
             break;
@@ -2997,19 +2967,13 @@ xenDaemonGetSchedulerType(virDomainPtr domain, int *nparams)
         goto error;
     }
     if (STREQ(ret, "credit")) {
-        schedulertype = strdup("credit");
-        if (schedulertype == NULL){
-            virReportOOMError();
+        if (VIR_STRDUP(schedulertype, "credit") < 0)
             goto error;
-        }
         if (nparams)
             *nparams = XEN_SCHED_CRED_NPARAM;
     } else if (STREQ(ret, "sedf")) {
-        schedulertype = strdup("sedf");
-        if (schedulertype == NULL){
-            virReportOOMError();
+        if (VIR_STRDUP(schedulertype, "sedf") < 0)
             goto error;
-        }
         if (nparams)
             *nparams = XEN_SCHED_SEDF_NPARAM;
     } else {
diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
index 66a6c4c..e64a9dd 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -241,8 +241,7 @@ xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename)
             virReportOOMError();
             return -1;
         }
-        if ((entry->filename = strdup(filename)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(entry->filename, filename) < 0) {
             VIR_FREE(entry);
             return -1;
         }
@@ -1047,10 +1046,8 @@ xenXMDomainDefineXML(virConnectPtr conn, const char *xml)
         goto error;
     }
 
-    if ((entry->filename = strdup(filename)) == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(entry->filename, filename) < 0)
         goto error;
-    }
     entry->def = def;
 
     if (virHashAddEntry(priv->configCache, filename, entry) < 0) {
@@ -1140,7 +1137,7 @@ xenXMListIterator(void *payload ATTRIBUTE_UNUSED, const void *name, void *data)
 
     dom = xenDaemonLookupByName(ctx->conn, name);
     if (!dom) {
-        if (!(ctx->names[ctx->count] = strdup(name)))
+        if (VIR_STRDUP(ctx->names[ctx->count], name) < 0)
             ctx->oom = 1;
         else
             ctx->count++;
diff --git a/src/xen/xs_internal.c b/src/xen/xs_internal.c
index 496c30b..eab18f1 100644
--- a/src/xen/xs_internal.c
+++ b/src/xen/xs_internal.c
@@ -50,6 +50,7 @@
 #include "xen_driver.h"
 #include "xs_internal.h"
 #include "xen_hypervisor.h"
+#include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_XEN
 
@@ -428,11 +429,7 @@ xenStoreDomainGetNetworkID(virConnectPtr conn, int id, const char *mac)
         VIR_FREE(val);
 
         if (match) {
-            ret = strdup(list[i]);
-
-            if (ret == NULL)
-                virReportOOMError();
-
+            ignore_value(VIR_STRDUP(ret, list[i]));
             break;
         }
     }
@@ -481,10 +478,7 @@ xenStoreDomainGetDiskID(virConnectPtr conn, int id, const char *dev)
             if ((devlen != len) || memcmp(val, dev, len)) {
                 VIR_FREE(val);
             } else {
-                ret = strdup(list[i]);
-
-                if (ret == NULL)
-                    virReportOOMError();
+                ignore_value(VIR_STRDUP(ret, list[i]));
 
                 VIR_FREE(val);
                 VIR_FREE(list);
@@ -504,10 +498,7 @@ xenStoreDomainGetDiskID(virConnectPtr conn, int id, const char *dev)
             if ((devlen != len) || memcmp(val, dev, len)) {
                 VIR_FREE(val);
             } else {
-                ret = strdup(list[i]);
-
-                if (ret == NULL)
-                    virReportOOMError();
+                ignore_value(VIR_STRDUP(ret, list[i]));
 
                 VIR_FREE(val);
                 VIR_FREE(list);
@@ -559,7 +550,7 @@ xenStoreDomainGetPCIID(virConnectPtr conn, int id, const char *bdf)
         VIR_FREE(val);
 
         if (match) {
-            ret = strdup(list[i]);
+            ignore_value(VIR_STRDUP(ret, list[i]));
             break;
         }
     }
@@ -665,22 +656,22 @@ xenStoreAddWatch(virConnectPtr conn,
         }
     }
 
-    if (VIR_ALLOC(watch) < 0)
-        goto no_memory;
+    if (VIR_ALLOC(watch) < 0) {
+        virReportOOMError();
+        goto error;
+    }
 
-    watch->path   = strdup(path);
-    watch->token  = strdup(token);
-    watch->cb     = cb;
+    watch->cb = cb;
     watch->opaque = opaque;
-
-    if (watch->path == NULL || watch->token == NULL) {
-        goto no_memory;
-    }
+    if (VIR_STRDUP(watch->path, path) < 0 ||
+        VIR_STRDUP(watch->token, token) < 0)
+        goto error;
 
     /* Make space on list */
     n = list->count;
     if (VIR_REALLOC_N(list->watches, n + 1) < 0) {
-        goto no_memory;
+        virReportOOMError();
+        goto error;
     }
 
     list->watches[n] = watch;
@@ -688,15 +679,12 @@ xenStoreAddWatch(virConnectPtr conn,
 
     return xs_watch(priv->xshandle, watch->path, watch->token);
 
-  no_memory:
+  error:
     if (watch) {
         VIR_FREE(watch->path);
         VIR_FREE(watch->token);
         VIR_FREE(watch);
     }
-
-    virReportOOMError();
-
     return -1;
 }
 
-- 
1.8.2.1




More information about the libvir-list mailing list