mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-24 21:45:21 +00:00
virJSONValueObjectAddVArgs: Allocate new object if passed pointer is NULL
Until now the code would crash if virJSONValueObjectAdd is used without a valid object. Adding the functionality of allocating it if it's NULL will allow us to replace all uses of virJSONValueObjectCreate with this single function. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
34fc5832e3
commit
bd74e0d995
@ -155,11 +155,15 @@ int
|
|||||||
virJSONValueObjectAddVArgs(virJSONValue **objptr,
|
virJSONValueObjectAddVArgs(virJSONValue **objptr,
|
||||||
va_list args)
|
va_list args)
|
||||||
{
|
{
|
||||||
|
g_autoptr(virJSONValue) newobj = NULL;
|
||||||
virJSONValue *obj = *objptr;
|
virJSONValue *obj = *objptr;
|
||||||
char type;
|
char type;
|
||||||
char *key;
|
char *key;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
if (obj == NULL)
|
||||||
|
newobj = obj = virJSONValueNewObject();
|
||||||
|
|
||||||
while ((key = va_arg(args, char *)) != NULL) {
|
while ((key = va_arg(args, char *)) != NULL) {
|
||||||
|
|
||||||
if (strlen(key) < 3 || key[1] != ':') {
|
if (strlen(key) < 3 || key[1] != ':') {
|
||||||
@ -344,6 +348,9 @@ virJSONValueObjectAddVArgs(virJSONValue **objptr,
|
|||||||
if (virJSONValueObjectKeysNumber(obj) == 0)
|
if (virJSONValueObjectKeysNumber(obj) == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (newobj)
|
||||||
|
*objptr = g_steal_pointer(&newobj);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -366,17 +373,9 @@ int
|
|||||||
virJSONValueObjectCreateVArgs(virJSONValue **obj,
|
virJSONValueObjectCreateVArgs(virJSONValue **obj,
|
||||||
va_list args)
|
va_list args)
|
||||||
{
|
{
|
||||||
int ret;
|
|
||||||
|
|
||||||
*obj = virJSONValueNewObject();
|
|
||||||
|
|
||||||
/* free the object on error, or if no value objects were added */
|
|
||||||
if ((ret = virJSONValueObjectAddVArgs(obj, args)) <= 0) {
|
|
||||||
virJSONValueFree(*obj);
|
|
||||||
*obj = NULL;
|
*obj = NULL;
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return virJSONValueObjectAddVArgs(obj, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user