virJSONValueArrayAppend: Clear pointer when taking ownership of passed value

The parent array 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 962d80e751
commit 6431b20c3e
19 changed files with 35 additions and 73 deletions

View File

@ -728,9 +728,8 @@ virLockDaemonPreExecRestart(const char *state_file,
if (!(child = virLockSpacePreExecRestart(lockspace))) if (!(child = virLockSpacePreExecRestart(lockspace)))
return -1; return -1;
if (virJSONValueArrayAppend(lockspaces, child) < 0) if (virJSONValueArrayAppend(lockspaces, &child) < 0)
return -1; return -1;
child = NULL;
tmp++; tmp++;
} }

View File

@ -642,9 +642,8 @@ virLogHandlerPreExecRestart(virLogHandlerPtr handler)
return NULL; return NULL;
} }
if (virJSONValueArrayAppend(files, file) < 0) if (virJSONValueArrayAppend(files, &file) < 0)
return NULL; return NULL;
file = NULL;
} }
if (virJSONValueObjectAppend(ret, "files", &files) < 0) if (virJSONValueObjectAppend(ret, "files", &files) < 0)

View File

@ -226,12 +226,11 @@ main(int argc, char **argv)
case VIR_LEASE_ACTION_OLD: case VIR_LEASE_ACTION_OLD:
case VIR_LEASE_ACTION_ADD: case VIR_LEASE_ACTION_ADD:
if (lease_new && virJSONValueArrayAppend(leases_array_new, lease_new) < 0) { if (lease_new && virJSONValueArrayAppend(leases_array_new, &lease_new) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to create json")); _("failed to create json"));
goto cleanup; goto cleanup;
} }
lease_new = NULL;
G_GNUC_FALLTHROUGH; G_GNUC_FALLTHROUGH;
case VIR_LEASE_ACTION_DEL: case VIR_LEASE_ACTION_DEL:

View File

@ -606,7 +606,7 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDefPtr def, char **buf)
if (virJSONValueObjectAppendString(jsonattr, attr->name, attr->value) < 0) if (virJSONValueObjectAppendString(jsonattr, attr->name, attr->value) < 0)
return -1; return -1;
if (virJSONValueArrayAppend(attributes, g_steal_pointer(&jsonattr)) < 0) if (virJSONValueArrayAppend(attributes, &jsonattr) < 0)
return -1; return -1;
} }

View File

@ -1180,9 +1180,8 @@ qemuAgentMakeStringsArray(const char **strings, unsigned int len)
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
g_autoptr(virJSONValue) str = virJSONValueNewString(strings[i]); g_autoptr(virJSONValue) str = virJSONValueNewString(strings[i]);
if (virJSONValueArrayAppend(ret, str) < 0) if (virJSONValueArrayAppend(ret, &str) < 0)
return NULL; return NULL;
str = NULL;
} }
return g_steal_pointer(&ret); return g_steal_pointer(&ret);
@ -1514,10 +1513,8 @@ qemuAgentSetVCPUsCommand(qemuAgentPtr agent,
if (virJSONValueObjectAppendBoolean(cpu, "online", in->online) < 0) if (virJSONValueObjectAppendBoolean(cpu, "online", in->online) < 0)
goto cleanup; goto cleanup;
if (virJSONValueArrayAppend(cpus, cpu) < 0) if (virJSONValueArrayAppend(cpus, &cpu) < 0)
goto cleanup; goto cleanup;
cpu = NULL;
} }
if (*nmodified == 0) { if (*nmodified == 0) {

View File

@ -529,10 +529,8 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
if (!(server = qemuBlockStorageSourceBuildJSONSocketAddress(host, legacy))) if (!(server = qemuBlockStorageSourceBuildJSONSocketAddress(host, legacy)))
return NULL; return NULL;
if (virJSONValueArrayAppend(servers, server) < 0) if (virJSONValueArrayAppend(servers, &server) < 0)
return NULL; return NULL;
server = NULL;
} }
return g_steal_pointer(&servers); return g_steal_pointer(&servers);
@ -617,10 +615,8 @@ qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(virStorageSourcePtr src)
if (!(server = qemuBlockStorageSourceBuildJSONInetSocketAddress(host))) if (!(server = qemuBlockStorageSourceBuildJSONInetSocketAddress(host)))
return NULL; return NULL;
if (virJSONValueArrayAppend(servers, server) < 0) if (virJSONValueArrayAppend(servers, &server) < 0)
return NULL; return NULL;
server = NULL;
} }
return g_steal_pointer(&servers); return g_steal_pointer(&servers);
@ -907,16 +903,12 @@ qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src,
authmodes = virJSONValueNewArray(); authmodes = virJSONValueNewArray();
if (!(mode = virJSONValueNewString("cephx")) || if (!(mode = virJSONValueNewString("cephx")) ||
virJSONValueArrayAppend(authmodes, mode) < 0) virJSONValueArrayAppend(authmodes, &mode) < 0)
return NULL; return NULL;
mode = NULL;
if (!(mode = virJSONValueNewString("none")) || if (!(mode = virJSONValueNewString("none")) ||
virJSONValueArrayAppend(authmodes, mode) < 0) virJSONValueArrayAppend(authmodes, &mode) < 0)
return NULL; return NULL;
mode = NULL;
} }
if (virJSONValueObjectCreate(&ret, if (virJSONValueObjectCreate(&ret,

View File

@ -10218,9 +10218,8 @@ qemuBuildChannelGuestfwdNetdevProps(virDomainChrDefPtr chr)
chr->info.alias) < 0) chr->info.alias) < 0)
return NULL; return NULL;
if (virJSONValueArrayAppend(guestfwdarr, guestfwdstrobj) < 0) if (virJSONValueArrayAppend(guestfwdarr, &guestfwdstrobj) < 0)
return NULL; return NULL;
guestfwdstrobj = NULL;
if (virJSONValueObjectCreate(&ret, if (virJSONValueObjectCreate(&ret,
"s:type", "user", "s:type", "user",

View File

@ -812,10 +812,8 @@ qemuFirmwareTargetFormat(virJSONValuePtr doc,
if (virJSONValueObjectAppend(target, "machines", &machines) < 0) if (virJSONValueObjectAppend(target, "machines", &machines) < 0)
return -1; return -1;
if (virJSONValueArrayAppend(targetsJSON, target) < 0) if (virJSONValueArrayAppend(targetsJSON, &target) < 0)
return -1; return -1;
target = NULL;
} }
if (virJSONValueObjectAppend(doc, "targets", &targetsJSON) < 0) if (virJSONValueObjectAppend(doc, "targets", &targetsJSON) < 0)

View File

@ -1665,10 +1665,9 @@ qemuMigrationCookieBlockDirtyBitmapsToParams(GSList *disks,
NULL) < 0) NULL) < 0)
return -1; return -1;
if (virJSONValueArrayAppend(jsonbitmaps, jsonbitmap) < 0) if (virJSONValueArrayAppend(jsonbitmaps, &jsonbitmap) < 0)
return -1; return -1;
jsonbitmap = NULL;
hasBitmaps = true; hasBitmaps = true;
} }
@ -1682,10 +1681,9 @@ qemuMigrationCookieBlockDirtyBitmapsToParams(GSList *disks,
NULL) < 0) NULL) < 0)
return -1; return -1;
if (virJSONValueArrayAppend(map, jsondisk) < 0) if (virJSONValueArrayAppend(map, &jsondisk) < 0)
return -1; return -1;
jsondisk = NULL;
hasDisks = true; hasDisks = true;
} }

View File

@ -797,10 +797,8 @@ qemuMigrationCapsToJSON(virBitmapPtr caps,
NULL) < 0) NULL) < 0)
return NULL; return NULL;
if (virJSONValueArrayAppend(json, cap) < 0) if (virJSONValueArrayAppend(json, &cap) < 0)
return NULL; return NULL;
cap = NULL;
} }
return g_steal_pointer(&json); return g_steal_pointer(&json);

View File

@ -525,10 +525,9 @@ qemuMonitorJSONTransactionAdd(virJSONValuePtr actions,
"A:data", &data, NULL) < 0) "A:data", &data, NULL) < 0)
goto cleanup; goto cleanup;
if (virJSONValueArrayAppend(actions, entry) < 0) if (virJSONValueArrayAppend(actions, &entry) < 0)
goto cleanup; goto cleanup;
entry = NULL;
ret = 0; ret = 0;
cleanup: cleanup:
@ -4987,11 +4986,8 @@ int qemuMonitorJSONSendKey(qemuMonitorPtr mon,
if (virJSONValueObjectAppendNumberInt(key, "data", keycodes[i]) < 0) if (virJSONValueObjectAppendNumberInt(key, "data", keycodes[i]) < 0)
goto cleanup; goto cleanup;
if (virJSONValueArrayAppend(keys, key) < 0) if (virJSONValueArrayAppend(keys, &key) < 0)
goto cleanup; goto cleanup;
key = NULL;
} }
cmd = qemuMonitorJSONMakeCommand("send-key", cmd = qemuMonitorJSONMakeCommand("send-key",
@ -9324,10 +9320,9 @@ qemuMonitorJSONTransactionBitmapMergeSourceAddBitmap(virJSONValuePtr sources,
NULL) < 0) NULL) < 0)
return -1; return -1;
if (virJSONValueArrayAppend(sources, sourceobj) < 0) if (virJSONValueArrayAppend(sources, &sourceobj) < 0)
return -1; return -1;
sourceobj = NULL;
return 0; return 0;
} }

View File

@ -585,9 +585,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
if (!(child = virNetServerServicePreExecRestart(srv->services[i]))) if (!(child = virNetServerServicePreExecRestart(srv->services[i])))
goto error; goto error;
if (virJSONValueArrayAppend(services, child) < 0) if (virJSONValueArrayAppend(services, &child) < 0)
goto error; goto error;
child = NULL;
} }
if (virJSONValueObjectAppend(object, "services", &services) < 0) if (virJSONValueObjectAppend(object, "services", &services) < 0)
@ -598,9 +597,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
if (!(child = virNetServerClientPreExecRestart(srv->clients[i]))) if (!(child = virNetServerClientPreExecRestart(srv->clients[i])))
goto error; goto error;
if (virJSONValueArrayAppend(clients, child) < 0) if (virJSONValueArrayAppend(clients, &child) < 0)
goto error; goto error;
child = NULL;
} }
if (virJSONValueObjectAppend(object, "clients", &clients) < 0) if (virJSONValueObjectAppend(object, "clients", &clients) < 0)

View File

@ -352,9 +352,8 @@ virJSONValuePtr virNetServerServicePreExecRestart(virNetServerServicePtr svc)
if (!(child = virNetSocketPreExecRestart(svc->socks[i]))) if (!(child = virNetSocketPreExecRestart(svc->socks[i])))
return NULL; return NULL;
if (virJSONValueArrayAppend(socks, child) < 0) if (virJSONValueArrayAppend(socks, &child) < 0)
return NULL; return NULL;
child = NULL;
} }
if (virJSONValueObjectAppend(object, "socks", &socks) < 0) if (virJSONValueObjectAppend(object, "socks", &socks) < 0)

View File

@ -298,8 +298,7 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
return -1; return -1;
} }
if ((rc = virJSONValueObjectAppend(obj, key, val)) == 0) rc = virJSONValueObjectAppend(obj, key, val);
*val = NULL;
} break; } break;
case 'M': case 'M':
@ -774,7 +773,7 @@ virJSONValueObjectAppendNull(virJSONValuePtr object,
int int
virJSONValueArrayAppend(virJSONValuePtr array, virJSONValueArrayAppend(virJSONValuePtr array,
virJSONValuePtr value) virJSONValuePtr *value)
{ {
if (array->type != VIR_JSON_TYPE_ARRAY) { if (array->type != VIR_JSON_TYPE_ARRAY) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array")); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array"));
@ -785,7 +784,7 @@ virJSONValueArrayAppend(virJSONValuePtr array,
array->data.array.nvalues + 1) < 0) array->data.array.nvalues + 1) < 0)
return -1; return -1;
array->data.array.values[array->data.array.nvalues] = value; array->data.array.values[array->data.array.nvalues] = g_steal_pointer(value);
array->data.array.nvalues++; array->data.array.nvalues++;
return 0; return 0;
@ -798,9 +797,8 @@ virJSONValueArrayAppendString(virJSONValuePtr object,
{ {
g_autoptr(virJSONValue) jvalue = virJSONValueNewString(value); g_autoptr(virJSONValue) jvalue = virJSONValueNewString(value);
if (virJSONValueArrayAppend(object, jvalue) < 0) if (virJSONValueArrayAppend(object, &jvalue) < 0)
return -1; return -1;
jvalue = NULL;
return 0; return 0;
} }
@ -1243,9 +1241,8 @@ virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap)
while ((pos = virBitmapNextSetBit(bitmap, pos)) > -1) { while ((pos = virBitmapNextSetBit(bitmap, pos)) > -1) {
g_autoptr(virJSONValue) newelem = virJSONValueNewNumberLong(pos); g_autoptr(virJSONValue) newelem = virJSONValueNewNumberLong(pos);
if (virJSONValueArrayAppend(ret, newelem) < 0) if (virJSONValueArrayAppend(ret, &newelem) < 0)
return NULL; return NULL;
newelem = NULL;
} }
return g_steal_pointer(&ret); return g_steal_pointer(&ret);
@ -1588,7 +1585,7 @@ virJSONParserInsertValue(virJSONParserPtr parser,
} }
if (virJSONValueArrayAppend(state->value, if (virJSONValueArrayAppend(state->value,
value) < 0) &value) < 0)
return -1; return -1;
} break; } break;
@ -2088,8 +2085,6 @@ virJSONValueObjectDeflattenWorker(const char *key,
if (virJSONValueObjectAppend(retobj, key, &newval) < 0) if (virJSONValueObjectAppend(retobj, key, &newval) < 0)
return -1; return -1;
newval = NULL;
return 0; return 0;
} }

View File

@ -72,7 +72,8 @@ virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
int virJSONValueObjectAppend(virJSONValuePtr object, int virJSONValueObjectAppend(virJSONValuePtr object,
const char *key, const char *key,
virJSONValuePtr *value); virJSONValuePtr *value);
int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value); int virJSONValueArrayAppend(virJSONValuePtr object,
virJSONValuePtr *value);
int virJSONValueArrayConcat(virJSONValuePtr a, int virJSONValueArrayConcat(virJSONValuePtr a,
virJSONValuePtr c); virJSONValuePtr c);

View File

@ -119,7 +119,7 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_array_new,
} }
/* Move old lease to new array */ /* Move old lease to new array */
if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) { if (virJSONValueArrayAppend(leases_array_new, &lease_tmp) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to create json")); _("failed to create json"));
return -1; return -1;

View File

@ -455,17 +455,15 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
if (!owner) if (!owner)
goto error; goto error;
if (virJSONValueArrayAppend(owners, owner) < 0) if (virJSONValueArrayAppend(owners, &owner) < 0)
goto error; goto error;
owner = NULL;
} }
if (virJSONValueObjectAppend(child, "owners", &owners) < 0) if (virJSONValueObjectAppend(child, "owners", &owners) < 0)
goto error; goto error;
if (virJSONValueArrayAppend(resources, child) < 0) if (virJSONValueArrayAppend(resources, &child) < 0)
goto error; goto error;
child = NULL;
tmp++; tmp++;
} }

View File

@ -216,18 +216,16 @@ virMACMapHashDumper(void *payload,
for (next = macs; next; next = next->next) { for (next = macs; next; next = next->next) {
g_autoptr(virJSONValue) m = virJSONValueNewString((const char *) next->data); g_autoptr(virJSONValue) m = virJSONValueNewString((const char *) next->data);
if (virJSONValueArrayAppend(arr, m) < 0) if (virJSONValueArrayAppend(arr, &m) < 0)
return -1; return -1;
m = NULL;
} }
if (virJSONValueObjectAppendString(obj, "domain", name) < 0 || if (virJSONValueObjectAppendString(obj, "domain", name) < 0 ||
virJSONValueObjectAppend(obj, "macs", &arr) < 0) virJSONValueObjectAppend(obj, "macs", &arr) < 0)
return -1; return -1;
if (virJSONValueArrayAppend(data, obj) < 0) if (virJSONValueArrayAppend(data, &obj) < 0)
return -1; return -1;
obj = NULL;
return 0; return 0;
} }

View File

@ -168,9 +168,8 @@ testQEMUSchemaValidateObjectMergeVariantMember(size_t pos G_GNUC_UNUSED,
if (!(copy = virJSONValueCopy(item))) if (!(copy = virJSONValueCopy(item)))
return -1; return -1;
if (virJSONValueArrayAppend(array, copy) < 0) if (virJSONValueArrayAppend(array, &copy) < 0)
return -1; return -1;
copy = NULL;
return 1; return 1;
} }