From bd74e0d995578ab301eb8283af07ce07df75ed4b Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 9 Nov 2021 16:28:23 +0100 Subject: [PATCH] virJSONValueObjectAddVArgs: Allocate new object if passed pointer is NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- 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 = 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); }