mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-09 06:25:19 +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,
|
||||
va_list args)
|
||||
{
|
||||
g_autoptr(virJSONValue) newobj = NULL;
|
||||
virJSONValue *obj = *objptr;
|
||||
char type;
|
||||
char *key;
|
||||
int rc;
|
||||
|
||||
if (obj == NULL)
|
||||
newobj = obj = virJSONValueNewObject();
|
||||
|
||||
while ((key = va_arg(args, char *)) != NULL) {
|
||||
|
||||
if (strlen(key) < 3 || key[1] != ':') {
|
||||
@ -344,6 +348,9 @@ virJSONValueObjectAddVArgs(virJSONValue **objptr,
|
||||
if (virJSONValueObjectKeysNumber(obj) == 0)
|
||||
return 0;
|
||||
|
||||
if (newobj)
|
||||
*objptr = g_steal_pointer(&newobj);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -366,17 +373,9 @@ int
|
||||
virJSONValueObjectCreateVArgs(virJSONValue **obj,
|
||||
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;
|
||||
}
|
||||
|
||||
return ret;
|
||||
return virJSONValueObjectAddVArgs(obj, args);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user