virJSONValueObjectAppend: Clear pointer when taking ownership of passed value

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@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-11 17:57:45 +01:00
parent e4b26c48cb
commit 5fc3892891
16 changed files with 53 additions and 91 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -1092,9 +1092,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;
}

View File

@ -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;
}

View File

@ -770,9 +770,8 @@ qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
if (!mapping)
return NULL;
if (virJSONValueObjectAppend(params, "block-bitmap-mapping", mapping) < 0)
if (virJSONValueObjectAppend(params, "block-bitmap-mapping", &mapping) < 0)
return NULL;
mapping = NULL;
}
return g_steal_pointer(&params);

View File

@ -4565,9 +4565,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;
@ -5583,9 +5582,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;
@ -6000,7 +5998,7 @@ qemuMonitorJSONMakeCPUModel(virCPUDefPtr cpu,
goto error;
}
if (virJSONValueObjectAppend(model, "props", props) < 0)
if (virJSONValueObjectAppend(model, "props", &props) < 0)
goto error;
}
@ -7481,9 +7479,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;
@ -7514,7 +7511,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;
@ -7526,10 +7523,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:
@ -7537,9 +7533,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)
@ -7585,9 +7580,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,

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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 {

View File

@ -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);

View File

@ -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;

View File

@ -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;