util: json: use VIR_AUTOPTR for aggregate types

By making use of GNU C's cleanup attribute handled by the
VIR_AUTOPTR macro for declaring aggregate pointer variables,
majority of the calls to *Free functions can be dropped, which
in turn leads to getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Sukrit Bhatnagar 2018-07-13 23:24:59 +05:30 committed by Erik Skultety
parent b07ee8074e
commit c450b55a65
1 changed files with 5 additions and 13 deletions

View File

@ -1786,7 +1786,7 @@ virJSONValueFromString(const char *jsonstring)
size_t len = strlen(jsonstring); size_t len = strlen(jsonstring);
# ifndef WITH_YAJL2 # ifndef WITH_YAJL2
yajl_parser_config cfg = { 0, 1 }; /* Match yajl 2 default behavior */ yajl_parser_config cfg = { 0, 1 }; /* Match yajl 2 default behavior */
virJSONValuePtr tmp; VIR_AUTOPTR(virJSONValue) tmp = NULL;
# endif # endif
VIR_DEBUG("string=%s", jsonstring); VIR_DEBUG("string=%s", jsonstring);
@ -1850,7 +1850,6 @@ virJSONValueFromString(const char *jsonstring)
jsonstring); jsonstring);
else else
ret = virJSONValueArraySteal(tmp, 0); ret = virJSONValueArraySteal(tmp, 0);
virJSONValueFree(tmp);
# endif # endif
} }
@ -2023,16 +2022,12 @@ char *
virJSONStringReformat(const char *jsonstr, virJSONStringReformat(const char *jsonstr,
bool pretty) bool pretty)
{ {
virJSONValuePtr json; VIR_AUTOPTR(virJSONValue) json = NULL;
char *ret;
if (!(json = virJSONValueFromString(jsonstr))) if (!(json = virJSONValueFromString(jsonstr)))
return NULL; return NULL;
ret = virJSONValueToString(json, pretty); return virJSONValueToString(json, pretty);
virJSONValueFree(json);
return ret;
} }
@ -2121,7 +2116,7 @@ virJSONValueObjectDeflattenWorker(const char *key,
virJSONValuePtr virJSONValuePtr
virJSONValueObjectDeflatten(virJSONValuePtr json) virJSONValueObjectDeflatten(virJSONValuePtr json)
{ {
virJSONValuePtr deflattened; VIR_AUTOPTR(virJSONValue) deflattened = NULL;
virJSONValuePtr ret = NULL; virJSONValuePtr ret = NULL;
if (!(deflattened = virJSONValueNewObject())) if (!(deflattened = virJSONValueNewObject()))
@ -2130,12 +2125,9 @@ virJSONValueObjectDeflatten(virJSONValuePtr json)
if (virJSONValueObjectForeachKeyValue(json, if (virJSONValueObjectForeachKeyValue(json,
virJSONValueObjectDeflattenWorker, virJSONValueObjectDeflattenWorker,
deflattened) < 0) deflattened) < 0)
goto cleanup; return NULL;
VIR_STEAL_PTR(ret, deflattened); VIR_STEAL_PTR(ret, deflattened);
cleanup:
virJSONValueFree(deflattened);
return ret; return ret;
} }