mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-22 12:35:17 +00:00
util: json: Convert virJSONValueNewObject() to g_new0
Make it obvious that the function always returns a valid pointer and fix all callers. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
parent
084c04b5bb
commit
e9153cc604
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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";
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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",
|
||||
|
@ -593,9 +593,6 @@ virJSONValuePtr virNetServerClientPreExecRestart(virNetServerClientPtr client)
|
||||
virJSONValuePtr object = virJSONValueNewObject();
|
||||
virJSONValuePtr child;
|
||||
|
||||
if (!object)
|
||||
return NULL;
|
||||
|
||||
virObjectLock(client);
|
||||
|
||||
if (virJSONValueObjectAppendNumberUlong(object, "id",
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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++) {
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user