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@xxxxxxxxxx> --- src/util/virjson.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index bd71b84960..8b3599e94f 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -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(); + *obj = NULL; - /* 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); } -- 2.31.1