By making use of the GCC's __attribute__((cleanup)) handled by VIR_AUTOFREE macro, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. --- src/util/virjson.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 92f3994..1391a3b 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -497,11 +497,10 @@ virJSONValuePtr virJSONValueNewNumberInt(int data) { virJSONValuePtr val = NULL; - char *str; + VIR_AUTOFREE(char *) str = NULL; if (virAsprintf(&str, "%i", data) < 0) return NULL; val = virJSONValueNewNumber(str); - VIR_FREE(str); return val; } @@ -510,11 +509,10 @@ virJSONValuePtr virJSONValueNewNumberUint(unsigned int data) { virJSONValuePtr val = NULL; - char *str; + VIR_AUTOFREE(char *) str = NULL; if (virAsprintf(&str, "%u", data) < 0) return NULL; val = virJSONValueNewNumber(str); - VIR_FREE(str); return val; } @@ -523,11 +521,10 @@ virJSONValuePtr virJSONValueNewNumberLong(long long data) { virJSONValuePtr val = NULL; - char *str; + VIR_AUTOFREE(char *) str = NULL; if (virAsprintf(&str, "%lld", data) < 0) return NULL; val = virJSONValueNewNumber(str); - VIR_FREE(str); return val; } @@ -536,11 +533,10 @@ virJSONValuePtr virJSONValueNewNumberUlong(unsigned long long data) { virJSONValuePtr val = NULL; - char *str; + VIR_AUTOFREE(char *) str = NULL; if (virAsprintf(&str, "%llu", data) < 0) return NULL; val = virJSONValueNewNumber(str); - VIR_FREE(str); return val; } @@ -549,11 +545,10 @@ virJSONValuePtr virJSONValueNewNumberDouble(double data) { virJSONValuePtr val = NULL; - char *str; + VIR_AUTOFREE(char *) str = NULL; if (virDoubleToStr(&str, data) < 0) return NULL; val = virJSONValueNewNumber(str); - VIR_FREE(str); return val; } @@ -1171,10 +1166,9 @@ int virJSONValueGetArrayAsBitmap(const virJSONValue *val, virBitmapPtr *bitmap) { - int ret = -1; virJSONValuePtr elem; size_t i; - unsigned long long *elems = NULL; + VIR_AUTOFREE(unsigned long long *) elems = NULL; unsigned long long maxelem = 0; *bitmap = NULL; @@ -1191,25 +1185,20 @@ virJSONValueGetArrayAsBitmap(const virJSONValue *val, if (elem->type != VIR_JSON_TYPE_NUMBER || virStrToLong_ullp(elem->data.number, NULL, 10, &elems[i]) < 0) - goto cleanup; + return -1; if (elems[i] > maxelem) maxelem = elems[i]; } if (!(*bitmap = virBitmapNewQuiet(maxelem + 1))) - goto cleanup; + return -1; /* second pass sets the correct bits in the map */ for (i = 0; i < val->data.array.nvalues; i++) ignore_value(virBitmapSetBit(*bitmap, elems[i])); - ret = 0; - - cleanup: - VIR_FREE(elems); - - return ret; + return 0; } -- 1.8.3.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list