The parent object takes ownership of the inserted value once all checks
pass. Don't make the callers second-guess when that happens and modify
the function to take a double pointer so that it can be cleared once the
ownership is taken.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/locking/lock_daemon.c | 9 +++----
src/logging/log_daemon.c | 6 ++---
src/logging/log_handler.c | 3 +--
src/node_device/node_device_driver.c | 2 +-
src/qemu/qemu_block.c | 3 +--
src/qemu/qemu_firmware.c | 23 +++++-----------
src/qemu/qemu_monitor_json.c | 22 ++++++----------
src/rpc/virnetdaemon.c | 6 ++---
src/rpc/virnetserver.c | 6 ++---
src/rpc/virnetserverclient.c | 6 ++---
src/rpc/virnetserverservice.c | 3 +--
src/util/virjson.c | 39 ++++++++++++----------------
src/util/virjson.h | 4 ++-
src/util/virlockspace.c | 6 ++---
src/util/virmacmap.c | 3 +--
15 files changed, 52 insertions(+), 89 deletions(-)
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index 5913c0cb9c..26905a462b 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -711,16 +711,14 @@ virLockDaemonPreExecRestart(const char *state_file,
if (!(daemon = virNetDaemonPreExecRestart(dmn)))
return -1;
- if (virJSONValueObjectAppend(object, "daemon", daemon) < 0)
+ if (virJSONValueObjectAppend(object, "daemon", &daemon) < 0)
return -1;
- daemon = NULL;
if (!(defaultLockspace =
virLockSpacePreExecRestart(lockDaemon->defaultLockspace)))
return -1;
- if (virJSONValueObjectAppend(object, "defaultLockspace", defaultLockspace)
< 0)
+ if (virJSONValueObjectAppend(object, "defaultLockspace",
&defaultLockspace) < 0)
return -1;
- defaultLockspace = NULL;
tmp = pairs = virHashGetItems(lockDaemon->lockspaces, NULL, false);
while (tmp && tmp->key) {
@@ -737,9 +735,8 @@ virLockDaemonPreExecRestart(const char *state_file,
tmp++;
}
- if (virJSONValueObjectAppend(object, "lockspaces", lockspaces) < 0)
+ if (virJSONValueObjectAppend(object, "lockspaces", &lockspaces) <
0)
return -1;
- lockspaces = NULL;
if (!(magic = virLockDaemonGetExecRestartMagic()))
return -1;
diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c
index ceffa6a368..cc0b96ea05 100644
--- a/src/logging/log_daemon.c
+++ b/src/logging/log_daemon.c
@@ -516,9 +516,8 @@ virLogDaemonPreExecRestart(const char *state_file,
if (!(daemon = virNetDaemonPreExecRestart(dmn)))
return -1;
- if (virJSONValueObjectAppend(object, "daemon", daemon) < 0)
+ if (virJSONValueObjectAppend(object, "daemon", &daemon) < 0)
return -1;
- daemon = NULL;
if (!(magic = virLogDaemonGetExecRestartMagic()))
return -1;
@@ -529,9 +528,8 @@ virLogDaemonPreExecRestart(const char *state_file,
if (!(handler = virLogHandlerPreExecRestart(logDaemon->handler)))
return -1;
- if (virJSONValueObjectAppend(object, "handler", handler) < 0)
+ if (virJSONValueObjectAppend(object, "handler", &handler) < 0)
return -1;
- handler = NULL;
if (!(state = virJSONValueToString(object, true)))
return -1;
diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c
index cacf9584cd..d649c4d132 100644
--- a/src/logging/log_handler.c
+++ b/src/logging/log_handler.c
@@ -647,9 +647,8 @@ virLogHandlerPreExecRestart(virLogHandlerPtr handler)
file = NULL;
}
- if (virJSONValueObjectAppend(ret, "files", files) < 0)
+ if (virJSONValueObjectAppend(ret, "files", &files) < 0)
return NULL;
- files = NULL;
return g_steal_pointer(&ret);
}
diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index 51b20848ce..d946e64ad6 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -610,7 +610,7 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDefPtr def, char **buf)
return -1;
}
- if (virJSONValueObjectAppend(json, "attrs",
g_steal_pointer(&attributes)) < 0)
+ if (virJSONValueObjectAppend(json, "attrs", &attributes) < 0)
return -1;
}
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 3d88e701b2..1df51ba97b 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -1097,9 +1097,8 @@ qemuBlockStorageSourceGetBlockdevGetCacheProps(virStorageSourcePtr
src,
NULL) < 0)
return -1;
- if (virJSONValueObjectAppend(props, "cache", cacheobj) < 0)
+ if (virJSONValueObjectAppend(props, "cache", &cacheobj) < 0)
return -1;
- cacheobj = NULL;
return 0;
}
diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c
index c571fdecf7..5e8fdd0ff1 100644
--- a/src/qemu/qemu_firmware.c
+++ b/src/qemu/qemu_firmware.c
@@ -662,10 +662,9 @@ qemuFirmwareInterfaceFormat(virJSONValuePtr doc,
if (virJSONValueObjectAppend(doc,
"interface-types",
- interfacesJSON) < 0)
+ &interfacesJSON) < 0)
return -1;
- interfacesJSON = NULL;
return 0;
}
@@ -706,18 +705,15 @@ qemuFirmwareMappingFlashFormat(virJSONValuePtr mapping,
if (virJSONValueObjectAppend(mapping,
"executable",
- executable) < 0)
+ &executable) < 0)
return -1;
- executable = NULL;
if (virJSONValueObjectAppend(mapping,
"nvram-template",
- nvram_template) < 0)
+ &nvram_template) < 0)
return -1;
- nvram_template = NULL;
-
return 0;
}
@@ -778,10 +774,9 @@ qemuFirmwareMappingFormat(virJSONValuePtr doc,
break;
}
- if (virJSONValueObjectAppend(doc, "mapping", mapping) < 0)
+ if (virJSONValueObjectAppend(doc, "mapping", &mapping) < 0)
return -1;
- mapping = NULL;
return 0;
}
@@ -814,21 +809,18 @@ qemuFirmwareTargetFormat(virJSONValuePtr doc,
return -1;
}
- if (virJSONValueObjectAppend(target, "machines", machines) < 0)
+ if (virJSONValueObjectAppend(target, "machines", &machines) <
0)
return -1;
- machines = NULL;
-
if (virJSONValueArrayAppend(targetsJSON, target) < 0)
return -1;
target = NULL;
}
- if (virJSONValueObjectAppend(doc, "targets", targetsJSON) < 0)
+ if (virJSONValueObjectAppend(doc, "targets", &targetsJSON) < 0)
return -1;
- targetsJSON = NULL;
return 0;
}
@@ -850,10 +842,9 @@ qemuFirmwareFeatureFormat(virJSONValuePtr doc,
if (virJSONValueObjectAppend(doc,
"features",
- featuresJSON) < 0)
+ &featuresJSON) < 0)
return -1;
- featuresJSON = NULL;
return 0;
}
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index f8c78d9093..924e03b4da 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4554,9 +4554,8 @@ qemuMonitorJSONAddDeviceArgs(qemuMonitorPtr mon,
if (!(cmd = qemuMonitorJSONMakeCommand("device_add", NULL)))
goto cleanup;
- if (virJSONValueObjectAppend(cmd, "arguments", args) < 0)
+ if (virJSONValueObjectAppend(cmd, "arguments", &args) < 0)
goto cleanup;
- args = NULL; /* obj owns reference to args now */
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
@@ -5572,9 +5571,8 @@ int qemuMonitorJSONSetBlockIoThrottle(qemuMonitorPtr mon,
NULL) < 0)
goto cleanup;
- if (virJSONValueObjectAppend(cmd, "arguments", args) < 0)
+ if (virJSONValueObjectAppend(cmd, "arguments", &args) < 0)
goto cleanup;
- args = NULL; /* obj owns reference to args now */
if (qemuMonitorJSONCommand(mon, cmd, &result) < 0)
goto cleanup;
@@ -5989,7 +5987,7 @@ qemuMonitorJSONMakeCPUModel(virCPUDefPtr cpu,
goto error;
}
- if (virJSONValueObjectAppend(model, "props", props) < 0)
+ if (virJSONValueObjectAppend(model, "props", &props) < 0)
goto error;
}
@@ -7470,9 +7468,8 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
addr = qemuMonitorJSONBuildInetSocketAddress(chr->data.tcp.host,
chr->data.tcp.service);
if (!addr ||
- virJSONValueObjectAppend(data, "addr", addr) < 0)
+ virJSONValueObjectAppend(data, "addr", &addr) < 0)
goto cleanup;
- addr = NULL;
telnet = chr->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
@@ -7503,7 +7500,7 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
addr = qemuMonitorJSONBuildInetSocketAddress(host,
chr->data.udp.connectService);
if (!addr ||
- virJSONValueObjectAppend(data, "remote", addr) < 0)
+ virJSONValueObjectAppend(data, "remote", &addr) < 0)
goto cleanup;
host = chr->data.udp.bindHost;
@@ -7515,10 +7512,9 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
port = "";
addr = qemuMonitorJSONBuildInetSocketAddress(host, port);
if (!addr ||
- virJSONValueObjectAppend(data, "local", addr) < 0)
+ virJSONValueObjectAppend(data, "local", &addr) < 0)
goto cleanup;
}
- addr = NULL;
break;
case VIR_DOMAIN_CHR_TYPE_UNIX:
@@ -7526,9 +7522,8 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
addr = qemuMonitorJSONBuildUnixSocketAddress(chr->data.nix.path);
if (!addr ||
- virJSONValueObjectAppend(data, "addr", addr) < 0)
+ virJSONValueObjectAppend(data, "addr", &addr) < 0)
goto cleanup;
- addr = NULL;
if (chr->data.nix.listen &&
virJSONValueObjectAppendBoolean(data, "wait", false) < 0)
@@ -7574,9 +7569,8 @@ qemuMonitorJSONAttachCharDevCommand(const char *chrID,
goto cleanup;
if (virJSONValueObjectAppendString(backend, "type", backend_type) < 0
||
- virJSONValueObjectAppend(backend, "data", data) < 0)
+ virJSONValueObjectAppend(backend, "data", &data) < 0)
goto cleanup;
- data = NULL;
if (!(ret = qemuMonitorJSONMakeCommand("chardev-add",
"s:id", chrID,
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 6132615093..327540c4b4 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -404,16 +404,14 @@ virNetDaemonPreExecRestart(virNetDaemonPtr dmn)
if (!srvJSON)
goto error;
- if (virJSONValueObjectAppend(srvObj, srvArray[i].key, srvJSON) < 0)
+ if (virJSONValueObjectAppend(srvObj, srvArray[i].key, &srvJSON) < 0)
goto error;
- srvJSON = NULL;
}
virObjectUnlock(dmn);
- if (virJSONValueObjectAppend(object, "servers", srvObj) < 0)
+ if (virJSONValueObjectAppend(object, "servers", &srvObj) < 0)
return NULL;
- srvObj = NULL;
return g_steal_pointer(&object);
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index ee402e8ea0..c2650ade09 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -590,9 +590,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
child = NULL;
}
- if (virJSONValueObjectAppend(object, "services", services) < 0)
+ if (virJSONValueObjectAppend(object, "services", &services) < 0)
goto error;
- services = NULL;
for (i = 0; i < srv->nclients; i++) {
g_autoptr(virJSONValue) child = NULL;
@@ -604,9 +603,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
child = NULL;
}
- if (virJSONValueObjectAppend(object, "clients", clients) < 0)
+ if (virJSONValueObjectAppend(object, "clients", &clients) < 0)
goto error;
- clients = NULL;
virObjectUnlock(srv);
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 0789ad9154..2bb3443287 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -610,16 +610,14 @@ virJSONValuePtr
virNetServerClientPreExecRestart(virNetServerClientPtr client)
if (!(sock = virNetSocketPreExecRestart(client->sock)))
goto error;
- if (virJSONValueObjectAppend(object, "sock", sock) < 0)
+ if (virJSONValueObjectAppend(object, "sock", &sock) < 0)
goto error;
- sock = NULL;
if (!(priv = client->privateDataPreExecRestart(client, client->privateData)))
goto error;
- if (virJSONValueObjectAppend(object, "privateData", priv) < 0)
+ if (virJSONValueObjectAppend(object, "privateData", &priv) < 0)
goto error;
- priv = NULL;
virObjectUnlock(client);
return g_steal_pointer(&object);
diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c
index a72277226a..fece6305e8 100644
--- a/src/rpc/virnetserverservice.c
+++ b/src/rpc/virnetserverservice.c
@@ -357,9 +357,8 @@ virJSONValuePtr
virNetServerServicePreExecRestart(virNetServerServicePtr svc)
child = NULL;
}
- if (virJSONValueObjectAppend(object, "socks", socks) < 0)
+ if (virJSONValueObjectAppend(object, "socks", &socks) < 0)
return NULL;
- socks = NULL;
return g_steal_pointer(&object);
}
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 7b52525797..5b3aa49a7f 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -298,7 +298,7 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
return -1;
}
- if ((rc = virJSONValueObjectAppend(obj, key, *val)) == 0)
+ if ((rc = virJSONValueObjectAppend(obj, key, val)) == 0)
*val = NULL;
} break;
@@ -320,9 +320,8 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
if (!(jsonMap = virJSONValueNewArrayFromBitmap(map)))
return -1;
- if ((rc = virJSONValueObjectAppend(obj, key, jsonMap)) < 0)
+ if ((rc = virJSONValueObjectAppend(obj, key, &jsonMap)) < 0)
return -1;
- jsonMap = NULL;
} break;
default:
@@ -616,9 +615,9 @@ virJSONValueObjectInsert(virJSONValuePtr object,
int
virJSONValueObjectAppend(virJSONValuePtr object,
const char *key,
- virJSONValuePtr value)
+ virJSONValuePtr *value)
{
- return virJSONValueObjectInsert(object, key, &value, false);
+ return virJSONValueObjectInsert(object, key, value, false);
}
@@ -679,9 +678,8 @@ virJSONValueObjectAppendNumberInt(virJSONValuePtr object,
{
g_autoptr(virJSONValue) jvalue = virJSONValueNewNumberInt(number);
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -694,9 +692,8 @@ virJSONValueObjectAppendNumberUint(virJSONValuePtr object,
{
g_autoptr(virJSONValue) jvalue = virJSONValueNewNumberUint(number);
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -709,9 +706,8 @@ virJSONValueObjectAppendNumberLong(virJSONValuePtr object,
{
g_autoptr(virJSONValue) jvalue = virJSONValueNewNumberLong(number);
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -724,9 +720,8 @@ virJSONValueObjectAppendNumberUlong(virJSONValuePtr object,
{
g_autoptr(virJSONValue) jvalue = virJSONValueNewNumberUlong(number);
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -743,9 +738,8 @@ virJSONValueObjectAppendNumberDouble(virJSONValuePtr object,
if (!jvalue)
return -1;
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -758,9 +752,8 @@ virJSONValueObjectAppendBoolean(virJSONValuePtr object,
{
g_autoptr(virJSONValue) jvalue = virJSONValueNewBoolean(boolean_);
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -772,9 +765,8 @@ virJSONValueObjectAppendNull(virJSONValuePtr object,
{
g_autoptr(virJSONValue) jvalue = virJSONValueNewNull();
- if (virJSONValueObjectAppend(object, key, jvalue) < 0)
+ if (virJSONValueObjectAppend(object, key, &jvalue) < 0)
return -1;
- jvalue = NULL;
return 0;
}
@@ -1583,7 +1575,7 @@ virJSONParserInsertValue(virJSONParserPtr parser,
if (virJSONValueObjectAppend(state->value,
state->key,
- value) < 0)
+ &value) < 0)
return -1;
VIR_FREE(state->key);
@@ -2093,7 +2085,7 @@ virJSONValueObjectDeflattenWorker(const char *key,
return -1;
}
- if (virJSONValueObjectAppend(retobj, key, newval) < 0)
+ if (virJSONValueObjectAppend(retobj, key, &newval) < 0)
return -1;
newval = NULL;
@@ -2111,9 +2103,10 @@ virJSONValueObjectDeflattenWorker(const char *key,
}
if (!(existobj = virJSONValueObjectGet(retobj, tokens[0]))) {
- existobj = virJSONValueNewObject();
+ virJSONValuePtr newobj = virJSONValueNewObject();
+ existobj = newobj;
- if (virJSONValueObjectAppend(retobj, tokens[0], existobj) < 0)
+ if (virJSONValueObjectAppend(retobj, tokens[0], &newobj) < 0)
return -1;
} else {
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 4dc7ed1a2d..ca99ae3b70 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -69,7 +69,9 @@ virJSONValuePtr virJSONValueNewArray(void);
virJSONValuePtr virJSONValueNewObject(void);
virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
-int virJSONValueObjectAppend(virJSONValuePtr object, const char *key, virJSONValuePtr
value);
+int virJSONValueObjectAppend(virJSONValuePtr object,
+ const char *key,
+ virJSONValuePtr *value);
int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
int virJSONValueArrayConcat(virJSONValuePtr a,
virJSONValuePtr c);
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 1b6b51b649..9ba6e83251 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -460,9 +460,8 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
owner = NULL;
}
- if (virJSONValueObjectAppend(child, "owners", owners) < 0)
+ if (virJSONValueObjectAppend(child, "owners", &owners) < 0)
goto error;
- owners = NULL;
if (virJSONValueArrayAppend(resources, child) < 0)
goto error;
@@ -471,9 +470,8 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
tmp++;
}
- if (virJSONValueObjectAppend(object, "resources", resources) < 0)
+ if (virJSONValueObjectAppend(object, "resources", &resources) < 0)
goto error;
- resources = NULL;
virMutexUnlock(&lockspace->lock);
return object;
diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c
index 0d458d7b7b..521ab89b5b 100644
--- a/src/util/virmacmap.c
+++ b/src/util/virmacmap.c
@@ -224,9 +224,8 @@ virMACMapHashDumper(void *payload,
}
if (virJSONValueObjectAppendString(obj, "domain", name) < 0 ||
- virJSONValueObjectAppend(obj, "macs", arr) < 0)
+ virJSONValueObjectAppend(obj, "macs", &arr) < 0)
return -1;
- arr = NULL;
if (virJSONValueArrayAppend(data, obj) < 0)
return -1;
--
2.29.2