Make it obvious that the function always returns a valid pointer and fix
all callers.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/admin/admin_server_dispatch.c | 3 ---
src/locking/lock_daemon.c | 8 +-------
src/logging/log_daemon.c | 8 +-------
src/logging/log_handler.c | 5 -----
src/qemu/qemu_agent.c | 8 ++------
src/qemu/qemu_command.c | 3 +--
src/qemu/qemu_firmware.c | 20 ++++----------------
src/qemu/qemu_migration_params.c | 8 ++------
src/qemu/qemu_monitor_json.c | 30 +++++++-----------------------
src/qemu/qemu_vhost_user.c | 3 +--
src/rpc/virnetdaemon.c | 10 ++--------
src/rpc/virnetserver.c | 5 +----
src/rpc/virnetserverclient.c | 3 ---
src/rpc/virnetserverservice.c | 3 ---
src/rpc/virnetsocket.c | 3 +--
src/util/virjson.c | 21 ++++-----------------
src/util/virlease.c | 6 +-----
src/util/virlockspace.c | 6 ------
src/util/virmacmap.c | 5 +----
src/util/virnetdev.c | 3 +--
tests/qemumonitortestutils.c | 8 ++------
21 files changed, 32 insertions(+), 137 deletions(-)
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c
index 7b3bd697f3..b3da577995 100644
--- a/src/admin/admin_server_dispatch.c
+++ b/src/admin/admin_server_dispatch.c
@@ -119,9 +119,6 @@ virJSONValuePtr remoteAdmClientPreExecRestart(virNetServerClientPtr
client G_GNU
{
virJSONValuePtr object = virJSONValueNewObject();
- if (!object)
- return NULL;
-
/* No content to add at this time - just need empty object */
return object;
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index 5e5a0c1089..245155206a 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -774,9 +774,6 @@ virLockDaemonClientPreExecRestart(virNetServerClientPtr client
G_GNUC_UNUSED,
virJSONValuePtr object = virJSONValueNewObject();
char uuidstr[VIR_UUID_STRING_BUFLEN];
- if (!object)
- return NULL;
-
if (virJSONValueObjectAppendBoolean(object, "restricted",
priv->restricted) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Cannot set restricted data in JSON document"));
@@ -923,16 +920,13 @@ virLockDaemonPreExecRestart(const char *state_file,
virJSONValuePtr child;
char *state = NULL;
int ret = -1;
- virJSONValuePtr object;
+ virJSONValuePtr object = virJSONValueNewObject();
char *magic;
virHashKeyValuePairPtr pairs = NULL, tmp;
virJSONValuePtr lockspaces;
VIR_DEBUG("Running pre-restart exec");
- if (!(object = virJSONValueNewObject()))
- goto cleanup;
-
if (!(child = virNetDaemonPreExecRestart(dmn)))
goto cleanup;
diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c
index 772bbb805b..47377deb4f 100644
--- a/src/logging/log_daemon.c
+++ b/src/logging/log_daemon.c
@@ -597,9 +597,6 @@ virLogDaemonClientPreExecRestart(virNetServerClientPtr client
G_GNUC_UNUSED,
{
virJSONValuePtr object = virJSONValueNewObject();
- if (!object)
- return NULL;
-
return object;
}
@@ -718,15 +715,12 @@ virLogDaemonPreExecRestart(const char *state_file,
virJSONValuePtr child;
char *state = NULL;
int ret = -1;
- virJSONValuePtr object;
+ virJSONValuePtr object = virJSONValueNewObject();
char *magic;
virHashKeyValuePairPtr pairs = NULL;
VIR_DEBUG("Running pre-restart exec");
- if (!(object = virJSONValueNewObject()))
- goto cleanup;
-
if (!(child = virNetDaemonPreExecRestart(dmn)))
goto cleanup;
diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c
index 576d0d6921..87748d96d1 100644
--- a/src/logging/log_handler.c
+++ b/src/logging/log_handler.c
@@ -615,9 +615,6 @@ virLogHandlerPreExecRestart(virLogHandlerPtr handler)
size_t i;
char domuuid[VIR_UUID_STRING_BUFLEN];
- if (!ret)
- return NULL;
-
files = virJSONValueNewArray();
if (virJSONValueObjectAppend(ret, "files", files) < 0) {
@@ -627,8 +624,6 @@ virLogHandlerPreExecRestart(virLogHandlerPtr handler)
for (i = 0; i < handler->nfiles; i++) {
virJSONValuePtr file = virJSONValueNewObject();
- if (!file)
- goto error;
if (virJSONValueArrayAppend(files, file) < 0) {
virJSONValueFree(file);
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 23a775034f..cd25ef6cd3 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -1166,15 +1166,12 @@ static virJSONValuePtr G_GNUC_NULL_TERMINATED
qemuAgentMakeCommand(const char *cmdname,
...)
{
- virJSONValuePtr obj;
+ virJSONValuePtr obj = virJSONValueNewObject();
virJSONValuePtr jargs = NULL;
va_list args;
va_start(args, cmdname);
- if (!(obj = virJSONValueNewObject()))
- goto error;
-
if (virJSONValueObjectAppendString(obj, "execute", cmdname) < 0)
goto error;
@@ -1544,8 +1541,7 @@ qemuAgentSetVCPUsCommand(qemuAgentPtr mon,
(*nmodified)++;
/* create single cpu object */
- if (!(cpu = virJSONValueNewObject()))
- goto cleanup;
+ cpu = virJSONValueNewObject();
if (virJSONValueObjectAppendNumberInt(cpu, "logical-id", in->id)
< 0)
goto cleanup;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 9d215e5e06..9e0334a3e7 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -3442,8 +3442,7 @@ qemuBuildMemoryBackendProps(virJSONValuePtr *backendProps,
return -1;
}
- if (!(props = virJSONValueNewObject()))
- return -1;
+ props = virJSONValueNewObject();
if (def->mem.source == VIR_DOMAIN_MEMORY_SOURCE_MEMFD) {
backendType = "memory-backend-memfd";
diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c
index 68e2c6b40f..bd251c05fc 100644
--- a/src/qemu/qemu_firmware.c
+++ b/src/qemu/qemu_firmware.c
@@ -672,12 +672,9 @@ qemuFirmwareInterfaceFormat(virJSONValuePtr doc,
static virJSONValuePtr
qemuFirmwareFlashFileFormat(qemuFirmwareFlashFile flash)
{
- g_autoptr(virJSONValue) json = NULL;
+ g_autoptr(virJSONValue) json = virJSONValueNewObject();
virJSONValuePtr ret;
- if (!(json = virJSONValueNewObject()))
- return NULL;
-
if (virJSONValueObjectAppendString(json,
"filename",
flash.filename) < 0)
@@ -754,10 +751,7 @@ static int
qemuFirmwareMappingFormat(virJSONValuePtr doc,
qemuFirmwarePtr fw)
{
- g_autoptr(virJSONValue) mapping = NULL;
-
- if (!(mapping = virJSONValueNewObject()))
- return -1;
+ g_autoptr(virJSONValue) mapping = virJSONValueNewObject();
if (virJSONValueObjectAppendString(mapping,
"device",
@@ -802,13 +796,10 @@ qemuFirmwareTargetFormat(virJSONValuePtr doc,
for (i = 0; i < fw->ntargets; i++) {
qemuFirmwareTargetPtr t = fw->targets[i];
- g_autoptr(virJSONValue) target = NULL;
+ g_autoptr(virJSONValue) target = virJSONValueNewObject();
g_autoptr(virJSONValue) machines = NULL;
size_t j;
- if (!(target = virJSONValueNewObject()))
- return -1;
-
if (virJSONValueObjectAppendString(target,
"architecture",
virQEMUCapsArchToString(t->architecture))
< 0)
@@ -869,14 +860,11 @@ qemuFirmwareFeatureFormat(virJSONValuePtr doc,
char *
qemuFirmwareFormat(qemuFirmwarePtr fw)
{
- g_autoptr(virJSONValue) doc = NULL;
+ g_autoptr(virJSONValue) doc = virJSONValueNewObject();
if (!fw)
return NULL;
- if (!(doc = virJSONValueNewObject()))
- return NULL;
-
if (qemuFirmwareInterfaceFormat(doc, fw) < 0)
return NULL;
diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_params.c
index dd1ad9349b..a92bb2fa2b 100644
--- a/src/qemu/qemu_migration_params.c
+++ b/src/qemu/qemu_migration_params.c
@@ -729,15 +729,12 @@ qemuMigrationParamsFromJSON(virJSONValuePtr params)
virJSONValuePtr
qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
{
- virJSONValuePtr params = NULL;
+ virJSONValuePtr params = virJSONValueNewObject();
qemuMigrationParamValuePtr pv;
const char *name;
size_t i;
int rc;
- if (!(params = virJSONValueNewObject()))
- return NULL;
-
for (i = 0; i < QEMU_MIGRATION_PARAM_LAST; i++) {
name = qemuMigrationParamTypeToString(i);
pv = &migParams->params[i];
@@ -797,8 +794,7 @@ qemuMigrationCapsToJSON(virBitmapPtr caps,
ignore_value(virBitmapGetBit(states, bit, &state));
- if (!(cap = virJSONValueNewObject()))
- goto error;
+ cap = virJSONValueNewObject();
name = qemuMigrationCapabilityTypeToString(bit);
if (virJSONValueObjectAppendString(cap, "capability", name) < 0)
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 451e1afef5..5f0185d10b 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -690,15 +690,12 @@ qemuMonitorJSONParseKeywords(const char *str,
static virJSONValuePtr
qemuMonitorJSONKeywordStringToJSON(const char *str, const char *firstkeyword)
{
- virJSONValuePtr ret = NULL;
+ virJSONValuePtr ret = virJSONValueNewObject();
char **keywords = NULL;
char **values = NULL;
int nkeywords = 0;
size_t i;
- if (!(ret = virJSONValueNewObject()))
- return NULL;
-
if (qemuMonitorJSONParseKeywords(str, &keywords, &values, &nkeywords, 1)
< 0)
goto error;
@@ -3444,12 +3441,9 @@ qemuMonitorJSONSetMigrationParams(qemuMonitorPtr mon,
virJSONValuePtr params)
{
int ret = -1;
- virJSONValuePtr cmd = NULL;
+ virJSONValuePtr cmd = virJSONValueNewObject();
virJSONValuePtr reply = NULL;
- if (!(cmd = virJSONValueNewObject()))
- goto cleanup;
-
if (virJSONValueObjectAppendString(cmd, "execute",
"migrate-set-parameters") < 0)
goto cleanup;
@@ -4823,8 +4817,7 @@ int qemuMonitorJSONSendKey(qemuMonitorPtr mon,
}
/* create single key object */
- if (!(key = virJSONValueNewObject()))
- goto cleanup;
+ key = virJSONValueNewObject();
/* Union KeyValue has two types, use the generic one */
if (virJSONValueObjectAppendString(key, "type", "number")
< 0)
@@ -5817,19 +5810,15 @@ static virJSONValuePtr
qemuMonitorJSONMakeCPUModel(virCPUDefPtr cpu,
bool migratable)
{
- virJSONValuePtr model = NULL;
+ virJSONValuePtr model = virJSONValueNewObject();
virJSONValuePtr props = NULL;
size_t i;
- if (!(model = virJSONValueNewObject()))
- goto error;
-
if (virJSONValueObjectAppendString(model, "name", cpu->model) < 0)
goto error;
if (cpu->nfeatures || !migratable) {
- if (!(props = virJSONValueNewObject()))
- goto error;
+ props = virJSONValueNewObject();
for (i = 0; i < cpu->nfeatures; i++) {
char *name = cpu->features[i].name;
@@ -7382,8 +7371,8 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
const virDomainChrSourceDef *chr)
{
virJSONValuePtr ret = NULL;
- virJSONValuePtr backend = NULL;
- virJSONValuePtr data = NULL;
+ virJSONValuePtr backend = virJSONValueNewObject();
+ virJSONValuePtr data = virJSONValueNewObject();
virJSONValuePtr addr = NULL;
const char *backend_type = NULL;
const char *host;
@@ -7391,11 +7380,6 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
char *tlsalias = NULL;
bool telnet;
- if (!(backend = virJSONValueNewObject()) ||
- !(data = virJSONValueNewObject())) {
- goto cleanup;
- }
-
switch ((virDomainChrType)chr->type) {
case VIR_DOMAIN_CHR_TYPE_NULL:
case VIR_DOMAIN_CHR_TYPE_VC:
diff --git a/src/qemu/qemu_vhost_user.c b/src/qemu/qemu_vhost_user.c
index aee18c9670..be7c3dd5b8 100644
--- a/src/qemu/qemu_vhost_user.c
+++ b/src/qemu/qemu_vhost_user.c
@@ -217,8 +217,7 @@ qemuVhostUserFormat(qemuVhostUserPtr vu)
if (!vu)
return NULL;
- if (!(doc = virJSONValueNewObject()))
- return NULL;
+ doc = virJSONValueNewObject();
if (virJSONValueObjectAppendString(doc, "type",
qemuVhostUserTypeTypeToString(vu->type)) <
0)
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 7f2226b086..bb81a43d43 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -382,18 +382,12 @@ virJSONValuePtr
virNetDaemonPreExecRestart(virNetDaemonPtr dmn)
{
size_t i = 0;
- virJSONValuePtr object = NULL;
- virJSONValuePtr srvObj = NULL;
+ virJSONValuePtr object = virJSONValueNewObject();
+ virJSONValuePtr srvObj = virJSONValueNewObject();
virHashKeyValuePairPtr srvArray = NULL;
virObjectLock(dmn);
- if (!(object = virJSONValueNewObject()))
- goto error;
-
- if (!(srvObj = virJSONValueNewObject()))
- goto error;
-
if (virJSONValueObjectAppend(object, "servers", srvObj) < 0) {
virJSONValueFree(srvObj);
goto error;
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index c87dade1a8..a5998801de 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -546,16 +546,13 @@ virNetServerPtr virNetServerNewPostExecRestart(virJSONValuePtr
object,
virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
{
- virJSONValuePtr object;
+ virJSONValuePtr object = virJSONValueNewObject();
virJSONValuePtr clients;
virJSONValuePtr services;
size_t i;
virObjectLock(srv);
- if (!(object = virJSONValueNewObject()))
- goto error;
-
if (virJSONValueObjectAppendNumberUint(object, "min_workers",
virThreadPoolGetMinWorkers(srv->workers))
< 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 67b3bf9531..4d85ee25d7 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -593,9 +593,6 @@ virJSONValuePtr virNetServerClientPreExecRestart(virNetServerClientPtr
client)
virJSONValuePtr object = virJSONValueNewObject();
virJSONValuePtr child;
- if (!object)
- return NULL;
-
virObjectLock(client);
if (virJSONValueObjectAppendNumberUlong(object, "id",
diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c
index 0a003e5814..9d5df456a8 100644
--- a/src/rpc/virnetserverservice.c
+++ b/src/rpc/virnetserverservice.c
@@ -343,9 +343,6 @@ virJSONValuePtr
virNetServerServicePreExecRestart(virNetServerServicePtr svc)
virJSONValuePtr socks;
size_t i;
- if (!object)
- return NULL;
-
if (virJSONValueObjectAppendNumberInt(object, "auth", svc->auth) <
0)
goto error;
if (virJSONValueObjectAppendBoolean(object, "readonly", svc->readonly)
< 0)
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index a217404fa6..360711acb6 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -1283,8 +1283,7 @@ virJSONValuePtr virNetSocketPreExecRestart(virNetSocketPtr sock)
goto error;
}
- if (!(object = virJSONValueNewObject()))
- goto error;
+ object = virJSONValueNewObject();
if (virJSONValueObjectAppendNumberInt(object, "fd", sock->fd) < 0)
goto error;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index ca57df816f..2d7368b0b6 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -362,8 +362,7 @@ virJSONValueObjectCreateVArgs(virJSONValuePtr *obj,
{
int ret;
- if (!(*obj = virJSONValueNewObject()))
- return -1;
+ *obj = virJSONValueNewObject();
/* free the object on error, or if no value objects were added */
if ((ret = virJSONValueObjectAddVArgs(*obj, args)) <= 0) {
@@ -572,10 +571,7 @@ virJSONValueNewArray(void)
virJSONValuePtr
virJSONValueNewObject(void)
{
- virJSONValuePtr val;
-
- if (VIR_ALLOC(val) < 0)
- return NULL;
+ virJSONValuePtr val = g_new0(virJSONValue, 1);
val->type = VIR_JSON_TYPE_OBJECT;
@@ -1503,8 +1499,6 @@ virJSONValueCopy(const virJSONValue *in)
switch ((virJSONType) in->type) {
case VIR_JSON_TYPE_OBJECT:
out = virJSONValueNewObject();
- if (!out)
- return NULL;
for (i = 0; i < in->data.object.npairs; i++) {
virJSONValuePtr val = NULL;
if (!(val = virJSONValueCopy(in->data.object.pairs[i].value)))
@@ -1724,9 +1718,6 @@ virJSONParserHandleStartMap(void *ctx)
VIR_DEBUG("parser=%p", parser);
- if (!value)
- return 0;
-
if (virJSONParserInsertValue(parser, value) < 0) {
virJSONValueFree(value);
return 0;
@@ -2103,8 +2094,7 @@ virJSONValueObjectDeflattenWorker(const char *key,
}
if (!(existobj = virJSONValueObjectGet(retobj, tokens[0]))) {
- if (!(existobj = virJSONValueNewObject()))
- goto cleanup;
+ existobj = virJSONValueNewObject();
if (virJSONValueObjectAppend(retobj, tokens[0], existobj) < 0)
goto cleanup;
@@ -2143,10 +2133,7 @@ virJSONValueObjectDeflattenWorker(const char *key,
virJSONValuePtr
virJSONValueObjectDeflatten(virJSONValuePtr json)
{
- g_autoptr(virJSONValue) deflattened = NULL;
-
- if (!(deflattened = virJSONValueNewObject()))
- return NULL;
+ g_autoptr(virJSONValue) deflattened = virJSONValueNewObject();
if (virJSONValueObjectForeachKeyValue(json,
virJSONValueObjectDeflattenWorker,
diff --git a/src/util/virlease.c b/src/util/virlease.c
index e0f81539be..1cb9540d80 100644
--- a/src/util/virlease.c
+++ b/src/util/virlease.c
@@ -238,11 +238,7 @@ virLeaseNew(virJSONValuePtr *lease_ret,
}
/* Create new lease */
- if (!(lease_new = virJSONValueNewObject())) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("failed to create json"));
- return -1;
- }
+ lease_new = virJSONValueNewObject();
if (iaid && virJSONValueObjectAppendString(lease_new, "iaid", iaid)
< 0)
return -1;
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index a44377f89e..05517f8b40 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -434,9 +434,6 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
virJSONValuePtr resources;
virHashKeyValuePairPtr pairs = NULL, tmp;
- if (!object)
- return NULL;
-
virMutexLock(&lockspace->lock);
if (lockspace->dir &&
@@ -457,9 +454,6 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
virJSONValuePtr owners = NULL;
size_t i;
- if (!child)
- goto error;
-
if (virJSONValueArrayAppend(resources, child) < 0) {
virJSONValueFree(child);
goto error;
diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c
index ec589334ea..0c0ba90def 100644
--- a/src/util/virmacmap.c
+++ b/src/util/virmacmap.c
@@ -200,15 +200,12 @@ virMACMapHashDumper(void *payload,
const void *name,
void *data)
{
- virJSONValuePtr obj = NULL;
+ virJSONValuePtr obj = virJSONValueNewObject();
virJSONValuePtr arr = NULL;
const char **macs = payload;
size_t i;
int ret = -1;
- if (!(obj = virJSONValueNewObject()))
- goto cleanup;
-
arr = virJSONValueNewArray();
for (i = 0; macs[i]; i++) {
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 094d1ac09b..b465bdac2e 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -1874,8 +1874,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
}
}
- if (!(configJSON = virJSONValueNewObject()))
- goto cleanup;
+ configJSON = virJSONValueNewObject();
/* if there is a PF, it's now in pfDevName, and linkdev is either
* the VF's name, or NULL (if the VF isn't bound to a net driver
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index b29e5d8cd2..00f5b49439 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -529,12 +529,8 @@ qemuMonitorTestProcessCommandDefaultValidate(qemuMonitorTestPtr
test,
return -1;
}
- if (!args) {
- if (!(emptyargs = virJSONValueNewObject()))
- return -1;
-
- args = emptyargs;
- }
+ if (!args)
+ args = emptyargs = virJSONValueNewObject();
if (testQEMUSchemaValidate(args, schemaroot, test->qapischema, &debug) < 0)
{
if (qemuMonitorReportError(test,
--
2.24.1