From 4960022a1714c0ca934e889e8380d54ee9384cce Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 3 May 2013 14:52:48 +0200 Subject: [PATCH] Adapt to VIR_STRDUP and VIR_STRNDUP in src/* --- src/datatypes.c | 78 +++++++++++++++++++++++-------------------------- src/libvirt.c | 14 +++------ src/nodeinfo.c | 5 +--- 3 files changed, 41 insertions(+), 56 deletions(-) diff --git a/src/datatypes.c b/src/datatypes.c index d3cf5f2d38..f815b6c861 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -27,6 +27,7 @@ #include "virlog.h" #include "viralloc.h" #include "viruuid.h" +#include "virstring.h" #define VIR_FROM_THIS VIR_FROM_NONE @@ -225,8 +226,8 @@ virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid) if (!(ret = virObjectNew(virDomainClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0) + goto error; ret->conn = virObjectRef(conn); ret->id = -1; @@ -234,8 +235,7 @@ virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid) return ret; -no_memory: - virReportOOMError(); +error: virObjectUnref(ret); return NULL; } @@ -297,16 +297,15 @@ virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid) if (!(ret = virObjectNew(virNetworkClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0) + goto error; ret->conn = virObjectRef(conn); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); return ret; -no_memory: - virReportOOMError(); +error: virObjectUnref(ret); return NULL; } @@ -372,17 +371,15 @@ virGetInterface(virConnectPtr conn, const char *name, const char *mac) if (!(ret = virObjectNew(virInterfaceClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; - if (!(ret->mac = strdup(mac))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0 || + VIR_STRDUP(ret->mac, mac) < 0) + goto error; ret->conn = virObjectRef(conn); return ret; -no_memory: - virReportOOMError(); +error: virObjectUnref(ret); return NULL; } @@ -446,8 +443,8 @@ virGetStoragePool(virConnectPtr conn, const char *name, if (!(ret = virObjectNew(virStoragePoolClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0) + goto error; ret->conn = virObjectRef(conn); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); @@ -458,8 +455,7 @@ virGetStoragePool(virConnectPtr conn, const char *name, return ret; -no_memory: - virReportOOMError(); +error: virObjectUnref(ret); return NULL; } @@ -530,12 +526,10 @@ virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, if (!(ret = virObjectNew(virStorageVolClass))) return NULL; - if (!(ret->pool = strdup(pool))) - goto no_memory; - if (!(ret->name = strdup(name))) - goto no_memory; - if (!(ret->key = strdup(key))) - goto no_memory; + if (VIR_STRDUP(ret->pool, pool) < 0 || + VIR_STRDUP(ret->name, name) < 0 || + VIR_STRDUP(ret->key, key) < 0) + goto error; ret->conn = virObjectRef(conn); @@ -545,8 +539,7 @@ virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, return ret; -no_memory: - virReportOOMError(); +error: virObjectUnref(ret); return NULL; } @@ -610,13 +603,13 @@ virGetNodeDevice(virConnectPtr conn, const char *name) if (!(ret = virObjectNew(virNodeDeviceClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0) + goto error; ret->conn = virObjectRef(conn); return ret; -no_memory: - virReportOOMError(); + +error: virObjectUnref(ret); return NULL; } @@ -680,14 +673,14 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid, memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->usageType = usageType; - if (!(ret->usageID = strdup(usageID))) - goto no_memory; + if (VIR_STRDUP(ret->usageID, usageID) < 0) + goto error; ret->conn = virObjectRef(conn); return ret; -no_memory: - virReportOOMError(); + +error: virObjectUnref(ret); return NULL; } @@ -775,16 +768,16 @@ virGetNWFilter(virConnectPtr conn, const char *name, if (!(ret = virObjectNew(virNWFilterClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0) + goto error; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->conn = virObjectRef(conn); return ret; -no_memory: - virReportOOMError(); + +error: virObjectUnref(ret); return NULL; } @@ -832,13 +825,14 @@ virGetDomainSnapshot(virDomainPtr domain, const char *name) if (!(ret = virObjectNew(virDomainSnapshotClass))) return NULL; - if (!(ret->name = strdup(name))) - goto no_memory; + if (VIR_STRDUP(ret->name, name) < 0) + goto cleanup; + ret->domain = virObjectRef(domain); return ret; -no_memory: - virReportOOMError(); + +cleanup: virObjectUnref(ret); return NULL; } diff --git a/src/libvirt.c b/src/libvirt.c index 4087b5ae91..ab75110821 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -213,11 +213,9 @@ static int virConnectAuthCallbackDefault(virConnectCredentialPtr cred, } if (cred[i].type != VIR_CRED_EXTERNAL) { - if (STREQ(bufptr, "") && cred[i].defresult) - cred[i].result = strdup(cred[i].defresult); - else - cred[i].result = strdup(bufptr); - if (!cred[i].result) + if (VIR_STRDUP(cred[i].result, + STREQ(bufptr, "") && cred[i].defresult ? + cred[i].defresult : bufptr) < 0) return -1; cred[i].resultlen = strlen(cred[i].result); } @@ -1046,11 +1044,7 @@ virConnectOpenFindURIAliasMatch(virConfValuePtr value, const char *alias, char * STREQLEN(entry->str, alias, alias_len)) { VIR_DEBUG("Resolved alias '%s' to '%s'", alias, offset+1); - if (!(*uri = strdup(offset+1))) { - virReportOOMError(); - return -1; - } - return 0; + return VIR_STRDUP(*uri, offset+1); } entry = entry->next; diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 765f0fe4ce..7118a0fa33 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -982,11 +982,8 @@ int nodeGetMemoryStats(int cellNum ATTRIBUTE_UNUSED, FILE *meminfo; if (cellNum == VIR_NODE_MEMORY_STATS_ALL_CELLS) { - meminfo_path = strdup(MEMINFO_PATH); - if (!meminfo_path) { - virReportOOMError(); + if (VIR_STRDUP(meminfo_path, MEMINFO_PATH) < 0) return -1; - } } else { # if WITH_NUMACTL if (numa_available() < 0) { -- GitLab