[PATCH v3 29/34] Adapt to VIR_STRDUP and VIR_STRNDUP in src/xen/*

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

 



---
 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  | 78 ++++++++++++------------------------------------
 src/xen/xm_internal.c    |  9 ++----
 src/xen/xs_internal.c    | 44 ++++++++++-----------------
 7 files changed, 59 insertions(+), 131 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 6ad050c..f992ccd 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -443,10 +443,8 @@ xenUnifiedConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int f
     }
 #endif
 
-    if (!(priv->saveDir = strdup(XEN_SAVE_DIR))) {
-        virReportOOMError();
+    if (VIR_STRDUP(priv->saveDir, XEN_SAVE_DIR) < 0)
         goto fail;
-    }
 
     if (virFileMakePath(priv->saveDir) < 0) {
         VIR_ERROR(_("Failed to create save dir '%s': %s"), priv->saveDir,
@@ -2504,8 +2502,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;
@@ -2521,6 +2519,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 e15f7f2..03ebc0e 100644
--- a/src/xen/xen_hypervisor.c
+++ b/src/xen/xen_hypervisor.c
@@ -1199,16 +1199,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;
@@ -2894,14 +2890,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 d83708c..fe9ce65 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
@@ -62,12 +62,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;
@@ -107,11 +104,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;
@@ -122,8 +116,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 97f44ab..78edba4 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++;
     }
@@ -887,13 +886,8 @@ xenDaemonDomainLookupByID(virConnectPtr xend,
                      "%s", _("domain information incomplete, missing name"));
       goto error;
     }
-    if (domname) {
-      *domname = strdup(name);
-      if (*domname == NULL) {
-          virReportOOMError();
-          goto error;
-      }
-    }
+    if (domname && VIR_STRDUP(*domname, name) < 0)
+        goto error;
 
     if (sexpr_uuid(uuid, root, "domain/uuid") < 0) {
       virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -1454,14 +1448,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);
 
@@ -2363,12 +2351,8 @@ xenDaemonLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
         else
             id = -1;
 
-        if (domname) {
-            name = strdup(domname);
-
-            if (name == NULL)
-                virReportOOMError();
-        }
+        if (domname)
+            ignore_value(VIR_STRDUP(name, domname));
 
         sexpr_free(root);
     }
@@ -2532,12 +2516,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:
@@ -2551,10 +2532,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:
@@ -2886,12 +2865,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,
@@ -3050,9 +3026,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;
         }
@@ -3072,19 +3046,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);
@@ -3263,10 +3231,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;
@@ -3322,19 +3288,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 921c66a..b261e34 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -260,8 +260,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;
         }
@@ -1092,10 +1091,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) {
@@ -1190,7 +1187,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 5f0df63..776ddc6 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
 
@@ -851,11 +852,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;
         }
     }
@@ -904,10 +901,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);
@@ -927,10 +921,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);
@@ -982,7 +973,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;
         }
     }
@@ -1088,22 +1079,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;
@@ -1111,15 +1102,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.1.5

--
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]