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:
Peter Krempa 2021-11-09 16:28:23 +01:00
parent 34fc5832e3
commit bd74e0d995

View File

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