---
src/util/virauth.c | 17 +--
src/util/virauthconfig.c | 8 +-
src/util/virbitmap.c | 9 +-
src/util/vircgroup.c | 36 ++---
src/util/vircommand.c | 33 ++---
src/util/virconf.c | 34 ++---
src/util/virdnsmasq.c | 22 +--
src/util/virebtables.c | 34 ++---
src/util/virebtables.h | 2 +-
src/util/virerror.c | 19 +--
src/util/virhash.c | 5 +-
src/util/viridentity.c | 15 +-
src/util/virinitctl.c | 4 +-
src/util/viriptables.c | 4 +-
src/util/virjson.c | 18 ++-
src/util/virkeyfile.c | 13 +-
src/util/virlockspace.c | 25 ++--
src/util/virlog.c | 25 ++--
src/util/virnetdevmacvlan.c | 16 +--
src/util/virnetdevtap.c | 11 +-
src/util/virnetdevvportprofile.c | 4 +-
src/util/virobject.c | 16 ++-
src/util/virpci.c | 14 +-
src/util/virsexpr.c | 37 ++---
src/util/virsocketaddr.c | 9 +-
src/util/virstoragefile.c | 18 +--
src/util/virstring.c | 17 +--
src/util/virsysinfo.c | 290 ++++++++++++++++-----------------------
src/util/virtypedparam.c | 14 +-
src/util/viruri.c | 58 ++++----
src/util/virutil.c | 91 +++++-------
src/util/virxml.c | 5 +-
32 files changed, 356 insertions(+), 567 deletions(-)
diff --git a/src/util/virauth.c b/src/util/virauth.c
index fdcefe1..0888c37 100644
--- a/src/util/virauth.c
+++ b/src/util/virauth.c
@@ -51,8 +51,8 @@ int virAuthGetConfigFilePath(virConnectPtr conn,
if (authenv) {
VIR_DEBUG("Using path from env '%s'", authenv);
- if (!(*path = strdup(authenv)))
- goto no_memory;
+ if (VIR_STRDUP(*path, authenv) < 0)
+ goto cleanup;
return 0;
}
@@ -62,8 +62,8 @@ int virAuthGetConfigFilePath(virConnectPtr conn,
conn->uri->params[i].value) {
VIR_DEBUG("Using path from URI '%s'",
conn->uri->params[i].value);
- if (!(*path = strdup(conn->uri->params[i].value)))
- goto no_memory;
+ if (VIR_STRDUP(*path, conn->uri->params[i].value) < 0)
+ goto cleanup;
return 0;
}
}
@@ -81,8 +81,8 @@ int virAuthGetConfigFilePath(virConnectPtr conn,
VIR_FREE(*path);
- if (!(*path = strdup(SYSCONFDIR "/libvirt/auth.conf")))
- goto no_memory;
+ if (VIR_STRDUP(*path, SYSCONFDIR "/libvirt/auth.conf") < 0)
+ goto cleanup;
VIR_DEBUG("Checking for readability of '%s'", *path);
if (access(*path, R_OK) == 0)
@@ -136,11 +136,8 @@ virAuthGetCredential(virConnectPtr conn,
&tmp) < 0)
goto cleanup;
- if (tmp &&
- !(*value = strdup(tmp))) {
- virReportOOMError();
+ if (tmp && VIR_STRDUP(*value, tmp) < 0)
goto cleanup;
- }
ret = 0;
diff --git a/src/util/virauthconfig.c b/src/util/virauthconfig.c
index 4259cb3..c9f6046 100644
--- a/src/util/virauthconfig.c
+++ b/src/util/virauthconfig.c
@@ -47,10 +47,8 @@ virAuthConfigPtr virAuthConfigNew(const char *path)
goto error;
}
- if (!(auth->path = strdup(path))) {
- virReportOOMError();
+ if (VIR_STRDUP(auth->path, path) < 0)
goto error;
- }
if (!(auth->keyfile = virKeyFileNew()))
goto error;
@@ -77,10 +75,8 @@ virAuthConfigPtr virAuthConfigNewData(const char *path,
goto error;
}
- if (!(auth->path = strdup(path))) {
- virReportOOMError();
+ if (VIR_STRDUP(auth->path, path) < 0)
goto error;
- }
if (!(auth->keyfile = virKeyFileNew()))
goto error;
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index c81555a..cf6a099 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -37,6 +37,8 @@
#include "count-one-bits.h"
#include "virstring.h"
+#define VIR_FROM_THIS VIR_FROM_NONE
+
struct _virBitmap {
size_t max_bit;
size_t map_len;
@@ -226,8 +228,11 @@ char *virBitmapFormat(virBitmapPtr bitmap)
return NULL;
cur = virBitmapNextSetBit(bitmap, -1);
- if (cur < 0)
- return strdup("");
+ if (cur < 0) {
+ char *ret;
+ ignore_value(VIR_STRDUP(ret, ""));
+ return ret;
+ }
start = prev = cur;
while (prev >= 0) {
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 473d2fc..b3845ea 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -116,19 +116,14 @@ static int virCgroupCopyMounts(virCgroupPtr group,
if (!parent->controllers[i].mountPoint)
continue;
- group->controllers[i].mountPoint =
- strdup(parent->controllers[i].mountPoint);
-
- if (!group->controllers[i].mountPoint)
+ if (VIR_STRDUP(group->controllers[i].mountPoint,
+ parent->controllers[i].mountPoint) < 0)
return -ENOMEM;
- if (parent->controllers[i].linkPoint) {
- group->controllers[i].linkPoint =
- strdup(parent->controllers[i].linkPoint);
-
- if (!group->controllers[i].linkPoint)
- return -ENOMEM;
- }
+ if (parent->controllers[i].linkPoint &&
+ VIR_STRDUP(group->controllers[i].linkPoint,
+ parent->controllers[i].linkPoint) < 0)
+ return -ENOMEM;
}
return 0;
}
@@ -177,8 +172,8 @@ static int virCgroupDetectMounts(virCgroupPtr group)
struct stat sb;
char *tmp2;
- if (!(group->controllers[i].mountPoint = strdup(entry.mnt_dir)))
- goto no_memory;
+ if (VIR_STRDUP(group->controllers[i].mountPoint, entry.mnt_dir)
< 0)
+ goto error;
tmp2 = strrchr(entry.mnt_dir, '/');
if (!tmp2) {
@@ -239,7 +234,7 @@ static int virCgroupCopyPlacement(virCgroupPtr group,
continue;
if (path[0] == '/') {
- if (!(group->controllers[i].placement = strdup(path)))
+ if (VIR_STRDUP(group->controllers[i].placement, path) < 0)
return -ENOMEM;
} else {
/*
@@ -821,7 +816,7 @@ static int virCgroupNew(const char *path,
}
if (path[0] == '/' || !parent) {
- if (!((*group)->path = strdup(path))) {
+ if (VIR_STRDUP((*group)->path, path) < 0) {
rc = -ENOMEM;
goto err;
}
@@ -1027,7 +1022,7 @@ static int virCgroupAddTaskStrController(virCgroupPtr group,
int rc = 0;
char *endp;
- if (!(str = strdup(pidstr)))
+ if (VIR_STRDUP(str, pidstr) < 0)
return -1;
cur = str;
@@ -1253,7 +1248,7 @@ int virCgroupNewPartition(const char *path,
if (STRNEQ(newpath, "/")) {
char *tmp;
- if (!(parentPath = strdup(newpath))) {
+ if (VIR_STRDUP(parentPath, newpath) < 0) {
rc = -ENOMEM;
goto cleanup;
}
@@ -2543,12 +2538,11 @@ static char *virCgroupIdentifyRoot(virCgroupPtr group)
}
tmp[0] = '\0';
- ret = strdup(group->controllers[i].mountPoint);
- tmp[0] = '/';
- if (!ret) {
- virReportOOMError();
+ if (VIR_STRDUP(ret, group->controllers[i].mountPoint) < 0) {
+ tmp[0] = '/';
return NULL;
}
+ tmp[0] = '/';
return ret;
}
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index f6f27d9..eecda58 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -946,9 +946,8 @@ virCommandSetPidFile(virCommandPtr cmd, const char *pidfile)
return;
VIR_FREE(cmd->pidfile);
- if (!(cmd->pidfile = strdup(pidfile))) {
+ if (VIR_STRDUP(cmd->pidfile, pidfile) < 0)
cmd->has_error = ENOMEM;
- }
}
@@ -1049,7 +1048,7 @@ virCommandSetSELinuxLabel(virCommandPtr cmd,
#if defined(WITH_SECDRIVER_SELINUX)
VIR_FREE(cmd->seLinuxLabel);
- if (label && !(cmd->seLinuxLabel = strdup(label)))
+ if (label && VIR_STRDUP(cmd->seLinuxLabel, label) < 0)
cmd->has_error = ENOMEM;
#endif
return;
@@ -1074,7 +1073,7 @@ virCommandSetAppArmorProfile(virCommandPtr cmd,
#if defined(WITH_SECDRIVER_APPARMOR)
VIR_FREE(cmd->appArmorProfile);
- if (profile && !(cmd->appArmorProfile = strdup(profile)))
+ if (profile && VIR_STRDUP(cmd->appArmorProfile, profile) < 0)
cmd->has_error = ENOMEM;
#endif
return;
@@ -1205,7 +1204,7 @@ virCommandAddEnvString(virCommandPtr cmd, const char *str)
if (!cmd || cmd->has_error)
return;
- if (!(env = strdup(str))) {
+ if (VIR_STRDUP(env, str) < 0) {
cmd->has_error = ENOMEM;
return;
}
@@ -1309,7 +1308,7 @@ virCommandAddArg(virCommandPtr cmd, const char *val)
if (!cmd || cmd->has_error)
return;
- if (!(arg = strdup(val))) {
+ if (VIR_STRDUP(arg, val) < 0) {
cmd->has_error = ENOMEM;
return;
}
@@ -1350,11 +1349,11 @@ virCommandAddArgBuffer(virCommandPtr cmd, virBufferPtr buf)
}
cmd->args[cmd->nargs] = virBufferContentAndReset(buf);
- if (!cmd->args[cmd->nargs])
- cmd->args[cmd->nargs] = strdup("");
if (!cmd->args[cmd->nargs]) {
- cmd->has_error = ENOMEM;
- return;
+ if (VIR_STRDUP(cmd->args[cmd->nargs], "") < 0) {
+ cmd->has_error = ENOMEM;
+ return;
+ }
}
cmd->nargs++;
}
@@ -1440,8 +1439,9 @@ virCommandAddArgSet(virCommandPtr cmd, const char *const*vals)
narg = 0;
while (vals[narg] != NULL) {
- char *arg = strdup(vals[narg++]);
- if (!arg) {
+ char *arg;
+
+ if (VIR_STRDUP(arg, vals[narg++]) < 0) {
cmd->has_error = ENOMEM;
return;
}
@@ -1481,8 +1481,7 @@ virCommandAddArgList(virCommandPtr cmd, ...)
char *arg = va_arg(list, char *);
if (!arg)
break;
- arg = strdup(arg);
- if (!arg) {
+ if (VIR_STRDUP(arg, arg) < 0) {
cmd->has_error = ENOMEM;
va_end(list);
return;
@@ -1511,8 +1510,7 @@ virCommandSetWorkingDirectory(virCommandPtr cmd, const char *pwd)
cmd->has_error = -1;
VIR_DEBUG("cannot set directory twice");
} else {
- cmd->pwd = strdup(pwd);
- if (!cmd->pwd)
+ if (VIR_STRDUP(cmd->pwd, pwd) < 0)
cmd->has_error = ENOMEM;
}
}
@@ -1539,8 +1537,7 @@ virCommandSetInputBuffer(virCommandPtr cmd, const char *inbuf)
return;
}
- cmd->inbuf = strdup(inbuf);
- if (!cmd->inbuf)
+ if (VIR_STRDUP(cmd->inbuf, inbuf) < 0)
cmd->has_error = ENOMEM;
}
diff --git a/src/util/virconf.c b/src/util/virconf.c
index 16f074a..c578bf4 100644
--- a/src/util/virconf.c
+++ b/src/util/virconf.c
@@ -23,7 +23,6 @@
#include <config.h>
#include <string.h>
-
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
@@ -38,6 +37,7 @@
#include "virlog.h"
#include "viralloc.h"
#include "virfile.h"
+#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_CONF
@@ -397,11 +397,8 @@ virConfParseString(virConfParserCtxtPtr ctxt)
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
return NULL;
}
- ret = strndup(base, ctxt->cur - base);
- if (ret == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
return NULL;
- }
NEXT;
} else if ((ctxt->cur + 6 < ctxt->end) &&
(STRPREFIX(ctxt->cur, "\"\"\""))) {
@@ -421,11 +418,8 @@ virConfParseString(virConfParserCtxtPtr ctxt)
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
return NULL;
}
- ret = strndup(base, ctxt->cur - base);
- if (ret == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
return NULL;
- }
ctxt->cur += 3;
} else if (CUR == '"') {
NEXT;
@@ -436,11 +430,8 @@ virConfParseString(virConfParserCtxtPtr ctxt)
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
return NULL;
}
- ret = strndup(base, ctxt->cur - base);
- if (ret == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
return NULL;
- }
NEXT;
}
return ret;
@@ -577,11 +568,8 @@ virConfParseName(virConfParserCtxtPtr ctxt)
((ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) &&
((CUR == ':') || (CUR == '.') || (CUR == '-')))))
NEXT;
- ret = strndup(base, ctxt->cur - base);
- if (ret == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
return NULL;
- }
return ret;
}
@@ -604,11 +592,8 @@ virConfParseComment(virConfParserCtxtPtr ctxt)
NEXT;
base = ctxt->cur;
while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
- comm = strndup(base, ctxt->cur - base);
- if (comm == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(comm, base, ctxt->cur - base) < 0)
return -1;
- }
virConfAddEntry(ctxt->conf, NULL, NULL, comm);
return 0;
}
@@ -680,9 +665,7 @@ virConfParseStatement(virConfParserCtxtPtr ctxt)
NEXT;
base = ctxt->cur;
while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
- comm = strndup(base, ctxt->cur - base);
- if (comm == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(comm, base, ctxt->cur - base) < 0) {
VIR_FREE(name);
virConfFreeValue(value);
return -1;
@@ -905,8 +888,7 @@ virConfSetValue(virConfPtr conf,
return -1;
}
cur->comment = NULL;
- if (!(cur->name = strdup(setting))) {
- virReportOOMError();
+ if (VIR_STRDUP(cur->name, setting) < 0) {
virConfFreeValue(value);
VIR_FREE(cur);
return -1;
diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c
index 36f31d2..d37ee3e 100644
--- a/src/util/virdnsmasq.c
+++ b/src/util/virdnsmasq.c
@@ -118,8 +118,8 @@ addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
if (VIR_ALLOC(addnhostsfile->hosts[idx].hostnames) < 0)
goto alloc_error;
- if (!(addnhostsfile->hosts[idx].ip = strdup(ipstr)))
- goto alloc_error;
+ if (VIR_STRDUP(addnhostsfile->hosts[idx].ip, ipstr) < 0)
+ goto error;
addnhostsfile->hosts[idx].nhostnames = 0;
addnhostsfile->nhosts++;
@@ -128,9 +128,9 @@ addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
if (VIR_REALLOC_N(addnhostsfile->hosts[idx].hostnames,
addnhostsfile->hosts[idx].nhostnames + 1) < 0)
goto alloc_error;
- if
(!(addnhostsfile->hosts[idx].hostnames[addnhostsfile->hosts[idx].nhostnames]
- = strdup(name)))
- goto alloc_error;
+ if
(VIR_STRDUP(addnhostsfile->hosts[idx].hostnames[addnhostsfile->hosts[idx].nhostnames],
+ name) < 0)
+ goto error;
VIR_FREE(ipstr);
@@ -140,6 +140,7 @@ addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
alloc_error:
virReportOOMError();
+ error:
VIR_FREE(ipstr);
return -1;
}
@@ -470,10 +471,8 @@ dnsmasqContextNew(const char *network_name,
return NULL;
}
- if (!(ctx->config_dir = strdup(config_dir))) {
- virReportOOMError();
+ if (VIR_STRDUP(ctx->config_dir, config_dir) < 0)
goto error;
- }
if (!(ctx->hostsfile = hostsfileNew(network_name, config_dir)))
goto error;
@@ -797,14 +796,15 @@ dnsmasqCapsNewEmpty(const char *binaryPath)
return NULL;
if (!(caps = virObjectNew(dnsmasqCapsClass)))
return NULL;
- if (!(caps->flags = virBitmapNew(DNSMASQ_CAPS_LAST)))
+ if (!(caps->flags = virBitmapNew(DNSMASQ_CAPS_LAST))) {
+ virReportOOMError();
goto error;
- if (!(caps->binaryPath = strdup(binaryPath ? binaryPath : DNSMASQ)))
+ }
+ if (VIR_STRDUP(caps->binaryPath, binaryPath ? binaryPath : DNSMASQ) < 0)
goto error;
return caps;
error:
- virReportOOMError();
virObjectUnref(caps);
return NULL;
}
diff --git a/src/util/virebtables.c b/src/util/virebtables.c
index 6bc6fed..1ac0bd0 100644
--- a/src/util/virebtables.c
+++ b/src/util/virebtables.c
@@ -51,6 +51,8 @@
#include "virstring.h"
#include "virutil.h"
+#define VIR_FROM_THIS VIR_FROM_NONE
+
#if HAVE_FIREWALLD
static char *firewall_cmd_path = NULL;
@@ -113,7 +115,7 @@ ebtRuleFree(ebtRule *rule)
static int
ebtRulesAppend(ebtRules *rules,
char *rule,
- const char **argv,
+ char **argv,
int command_idx)
{
if (VIR_REALLOC_N(rules->rules, rules->nrules+1) < 0) {
@@ -186,10 +188,10 @@ ebtRulesNew(const char *table,
if (VIR_ALLOC(rules) < 0)
return NULL;
- if (!(rules->table = strdup(table)))
+ if (VIR_STRDUP(rules->table, table) < 0)
goto error;
- if (!(rules->chain = strdup(chain)))
+ if (VIR_STRDUP(rules->chain, chain) < 0)
goto error;
rules->rules = NULL;
@@ -207,7 +209,7 @@ ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg,
...)
{
va_list args;
int retval = ENOMEM;
- const char **argv;
+ char **argv;
char *rule = NULL;
const char *s;
int n, command_idx;
@@ -236,36 +238,36 @@ ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg,
...)
#if HAVE_FIREWALLD
if (firewall_cmd_path) {
- if (!(argv[n++] = strdup(firewall_cmd_path)))
+ if (VIR_STRDUP(argv[n++], firewall_cmd_path) < 0)
goto error;
- if (!(argv[n++] = strdup("--direct")))
+ if (VIR_STRDUP(argv[n++], "--direct") < 0)
goto error;
- if (!(argv[n++] = strdup("--passthrough")))
+ if (VIR_STRDUP(argv[n++], "--passthrough") < 0)
goto error;
- if (!(argv[n++] = strdup("eb")))
+ if (VIR_STRDUP(argv[n++], "eb") < 0)
goto error;
} else
#endif
- if (!(argv[n++] = strdup(EBTABLES_PATH)))
+ if (VIR_STRDUP(argv[n++], EBTABLES_PATH) < 0)
goto error;
command_idx = n;
if (action == ADD || action == REMOVE) {
- if (!(argv[n++] = strdup("--insert")))
+ if (VIR_STRDUP(argv[n++], "--insert") < 0)
goto error;
- if (!(argv[n++] = strdup(rules->chain)))
+ if (VIR_STRDUP(argv[n++], rules->chain) < 0)
goto error;
}
- if (!(argv[n++] = strdup(arg)))
+ if (VIR_STRDUP(argv[n++], arg) < 0)
goto error;
va_start(args, arg);
while ((s = va_arg(args, const char *))) {
- if (!(argv[n++] = strdup(s))) {
+ if (VIR_STRDUP(argv[n++], s) < 0) {
va_end(args);
goto error;
}
@@ -273,16 +275,16 @@ ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg,
...)
va_end(args);
- if (!(rule = virArgvToString(&argv[command_idx])))
+ if (!(rule = virArgvToString((const char **) &argv[command_idx])))
goto error;
if (action == REMOVE) {
VIR_FREE(argv[command_idx]);
- if (!(argv[command_idx] = strdup("--delete")))
+ if (VIR_STRDUP(argv[command_idx], "--delete") < 0)
goto error;
}
- if (virRun(argv, NULL) < 0) {
+ if (virRun((const char **)argv, NULL) < 0) {
retval = errno;
goto error;
}
diff --git a/src/util/virebtables.h b/src/util/virebtables.h
index 671d9dc..3dfe32b 100644
--- a/src/util/virebtables.h
+++ b/src/util/virebtables.h
@@ -31,7 +31,7 @@
typedef struct
{
char *rule;
- const char **argv;
+ char **argv;
int command_idx;
} ebtRule;
diff --git a/src/util/virerror.c b/src/util/virerror.c
index af4da8c..9b888e5 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -164,7 +164,8 @@ virErrorGenericFailure(virErrorPtr err)
err->code = VIR_ERR_INTERNAL_ERROR;
err->domain = VIR_FROM_NONE;
err->level = VIR_ERR_ERROR;
- err->message = strdup(_("An error occurred, but the cause is
unknown"));
+ ignore_value(VIR_STRDUP_QUIET(err->message,
+ _("An error occurred, but the cause is
unknown")));
}
@@ -184,13 +185,13 @@ virCopyError(virErrorPtr from,
to->code = from->code;
to->domain = from->domain;
to->level = from->level;
- if (from->message && !(to->message = strdup(from->message)))
+ if (from->message && VIR_STRDUP_QUIET(to->message, from->message)
< 0)
ret = -1;
- if (from->str1 && !(to->str1 = strdup(from->str1)))
+ if (from->str1 && VIR_STRDUP_QUIET(to->str1, from->str1) < 0)
ret = -1;
- if (from->str2 && !(to->str2 = strdup(from->str2)))
+ if (from->str2 && VIR_STRDUP_QUIET(to->str2, from->str2) < 0)
ret = -1;
- if (from->str3 && !(to->str3 = strdup(from->str3)))
+ if (from->str3 && VIR_STRDUP_QUIET(to->str3, from->str3) < 0)
ret = -1;
to->int1 = from->int1;
to->int2 = from->int2;
@@ -646,7 +647,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
* formats the message; drop message on OOM situations
*/
if (fmt == NULL) {
- str = strdup(_("No error message provided"));
+ ignore_value(VIR_STRDUP_QUIET(str, _("No error message provided")));
} else {
va_list ap;
va_start(ap, fmt);
@@ -666,11 +667,11 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
to->message = str;
to->level = level;
if (str1 != NULL)
- to->str1 = strdup(str1);
+ ignore_value(VIR_STRDUP_QUIET(to->str1, str1));
if (str2 != NULL)
- to->str2 = strdup(str2);
+ ignore_value(VIR_STRDUP_QUIET(to->str2, str2));
if (str3 != NULL)
- to->str3 = strdup(str3);
+ ignore_value(VIR_STRDUP_QUIET(to->str3, str3));
to->int1 = int1;
to->int2 = int2;
diff --git a/src/util/virhash.c b/src/util/virhash.c
index 2fe8751..bb708fc 100644
--- a/src/util/virhash.c
+++ b/src/util/virhash.c
@@ -30,6 +30,7 @@
#include "virlog.h"
#include "virhashcode.h"
#include "virrandom.h"
+#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
@@ -85,7 +86,9 @@ static bool virHashStrEqual(const void *namea, const void *nameb)
static void *virHashStrCopy(const void *name)
{
- return strdup(name);
+ char *ret;
+ ignore_value(VIR_STRDUP(ret, name));
+ return ret;
}
static void virHashStrFree(void *name)
diff --git a/src/util/viridentity.c b/src/util/viridentity.c
index c9efd3f..e2dc048 100644
--- a/src/util/viridentity.c
+++ b/src/util/viridentity.c
@@ -34,6 +34,7 @@
#include "virobject.h"
#include "virthread.h"
#include "virutil.h"
+#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_IDENTITY
@@ -150,12 +151,11 @@ virIdentityPtr virIdentityGetSystem(void)
_("Unable to lookup SELinux process context"));
goto cleanup;
}
- seccontext = strdup(con);
- freecon(con);
- if (!seccontext) {
- virReportOOMError();
+ if (VIR_STRDUP(seccontext, con) < 0) {
+ freecon(con);
goto cleanup;
}
+ freecon(con);
#endif
if (!(ret = virIdentityNew()))
@@ -246,12 +246,7 @@ int virIdentitySetAttr(virIdentityPtr ident,
goto cleanup;
}
- if (!(ident->attrs[attr] = strdup(value))) {
- virReportOOMError();
- goto cleanup;
- }
-
- ret = 0;
+ ret = VIR_STRDUP(ident->attrs[attr], value);
cleanup:
return ret;
diff --git a/src/util/virinitctl.c b/src/util/virinitctl.c
index 59db057..b980952 100644
--- a/src/util/virinitctl.c
+++ b/src/util/virinitctl.c
@@ -137,10 +137,8 @@ int virInitctlSetRunLevel(virInitctlRunLevel level,
return -1;
}
} else {
- if (!(path = strdup(VIR_INITCTL_FIFO))) {
- virReportOOMError();
+ if (VIR_STRDUP(path, VIR_INITCTL_FIFO) < 0)
return -1;
- }
}
if ((fd = open(path, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY)) < 0) {
diff --git a/src/util/viriptables.c b/src/util/viriptables.c
index 06a1356..88c3bcd 100644
--- a/src/util/viriptables.c
+++ b/src/util/viriptables.c
@@ -118,10 +118,10 @@ iptRulesNew(const char *table,
if (VIR_ALLOC(rules) < 0)
return NULL;
- if (!(rules->table = strdup(table)))
+ if (VIR_STRDUP(rules->table, table) < 0)
goto error;
- if (!(rules->chain = strdup(chain)))
+ if (VIR_STRDUP(rules->chain, chain) < 0)
goto error;
return rules;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 92138d3..66376c1 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -107,7 +107,7 @@ virJSONValuePtr virJSONValueNewString(const char *data)
return NULL;
val->type = VIR_JSON_TYPE_STRING;
- if (!(val->data.string = strdup(data))) {
+ if (VIR_STRDUP(val->data.string, data) < 0) {
VIR_FREE(val);
return NULL;
}
@@ -126,7 +126,7 @@ virJSONValuePtr virJSONValueNewStringLen(const char *data, size_t
length)
return NULL;
val->type = VIR_JSON_TYPE_STRING;
- if (!(val->data.string = strndup(data, length))) {
+ if (VIR_STRNDUP(val->data.string, data, length) < 0) {
VIR_FREE(val);
return NULL;
}
@@ -142,7 +142,7 @@ static virJSONValuePtr virJSONValueNewNumber(const char *data)
return NULL;
val->type = VIR_JSON_TYPE_NUMBER;
- if (!(val->data.number = strdup(data))) {
+ if (VIR_STRDUP(val->data.number, data) < 0) {
VIR_FREE(val);
return NULL;
}
@@ -269,7 +269,7 @@ int virJSONValueObjectAppend(virJSONValuePtr object, const char *key,
virJSONVal
if (virJSONValueObjectHasKey(object, key))
return -1;
- if (!(newkey = strdup(key)))
+ if (VIR_STRDUP(newkey, key) < 0)
return -1;
if (VIR_REALLOC_N(object->data.object.pairs,
@@ -751,10 +751,10 @@ static int virJSONParserHandleNumber(void *ctx,
yajl_size_t l)
{
virJSONParserPtr parser = ctx;
- char *str = strndup(s, l);
+ char *str;
virJSONValuePtr value;
- if (!str)
+ if (VIR_STRNDUP(str, s, l) < 0)
return -1;
value = virJSONValueNewNumber(str);
VIR_FREE(str);
@@ -808,8 +808,7 @@ static int virJSONParserHandleMapKey(void *ctx,
state = &parser->state[parser->nstate-1];
if (state->key)
return 0;
- state->key = strndup((const char *)stringVal, stringLen);
- if (!state->key)
+ if (VIR_STRNDUP(state->key, (const char *)stringVal, stringLen) < 0)
return 0;
return 1;
}
@@ -1094,8 +1093,7 @@ char *virJSONValueToString(virJSONValuePtr object,
goto cleanup;
}
- if (!(ret = strdup((const char *)str)))
- virReportOOMError();
+ ignore_value(VIR_STRDUP(ret, (const char *)str));
cleanup:
yajl_gen_free(g);
diff --git a/src/util/virkeyfile.c b/src/util/virkeyfile.c
index d77e95d..c589ab2 100644
--- a/src/util/virkeyfile.c
+++ b/src/util/virkeyfile.c
@@ -32,6 +32,7 @@
#include "virhash.h"
#include "virkeyfile.h"
#include "virerror.h"
+#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_CONF
@@ -121,10 +122,8 @@ static int virKeyFileParseGroup(virKeyFileParserCtxtPtr ctxt)
return -1;
}
- if (!(ctxt->groupname = strndup(name, ctxt->cur - name))) {
- virReportOOMError();
+ if (VIR_STRNDUP(ctxt->groupname, name, ctxt->cur - name) < 0)
return -1;
- }
NEXT;
@@ -167,10 +166,8 @@ static int virKeyFileParseValue(virKeyFileParserCtxtPtr ctxt)
return -1;
}
- if (!(key = strndup(keystart, ctxt->cur - keystart))) {
- virReportOOMError();
+ if (VIR_STRNDUP(key, keystart, ctxt->cur - keystart) < 0)
return -1;
- }
NEXT;
valuestart = ctxt->cur;
@@ -183,10 +180,8 @@ static int virKeyFileParseValue(virKeyFileParserCtxtPtr ctxt)
len = ctxt->cur - valuestart;
if (IS_EOF && !IS_EOL(CUR))
len++;
- if (!(value = strndup(valuestart, len))) {
- virReportOOMError();
+ if (VIR_STRNDUP(value, valuestart, len) < 0)
goto cleanup;
- }
if (virHashAddEntry(ctxt->group, key, value) < 0) {
VIR_FREE(value);
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 620a460..060ac2f 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -70,10 +70,8 @@ static char *virLockSpaceGetResourcePath(virLockSpacePtr lockspace,
return NULL;
}
} else {
- if (!(ret = strdup(resname))) {
- virReportOOMError();
+ if (VIR_STRDUP(ret, resname) < 0)
return NULL;
- }
}
return ret;
@@ -132,8 +130,8 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
res->fd = -1;
res->flags = flags;
- if (!(res->name = strdup(resname)))
- goto no_memory;
+ if (VIR_STRDUP(res->name, resname) < 0)
+ goto error;
if (!(res->path = virLockSpaceGetResourcePath(lockspace, resname)))
goto no_memory;
@@ -262,9 +260,8 @@ virLockSpacePtr virLockSpaceNew(const char *directory)
return NULL;
}
- if (directory &&
- !(lockspace->dir = strdup(directory)))
- goto no_memory;
+ if (directory && VIR_STRDUP(lockspace->dir, directory) < 0)
+ goto error;
if (!(lockspace->resources = virHashCreate(VIR_LOCKSPACE_TABLE_SIZE,
virLockSpaceResourceDataFree)))
@@ -290,8 +287,6 @@ virLockSpacePtr virLockSpaceNew(const char *directory)
return lockspace;
-no_memory:
- virReportOOMError();
error:
virLockSpaceFree(lockspace);
return NULL;
@@ -324,10 +319,8 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr
object)
if (virJSONValueObjectHasKey(object, "directory")) {
const char *dir = virJSONValueObjectGetString(object, "directory");
- if (!(lockspace->dir = strdup(dir))) {
- virReportOOMError();
+ if (VIR_STRDUP(lockspace->dir, dir) < 0)
goto error;
- }
}
if (!(resources = virJSONValueObjectGet(object, "resources"))) {
@@ -362,8 +355,7 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr
object)
virLockSpaceResourceFree(res);
goto error;
}
- if (!(res->name = strdup(tmp))) {
- virReportOOMError();
+ if (VIR_STRDUP(res->name, tmp) < 0) {
virLockSpaceResourceFree(res);
goto error;
}
@@ -374,8 +366,7 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr
object)
virLockSpaceResourceFree(res);
goto error;
}
- if (!(res->path = strdup(tmp))) {
- virReportOOMError();
+ if (VIR_STRDUP(res->path, tmp) < 0) {
virLockSpaceResourceFree(res);
goto error;
}
diff --git a/src/util/virlog.c b/src/util/virlog.c
index eee9ddc..2ed0b10 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -557,8 +557,7 @@ virLogDefineFilter(const char *match,
}
}
- mdup = strdup(match);
- if (mdup == NULL) {
+ if (VIR_STRDUP(mdup, match) < 0) {
i = -1;
goto cleanup;
}
@@ -664,13 +663,9 @@ virLogDefineOutput(virLogOutputFunc f,
if (f == NULL)
return -1;
- if (dest == VIR_LOG_TO_SYSLOG || dest == VIR_LOG_TO_FILE) {
- if (name == NULL)
- return -1;
- ndup = strdup(name);
- if (ndup == NULL)
- return -1;
- }
+ if ((dest == VIR_LOG_TO_SYSLOG || dest == VIR_LOG_TO_FILE) &&
+ (!name || VIR_STRDUP(ndup, name) < 0))
+ return -1;
virLogLock();
if (VIR_REALLOC_N(virLogOutputs, virLogNbOutputs + 1)) {
@@ -1047,8 +1042,7 @@ virLogAddOutputToSyslog(virLogPriority priority,
* ident needs to be kept around on Solaris
*/
VIR_FREE(current_ident);
- current_ident = strdup(ident);
- if (current_ident == NULL)
+ if (VIR_STRDUP(current_ident, ident) < 0)
return -1;
openlog(current_ident, 0, 0);
@@ -1329,8 +1323,7 @@ virLogParseOutputs(const char *outputs)
if (str == cur)
goto cleanup;
#if HAVE_SYSLOG_H
- name = strndup(str, cur - str);
- if (name == NULL)
+ if (VIR_STRNDUP(name, str, cur - str) < 0)
goto cleanup;
if (virLogAddOutputToSyslog(prio, name) == 0)
count++;
@@ -1346,8 +1339,7 @@ virLogParseOutputs(const char *outputs)
cur++;
if (str == cur)
goto cleanup;
- name = strndup(str, cur - str);
- if (name == NULL)
+ if (VIR_STRNDUP(name, str, cur - str) < 0)
goto cleanup;
if (virFileAbsPath(name, &abspath) < 0) {
VIR_FREE(name);
@@ -1424,8 +1416,7 @@ virLogParseFilters(const char *filters)
cur++;
if (str == cur)
goto cleanup;
- name = strndup(str, cur - str);
- if (name == NULL)
+ if (VIR_STRNDUP(name, str, cur - str) < 0)
goto cleanup;
if (virLogDefineFilter(name, prio, flags) >= 0)
count++;
diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index 0c4fcbd..93bf798 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -31,6 +31,7 @@
#include "virmacaddr.h"
#include "virerror.h"
#include "virthread.h"
+#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NET
@@ -764,14 +765,14 @@ virNetDevMacVLanVPortProfileRegisterCallback(const char *ifname,
if (virtPortProfile && virNetlinkEventServiceIsRunning(NETLINK_ROUTE)) {
if (VIR_ALLOC(calld) < 0)
goto memory_error;
- if ((calld->cr_ifname = strdup(ifname)) == NULL)
- goto memory_error;
+ if (VIR_STRDUP(calld->cr_ifname, ifname) < 0)
+ goto error;
if (VIR_ALLOC(calld->virtPortProfile) < 0)
goto memory_error;
memcpy(calld->virtPortProfile, virtPortProfile, sizeof(*virtPortProfile));
virMacAddrSet(&calld->macaddress, macaddress);
- if ((calld->linkdev = strdup(linkdev)) == NULL)
- goto memory_error;
+ if (VIR_STRDUP(calld->linkdev, linkdev) < 0)
+ goto error;
memcpy(calld->vmuuid, vmuuid, sizeof(calld->vmuuid));
calld->vmOp = vmOp;
@@ -927,16 +928,13 @@ create_name:
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
goto disassociate_exit;
}
- if (!(*res_ifname = strdup(cr_ifname))) {
+ if (VIR_STRDUP(*res_ifname, cr_ifname) < 0) {
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
- virReportOOMError();
goto disassociate_exit;
}
} else {
- if (!(*res_ifname = strdup(cr_ifname))) {
- virReportOOMError();
+ if (VIR_STRDUP(*res_ifname, cr_ifname) < 0)
goto disassociate_exit;
- }
rc = 0;
}
diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index 75599db..08a5599 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -64,12 +64,7 @@ virNetDevTapGetName(int tapfd ATTRIBUTE_UNUSED, char **ifname
ATTRIBUTE_UNUSED)
return -1;
}
- *ifname = strdup(ifr.ifr_name);
- if (*ifname == NULL) {
- virReportOOMError();
- return -1;
- }
- return 0;
+ return VIR_STRDUP(*ifname, ifr.ifr_name);
#else
return -1;
#endif
@@ -204,10 +199,8 @@ int virNetDevTapCreate(char **ifname,
}
VIR_FREE(*ifname);
- if (!(*ifname = strdup(ifr.ifr_name))) {
- virReportOOMError();
+ if (VIR_STRDUP(*ifname, ifr.ifr_name) < 0)
goto cleanup;
- }
if (tapfd)
*tapfd = fd;
else
diff --git a/src/util/virnetdevvportprofile.c b/src/util/virnetdevvportprofile.c
index 883aa46..c337fb3 100644
--- a/src/util/virnetdevvportprofile.c
+++ b/src/util/virnetdevvportprofile.c
@@ -1034,9 +1034,7 @@ virNetDevVPortProfileOp8021Qbh(const char *ifname,
goto cleanup;
}
} else {
- physfndev = strdup(ifname);
- if (!physfndev) {
- virReportOOMError();
+ if (VIR_STRDUP(physfndev, ifname) < 0) {
rc = -1;
goto cleanup;
}
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 93e37e4..72a5248 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -28,6 +28,7 @@
#include "viratomic.h"
#include "virerror.h"
#include "virlog.h"
+#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
@@ -37,7 +38,7 @@ struct _virClass {
virClassPtr parent;
unsigned int magic;
- const char *name;
+ char *name;
size_t objectSize;
virObjectDisposeCallback dispose;
@@ -131,21 +132,22 @@ virClassPtr virClassNew(virClassPtr parent,
return NULL;
}
- if (VIR_ALLOC(klass) < 0)
- goto no_memory;
+ if (VIR_ALLOC(klass) < 0) {
+ virReportOOMError();
+ goto error;
+ }
klass->parent = parent;
- if (!(klass->name = strdup(name)))
- goto no_memory;
+ if (VIR_STRDUP(klass->name, name) < 0)
+ goto error;
klass->magic = virAtomicIntInc(&magicCounter);
klass->objectSize = objectSize;
klass->dispose = dispose;
return klass;
-no_memory:
+error:
VIR_FREE(klass);
- virReportOOMError();
return NULL;
}
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 5865613..1f19d3f 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -947,10 +947,8 @@ virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
goto cleanup;
}
/* drvdir = "/sys/bus/pci/drivers/${drivername}" */
- if (!(driver = strdup(last_component(drvdir)))) {
- virReportOOMError();
+ if (VIR_STRDUP(driver, last_component(drvdir)) < 0)
goto cleanup;
- }
if (!dev->unbind_from_stub)
goto remove_slot;
@@ -1392,10 +1390,8 @@ virPCIGetAddrString(unsigned int domain,
dev = virPCIDeviceNew(domain, bus, slot, function);
if (dev != NULL) {
- if ((*pciConfigAddr = strdup(dev->name)) == NULL) {
- virReportOOMError();
+ if (VIR_STRDUP(*pciConfigAddr, dev->name) < 0)
goto cleanup;
- }
ret = 0;
}
@@ -2268,11 +2264,7 @@ virPCIGetNetName(char *device_link_sysfs_path, char **netname)
continue;
/* Assume a single directory entry */
- *netname = strdup(entry->d_name);
- if (!*netname)
- virReportOOMError();
- else
- ret = 0;
+ ret = VIR_STRDUP(*netname, entry->d_name);
break;
}
diff --git a/src/util/virsexpr.c b/src/util/virsexpr.c
index 23b6781..b17c0d4 100644
--- a/src/util/virsexpr.c
+++ b/src/util/virsexpr.c
@@ -119,16 +119,10 @@ sexpr_string(const char *str, ssize_t len)
if (ret == NULL)
return ret;
ret->kind = SEXPR_VALUE;
- if (len > 0) {
- ret->u.value = strndup(str, len);
- } else {
- ret->u.value = strdup(str);
- }
- if (ret->u.value == NULL) {
+ if ((len > 0 && VIR_STRNDUP(ret->u.value, str, len) < 0) ||
+ (len <= 0 && VIR_STRDUP(ret->u.value, str) < 0))
VIR_FREE(ret);
- return NULL;
- }
return ret;
}
@@ -325,11 +319,8 @@ _string2sexpr(const char *buffer, size_t * end)
ptr++;
}
- ret->u.value = strndup(start, ptr - start);
- if (ret->u.value == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(ret->u.value, start, ptr - start) < 0)
goto error;
- }
if (*ptr == '\'')
ptr++;
@@ -341,11 +332,8 @@ _string2sexpr(const char *buffer, size_t * end)
ptr++;
}
- ret->u.value = strndup(start, ptr - start);
- if (ret->u.value == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(ret->u.value, start, ptr - start) < 0)
goto error;
- }
}
ret->kind = SEXPR_VALUE;
@@ -403,12 +391,8 @@ sexpr_lookup_key(const struct sexpr *sexpr, const char *node)
if ((node == NULL) || (sexpr == NULL))
return NULL;
- buffer = strdup(node);
-
- if (buffer == NULL) {
- virReportOOMError();
+ if (VIR_STRDUP(buffer, node) < 0)
return NULL;
- }
ptr = buffer;
token = strsep(&ptr, "/");
@@ -527,13 +511,10 @@ int sexpr_node_copy(const struct sexpr *sexpr, const char *node,
char **dst)
{
const char *val = sexpr_node(sexpr, node);
- if (val && *val) {
- *dst = strdup(val);
- if (!(*dst))
- return -1;
- } else {
- *dst = NULL;
- }
+ if (val && *val)
+ return VIR_STRDUP(*dst, val);
+
+ *dst = NULL;
return 0;
}
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index d6a0d93..1a6714e 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -272,8 +272,8 @@ virSocketAddrFormatFull(virSocketAddrPtr addr,
separator ? separator : ":") < 0)
goto no_memory;
} else {
- if (!(addrstr = strdup("127.0.0.1")))
- goto no_memory;
+ if (VIR_STRDUP(addrstr, "127.0.0.1") < 0)
+ goto error;
}
return addrstr;
}
@@ -293,14 +293,15 @@ virSocketAddrFormatFull(virSocketAddrPtr addr,
if (virAsprintf(&addrstr, "%s%s%s", host, separator, port) == -1)
goto no_memory;
} else {
- if (!(addrstr = strdup(host)))
- goto no_memory;
+ if (VIR_STRDUP(addrstr, host) < 0)
+ goto error;
}
return addrstr;
no_memory:
virReportOOMError();
+error:
return NULL;
}
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index c7bb85a..125f014 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -228,11 +228,8 @@ cowGetBackingStore(char **res,
return BACKING_STORE_OK;
}
- *res = strndup((const char*)buf + 4+4, COW_FILENAME_MAXLEN);
- if (*res == NULL) {
- virReportOOMError();
+ if (VIR_STRNDUP(*res, (const char*)buf + 4+4, COW_FILENAME_MAXLEN) < 0)
return BACKING_STORE_ERROR;
- }
return BACKING_STORE_OK;
}
@@ -440,11 +437,8 @@ vmdk4GetBackingStore(char **res,
goto cleanup;
}
*end = '\0';
- *res = strdup(start);
- if (*res == NULL) {
- virReportOOMError();
+ if (VIR_STRDUP(*res, start) < 0)
goto cleanup;
- }
ret = BACKING_STORE_OK;
@@ -775,9 +769,7 @@ virStorageFileGetMetadataInternal(const char *path,
meta->backingStoreIsFile = false;
if (backing != NULL) {
- meta->backingStore = strdup(backing);
- if (meta->backingStore == NULL) {
- virReportOOMError();
+ if (VIR_STRDUP(meta->backingStore, backing) < 0) {
VIR_FREE(backing);
goto cleanup;
}
@@ -1096,10 +1088,8 @@ int virStorageFileIsSharedFSType(const char *path,
struct statfs sb;
int statfs_ret;
- if ((dirpath = strdup(path)) == NULL) {
- virReportOOMError();
+ if (VIR_STRDUP(dirpath, path) < 0)
return -1;
- }
do {
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 394a558..13c2420 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -85,8 +85,8 @@ char **virStringSplit(const char *string,
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
goto no_memory;
- if (!(tokens[ntokens] = strndup(remainder, len)))
- goto no_memory;
+ if (VIR_STRNDUP(tokens[ntokens], remainder, len) < 0)
+ goto error;
ntokens++;
remainder = tmp + delimlen;
tmp = strstr(remainder, delim);
@@ -96,8 +96,8 @@ char **virStringSplit(const char *string,
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
goto no_memory;
- if (!(tokens[ntokens] = strdup(remainder)))
- goto no_memory;
+ if (VIR_STRDUP(tokens[ntokens], remainder) < 0)
+ goto error;
ntokens++;
}
@@ -109,6 +109,7 @@ char **virStringSplit(const char *string,
no_memory:
virReportOOMError();
+error:
for (i = 0 ; i < ntokens ; i++)
VIR_FREE(tokens[i]);
VIR_FREE(tokens);
@@ -144,12 +145,8 @@ char *virStringJoin(const char **strings,
return NULL;
}
ret = virBufferContentAndReset(&buf);
- if (!ret) {
- if (!(ret = strdup(""))) {
- virReportOOMError();
- return NULL;
- }
- }
+ if (!ret)
+ ignore_value(VIR_STRDUP(ret, ""));
return ret;
}
diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c
index 2efe634..7fd2537 100644
--- a/src/util/virsysinfo.c
+++ b/src/util/virsysinfo.c
@@ -144,32 +144,26 @@ virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol &&
- ((ret->system_family = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_family, cur, eol - cur) < 0)
+ return -1;
if ((cur = strstr(base, "model")) != NULL) {
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol && ((ret->system_serial = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_serial, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "machine")) != NULL) {
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol && ((ret->system_version = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_version, cur, eol - cur) < 0)
+ return -1;
}
return 0;
-
-no_memory:
- return -1;
}
static int
@@ -185,34 +179,31 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
cur = strchr(base, ':') + 1;
if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0) {
- goto no_memory;
+ return -1;
}
processor = &ret->processor[ret->nprocessor - 1];
virSkipSpaces(&cur);
- if (eol &&
- ((processor->processor_socket_destination = strndup
- (cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_socket_destination,
+ cur, eol - cur) < 0)
+ return -1;
if ((cur = strstr(base, "cpu")) != NULL) {
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol &&
- ((processor->processor_type = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_type,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "revision")) != NULL) {
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol &&
- ((processor->processor_version = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNUDP(processor->processor_version,
+ cur, eol - cur) < 0)
+ return -1;
}
base = cur;
@@ -270,32 +261,26 @@ virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol &&
- ((ret->system_family = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_family, cur, eol - cur) < 0)
+ return -1;
if ((cur = strstr(base, "model")) != NULL) {
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol && ((ret->system_serial = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_serial, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "machine")) != NULL) {
cur = strchr(cur, ':') + 1;
eol = strchr(cur, '\n');
virSkipSpaces(&cur);
- if (eol && ((ret->system_version = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_version, cur, eol - cur) < 0)
+ return -1;
}
return 0;
-
-no_memory:
- return -1;
}
static int
@@ -313,10 +298,8 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
eol = strchr(base, '\n');
cur = strchr(base, ':') + 1;
virSkipSpaces(&cur);
- if (eol &&
- ((processor_type = strndup(cur, eol - cur))
- == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor_type, cur, eol - cur) < 0)
+ goto error;
base = cur;
while ((tmp_base = strstr(base, "processor")) != NULL) {
@@ -325,19 +308,20 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
cur = strchr(base, ':') + 1;
if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0) {
- goto no_memory;
+ virReportOOMError();
+ goto error;
}
processor = &ret->processor[ret->nprocessor - 1];
virSkipSpaces(&cur);
if (eol &&
- ((processor->processor_socket_destination = strndup
- (cur, eol - cur)) == NULL))
- goto no_memory;
+ VIR_STRNDUP(processor->processor_socket_destination,
+ cur, eol - cur) < 0)
+ goto error;
if (processor_type &&
- !(processor->processor_type = strdup(processor_type)))
- goto no_memory;
+ VIR_STRDUP(processor->processor_type, processor_type) < 0)
+ goto error;
base = cur;
}
@@ -345,7 +329,7 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
VIR_FREE(processor_type);
return 0;
-no_memory:
+error:
VIR_FREE(processor_type);
return -1;
}
@@ -408,15 +392,11 @@ virSysinfoParseDelimited(const char *base, const char *name, char
**value,
start += 1;
end = strchrnul(start, delim2);
virSkipSpaces(&start);
- if (!((*value) = strndup(start, end - start))) {
- virReportOOMError();
- goto error;
- }
+ if (VIR_STRNDUP(*value, start, end - start) < 0)
+ return NULL;
virTrimSpaces(*value, NULL);
return end;
}
-
-error:
return NULL;
}
@@ -462,7 +442,8 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
goto cleanup;
}
processor = &ret->processor[ret->nprocessor - 1];
- processor->processor_manufacturer = strdup(manufacturer);
+ if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0)
+ goto cleanup;
if (!virSysinfoParseDelimited(procline, "version",
&processor->processor_version,
'=', ',') ||
@@ -556,32 +537,29 @@ virSysinfoParseBIOS(const char *base, virSysinfoDefPtr ret)
if ((cur = strstr(base, "Vendor: ")) != NULL) {
cur += 8;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->bios_vendor = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->bios_vendor, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Version: ")) != NULL) {
cur += 9;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->bios_version = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->bios_version, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Release Date: ")) != NULL) {
cur += 14;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->bios_date = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->bios_date, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
cur += 15;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->bios_release = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->bios_release, cur, eol - cur) < 0)
+ return -1;
}
return 0;
-
-no_memory:
- return -1;
}
static int
@@ -596,51 +574,47 @@ virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
cur += 14;
eol = strchr(cur, '\n');
- if ((eol) &&
- ((ret->system_manufacturer = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_manufacturer, cur, eol - cur) <
0)
+ return -1;
}
if ((cur = strstr(base, "Product Name: ")) != NULL) {
cur += 14;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->system_product = strndup(cur, eol - cur)) ==
NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_product, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Version: ")) != NULL) {
cur += 9;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->system_version = strndup(cur, eol - cur)) ==
NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_version, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Serial Number: ")) != NULL) {
cur += 15;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->system_serial = strndup(cur, eol - cur)) ==
NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_serial, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "UUID: ")) != NULL) {
cur += 6;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->system_uuid = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_uuid, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "SKU Number: ")) != NULL) {
cur += 12;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->system_sku = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_sku, cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Family: ")) != NULL) {
cur += 8;
eol = strchr(cur, '\n');
- if ((eol) && ((ret->system_family = strndup(cur, eol - cur)) ==
NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(ret->system_family, cur, eol - cur) < 0)
+ return -1;
}
return 0;
-
-no_memory:
- return -1;
}
static int
@@ -654,117 +628,100 @@ virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
base = tmp_base;
eol = NULL;
- if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0) {
- goto no_memory;
- }
+ if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
+ return -1;
processor = &ret->processor[ret->nprocessor - 1];
if ((cur = strstr(base, "Socket Designation: ")) != NULL) {
cur += 20;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_socket_destination
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_socket_destination,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Type: ")) != NULL) {
cur += 6;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_type = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_type, cur, eol - cur)
< 0)
+ return -1;
}
if ((cur = strstr(base, "Family: ")) != NULL) {
cur += 8;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_family = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_family, cur, eol -
cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
cur += 14;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_manufacturer
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_manufacturer,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Signature: ")) != NULL) {
cur += 11;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_signature
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_signature,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Version: ")) != NULL) {
cur += 9;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_version = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_version,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "External Clock: ")) != NULL) {
cur += 16;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_external_clock
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_external_clock,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Max Speed: ")) != NULL) {
cur += 11;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_max_speed
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_max_speed,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Status: ")) != NULL) {
cur += 8;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_status = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_status, cur, eol -
cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Serial Number: ")) != NULL) {
cur += 15;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_serial_number
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_serial_number,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Part Number: ")) != NULL) {
cur += 13;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((processor->processor_part_number
- = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(processor->processor_part_number,
+ cur, eol - cur) < 0)
+ return -1;
}
base += strlen("Processor Information");
}
return 0;
-
-no_memory:
- return -1;
}
static int
@@ -778,9 +735,8 @@ virSysinfoParseMemory(const char *base, virSysinfoDefPtr ret)
base = tmp_base;
eol = NULL;
- if (VIR_EXPAND_N(ret->memory, ret->nmemory, 1) < 0) {
- goto no_memory;
- }
+ if (VIR_EXPAND_N(ret->memory, ret->nmemory, 1) < 0)
+ return -1;
memory = &ret->memory[ret->nmemory - 1];
if ((cur = strstr(base, "Size: ")) != NULL) {
@@ -790,87 +746,74 @@ virSysinfoParseMemory(const char *base, virSysinfoDefPtr ret)
goto next;
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_size = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_size, cur, eol - cur) <
0)
+ return -1;
}
if ((cur = strstr(base, "Form Factor: ")) != NULL) {
cur += 13;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_form_factor = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_form_factor,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Locator: ")) != NULL) {
cur += 9;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_locator = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_locator,cur, eol - cur) <
0)
+ return -1;
}
if ((cur = strstr(base, "Bank Locator: ")) != NULL) {
cur += 14;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_bank_locator = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_bank_locator,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Type: ")) != NULL) {
cur += 6;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_type = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_type, cur, eol - cur) <
0)
+ return -1;
}
if ((cur = strstr(base, "Type Detail: ")) != NULL) {
cur += 13;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_type_detail = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_type_detail, cur, eol - cur)
< 0)
+ return -1;
}
if ((cur = strstr(base, "Speed: ")) != NULL) {
cur += 7;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_speed = strndup(cur, eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_speed, cur, eol - cur) <
0)
+ return -1;
}
if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
cur += 14;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_manufacturer = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_manufacturer, cur, eol -
cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Serial Number: ")) != NULL) {
cur += 15;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_serial_number = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_serial_number,
+ cur, eol - cur) < 0)
+ return -1;
}
if ((cur = strstr(base, "Part Number: ")) != NULL) {
cur += 13;
eol = strchr(cur, '\n');
virSkipSpacesBackwards(cur, &eol);
- if ((eol) &&
- ((memory->memory_part_number = strndup(cur,
- eol - cur)) == NULL))
- goto no_memory;
+ if (eol && VIR_STRNDUP(memory->memory_part_number, cur, eol - cur)
< 0)
+ return -1;
}
next:
@@ -878,9 +821,6 @@ virSysinfoParseMemory(const char *base, virSysinfoDefPtr ret)
}
return 0;
-
-no_memory:
- return -1;
}
virSysinfoDefPtr
diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index a9f8a33..4eb5cd6 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -150,12 +150,8 @@ virTypedParameterAssign(virTypedParameterPtr param, const char
*name,
break;
case VIR_TYPED_PARAM_STRING:
param->value.s = va_arg(ap, char *);
- if (!param->value.s)
- param->value.s = strdup("");
- if (!param->value.s) {
- virReportOOMError();
+ if (!param->value.s && VIR_STRDUP(param->value.s, "")
< 0)
goto cleanup;
- }
break;
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -248,10 +244,8 @@ virTypedParameterAssignFromStr(virTypedParameterPtr param, const char
*name,
}
break;
case VIR_TYPED_PARAM_STRING:
- if (!(param->value.s = strdup(val))) {
- virReportOOMError();
+ if (VIR_STRDUP(param->value.s, val) < 0)
goto cleanup;
- }
break;
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -913,10 +907,8 @@ virTypedParamsAddString(virTypedParameterPtr *params,
}
*maxparams = max;
- if (value && !(str = strdup(value))) {
- virReportOOMError();
+ if (value && VIR_STRDUP(str, value) < 0)
goto error;
- }
if (virTypedParameterAssign(*params + n, name,
VIR_TYPED_PARAM_STRING, str) < 0) {
diff --git a/src/util/viruri.c b/src/util/viruri.c
index 2a75425..745b978 100644
--- a/src/util/viruri.c
+++ b/src/util/viruri.c
@@ -37,13 +37,13 @@ virURIParamAppend(virURIPtr uri,
char *pname = NULL;
char *pvalue = NULL;
- if (!(pname = strdup(name)))
- goto no_memory;
- if (!(pvalue = strdup(value)))
- goto no_memory;
+ if (VIR_STRDUP(pname, name) < 0 || VIR_STRDUP(pvalue, value) < 0)
+ goto error;
- if (VIR_RESIZE_N(uri->params, uri->paramsAlloc, uri->paramsCount, 1) <
0)
- goto no_memory;
+ if (VIR_RESIZE_N(uri->params, uri->paramsAlloc, uri->paramsCount, 1) < 0)
{
+ virReportOOMError();
+ goto error;
+ }
uri->params[uri->paramsCount].name = pname;
uri->params[uri->paramsCount].value = pvalue;
@@ -52,10 +52,9 @@ virURIParamAppend(virURIPtr uri,
return 0;
-no_memory:
+error:
VIR_FREE(pname);
VIR_FREE(pvalue);
- virReportOOMError();
return -1;
}
@@ -167,34 +166,29 @@ virURIParse(const char *uri)
return NULL;
}
- if (VIR_ALLOC(ret) < 0)
- goto no_memory;
+ if (VIR_ALLOC(ret) < 0) {
+ virReportOOMError();
+ goto error;
+ }
- if (xmluri->scheme &&
- !(ret->scheme = strdup(xmluri->scheme)))
- goto no_memory;
- if (xmluri->server &&
- !(ret->server = strdup(xmluri->server)))
- goto no_memory;
+ if (xmluri->scheme && VIR_STRDUP(ret->scheme, xmluri->scheme) <
0)
+ goto error;
+ if (xmluri->server && VIR_STRDUP(ret->server, xmluri->server) <
0)
+ goto error;
ret->port = xmluri->port;
- if (xmluri->path &&
- !(ret->path = strdup(xmluri->path)))
- goto no_memory;
+ if (xmluri->path && VIR_STRDUP(ret->path, xmluri->path) < 0)
+ goto error;
#ifdef HAVE_XMLURI_QUERY_RAW
- if (xmluri->query_raw &&
- !(ret->query = strdup(xmluri->query_raw)))
- goto no_memory;
+ if (xmluri->query_raw && VIR_STRDUP(ret->query, xmluri->query_raw)
< 0)
+ goto error;
#else
- if (xmluri->query &&
- !(ret->query = strdup(xmluri->query)))
- goto no_memory;
+ if (xmluri->query && VIR_STRDUP(ret->query, xmluri->query) < 0)
+ goto error;
#endif
- if (xmluri->fragment &&
- !(ret->fragment = strdup(xmluri->fragment)))
- goto no_memory;
- if (xmluri->user &&
- !(ret->user = strdup(xmluri->user)))
- goto no_memory;
+ if (xmluri->fragment && VIR_STRDUP(ret->fragment, xmluri->fragment)
< 0)
+ goto error;
+ if (xmluri->user && VIR_STRDUP(ret->user, xmluri->user) < 0)
+ goto error;
/* First check: does it even make sense to jump inside */
if (ret->server != NULL &&
@@ -220,8 +214,6 @@ virURIParse(const char *uri)
return ret;
-no_memory:
- virReportOOMError();
error:
xmlFreeURI(xmluri);
virURIFree(ret);
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 982d4a3..d3d77b2 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -547,11 +547,8 @@ virFileResolveLinkHelper(const char *linkpath,
if (lstat(linkpath, &st) < 0)
return -1;
- if (!S_ISLNK(st.st_mode)) {
- if (!(*resultpath = strdup(linkpath)))
- return -1;
- return 0;
- }
+ if (!S_ISLNK(st.st_mode))
+ return VIR_STRDUP(*resultpath, linkpath);
}
*resultpath = canonicalize_file_name(linkpath);
@@ -623,9 +620,11 @@ char *virFindFileInPath(const char *file)
* copy of that path, after validating that it is executable
*/
if (IS_ABSOLUTE_FILE_NAME(file)) {
- if (virFileIsExecutable(file))
- return strdup(file);
- else
+ if (virFileIsExecutable(file)) {
+ char *ret;
+ ignore_value(VIR_STRDUP(ret, file));
+ return ret;
+ } else
return NULL;
}
@@ -641,7 +640,7 @@ char *virFindFileInPath(const char *file)
/* copy PATH env so we can tweak it */
path = getenv("PATH");
- if (path == NULL || (path = strdup(path)) == NULL)
+ if (!path|| VIR_STRDUP(path, path) < 0)
return NULL;
/* for each path segment, append the file to search for and test for
@@ -1296,7 +1295,7 @@ virFileMakePathWithMode(const char *path,
int ret = -1;
char *tmp;
- if ((tmp = strdup(path)) == NULL)
+ if (VIR_STRDUP(tmp, path) < 0)
goto cleanup;
ret = virFileMakePathHelper(tmp, mode);
@@ -1503,8 +1502,7 @@ int virFileAbsPath(const char *path, char **abspath)
char *buf;
if (path[0] == '/') {
- if (!(*abspath = strdup(path)))
- return -1;
+ return VIR_STRDUP(*abspath, path);
} else {
buf = getcwd(NULL, 0);
if (buf == NULL)
@@ -1528,11 +1526,8 @@ virFileSanitizePath(const char *path)
char *cleanpath;
int idx = 0;
- cleanpath = strdup(path);
- if (!cleanpath) {
- virReportOOMError();
+ if (VIR_STRDUP(cleanpath, path) < 0)
return NULL;
- }
/* Need to sanitize:
* // -> //
@@ -1963,8 +1958,8 @@ char *virGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
* string as-is; it's up to callers to check whether "localhost"
* is allowed.
*/
- result = strdup(hostname);
- goto check_and_return;
+ ignore_value(VIR_STRDUP(result, hostname));
+ goto cleanup;
}
/* otherwise, it's a shortened, non-localhost, hostname. Attempt to
@@ -1978,8 +1973,8 @@ char *virGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
if (r != 0) {
VIR_WARN("getaddrinfo failed for '%s': %s",
hostname, gai_strerror(r));
- result = strdup(hostname);
- goto check_and_return;
+ ignore_value(VIR_STRDUP(result, hostname));
+ goto cleanup;
}
/* Tell static analyzers about getaddrinfo semantics. */
@@ -1991,14 +1986,14 @@ char *virGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
* localhost. Ignore the canonicalized name and just return the
* original hostname
*/
- result = strdup(hostname);
+ ignore_value(VIR_STRDUP(result, hostname));
else
/* Caller frees this string. */
- result = strdup(info->ai_canonname);
+ ignore_value(VIR_STRDUP(result, info->ai_canonname));
freeaddrinfo(info);
-check_and_return:
+cleanup:
if (result == NULL)
virReportOOMError();
return result;
@@ -2052,15 +2047,9 @@ static char *virGetUserEnt(uid_t uid,
return NULL;
}
- if (field == VIR_USER_ENT_DIRECTORY)
- ret = strdup(pw->pw_dir);
- else
- ret = strdup(pw->pw_name);
-
+ ignore_value(VIR_STRDUP(ret, field == VIR_USER_ENT_DIRECTORY ?
+ pw->pw_dir : pw->pw_name));
VIR_FREE(strbuf);
- if (!ret)
- virReportOOMError();
-
return ret;
}
@@ -2105,12 +2094,8 @@ static char *virGetGroupEnt(gid_t gid)
return NULL;
}
- ret = strdup(gr->gr_name);
-
+ ignore_value(VIR_STRDUP(ret, gr->gr_name));
VIR_FREE(strbuf);
- if (!ret)
- virReportOOMError();
-
return ret;
}
@@ -2444,12 +2429,8 @@ virGetWin32SpecialFolder(int csidl, char **path)
*path = NULL;
if (SHGetSpecialFolderLocation(NULL, csidl, &pidl) == S_OK) {
- if (SHGetPathFromIDList(pidl, buf)) {
- if (!(*path = strdup(buf))) {
- virReportOOMError();
- ret = -1;
- }
- }
+ if (SHGetPathFromIDList(pidl, buf) && VIR_STRDUP(*path, buf) < 0)
+ ret = -1;
CoTaskMemFree(pidl);
}
return ret;
@@ -2479,12 +2460,7 @@ virGetWin32DirectoryRoot(char **path)
strcpy(windowsdir, "C:\\");
}
- if (!(*path = strdup(windowsdir))) {
- virReportOOMError();
- ret = -1;
- }
-
- return ret;
+ return VIR_STRDUP(*path, windowsdir);
}
@@ -2517,12 +2493,8 @@ virGetUserDirectory(void)
/* USERPROFILE is probably the closest equivalent to $HOME? */
dir = getenv("USERPROFILE");
- if (dir) {
- if (!(ret = strdup(dir))) {
- virReportOOMError();
- return NULL;
- }
- }
+ if (dir && VIR_STRDUP(ret, dir) < 0)
+ return NULL;
if (!ret &&
virGetWin32SpecialFolder(CSIDL_PROFILE, &ret) < 0)
@@ -2810,7 +2782,8 @@ char *virFileFindMountPoint(const char *type)
while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) {
if (STREQ(mb.mnt_type, type)) {
- ret = strdup(mb.mnt_dir);
+ if (VIR_STRDUP(ret, mb.mnt_dir) < 0)
+ errno = ENOMEM;
goto cleanup;
}
}
@@ -3121,10 +3094,8 @@ virReadFCHost(const char *sysfs_prefix,
else
p = buf;
- if (!(*result = strndup(p, sizeof(buf)))) {
- virReportOOMError();
+ if (VIR_STRNDUP(*result, p, sizeof(buf)) < 0)
goto cleanup;
- }
ret = 0;
cleanup:
@@ -3348,7 +3319,7 @@ virGetFCHostNameByWWN(const char *sysfs_prefix,
continue;
}
- ret = strdup(entry->d_name);
+ ignore_value(VIR_STRDUP(ret, entry->d_name));
break;
}
@@ -3433,7 +3404,7 @@ virFindFCHostCapableVport(const char *sysfs_prefix)
if ((strlen(max_vports) >= strlen(vports)) ||
((strlen(max_vports) == strlen(vports)) &&
strcmp(max_vports, vports) > 0)) {
- ret = strdup(entry->d_name);
+ ignore_value(VIR_STRDUP(ret, entry->d_name));
goto cleanup;
}
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 26120b6..f8c595e 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -87,11 +87,8 @@ virXPathString(const char *xpath,
xmlXPathFreeObject(obj);
return NULL;
}
- ret = strdup((char *) obj->stringval);
+ ignore_value(VIR_STRDUP(ret, (char *) obj->stringval));
xmlXPathFreeObject(obj);
- if (ret == NULL) {
- virReportOOMError();
- }
return ret;
}
--
1.8.1.5