1
0

util: xml: use g_auto for xmlXPathObject

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Ján Tomko 2021-08-11 13:48:13 +02:00
parent 8f6657aea9
commit 441d2f4e23

View File

@ -73,7 +73,7 @@ char *
virXPathString(const char *xpath, virXPathString(const char *xpath,
xmlXPathContextPtr ctxt) xmlXPathContextPtr ctxt)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
char *ret; char *ret;
if ((ctxt == NULL) || (xpath == NULL)) { if ((ctxt == NULL) || (xpath == NULL)) {
@ -84,11 +84,9 @@ virXPathString(const char *xpath,
obj = xmlXPathEval(BAD_CAST xpath, ctxt); obj = xmlXPathEval(BAD_CAST xpath, ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) || if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) { (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
xmlXPathFreeObject(obj);
return NULL; return NULL;
} }
ret = g_strdup((char *)obj->stringval); ret = g_strdup((char *)obj->stringval);
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -148,7 +146,7 @@ virXPathNumber(const char *xpath,
xmlXPathContextPtr ctxt, xmlXPathContextPtr ctxt,
double *value) double *value)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) { if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
@ -158,12 +156,10 @@ virXPathNumber(const char *xpath,
obj = xmlXPathEval(BAD_CAST xpath, ctxt); obj = xmlXPathEval(BAD_CAST xpath, ctxt);
if ((obj == NULL) || (obj->type != XPATH_NUMBER) || if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
(isnan(obj->floatval))) { (isnan(obj->floatval))) {
xmlXPathFreeObject(obj);
return -1; return -1;
} }
*value = obj->floatval; *value = obj->floatval;
xmlXPathFreeObject(obj);
return 0; return 0;
} }
@ -173,7 +169,7 @@ virXPathLongBase(const char *xpath,
int base, int base,
long *value) long *value)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
int ret = 0; int ret = 0;
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) { if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
@ -195,7 +191,6 @@ virXPathLongBase(const char *xpath,
ret = -1; ret = -1;
} }
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -275,7 +270,7 @@ virXPathULongBase(const char *xpath,
int base, int base,
unsigned long *value) unsigned long *value)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
int ret = 0; int ret = 0;
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) { if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
@ -297,7 +292,6 @@ virXPathULongBase(const char *xpath,
ret = -1; ret = -1;
} }
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -388,7 +382,7 @@ virXPathULongLong(const char *xpath,
xmlXPathContextPtr ctxt, xmlXPathContextPtr ctxt,
unsigned long long *value) unsigned long long *value)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
int ret = 0; int ret = 0;
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) { if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
@ -410,7 +404,6 @@ virXPathULongLong(const char *xpath,
ret = -1; ret = -1;
} }
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -431,7 +424,7 @@ virXPathLongLong(const char *xpath,
xmlXPathContextPtr ctxt, xmlXPathContextPtr ctxt,
long long *value) long long *value)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
int ret = 0; int ret = 0;
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) { if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
@ -453,7 +446,6 @@ virXPathLongLong(const char *xpath,
ret = -1; ret = -1;
} }
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -895,7 +887,7 @@ int
virXPathBoolean(const char *xpath, virXPathBoolean(const char *xpath,
xmlXPathContextPtr ctxt) xmlXPathContextPtr ctxt)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
int ret; int ret;
if ((ctxt == NULL) || (xpath == NULL)) { if ((ctxt == NULL) || (xpath == NULL)) {
@ -906,12 +898,10 @@ virXPathBoolean(const char *xpath,
obj = xmlXPathEval(BAD_CAST xpath, ctxt); obj = xmlXPathEval(BAD_CAST xpath, ctxt);
if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) || if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) ||
(obj->boolval < 0) || (obj->boolval > 1)) { (obj->boolval < 0) || (obj->boolval > 1)) {
xmlXPathFreeObject(obj);
return -1; return -1;
} }
ret = obj->boolval; ret = obj->boolval;
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -929,7 +919,7 @@ xmlNodePtr
virXPathNode(const char *xpath, virXPathNode(const char *xpath,
xmlXPathContextPtr ctxt) xmlXPathContextPtr ctxt)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
xmlNodePtr ret; xmlNodePtr ret;
if ((ctxt == NULL) || (xpath == NULL)) { if ((ctxt == NULL) || (xpath == NULL)) {
@ -941,12 +931,10 @@ virXPathNode(const char *xpath,
if ((obj == NULL) || (obj->type != XPATH_NODESET) || if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
(obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) || (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
(obj->nodesetval->nodeTab == NULL)) { (obj->nodesetval->nodeTab == NULL)) {
xmlXPathFreeObject(obj);
return NULL; return NULL;
} }
ret = obj->nodesetval->nodeTab[0]; ret = obj->nodesetval->nodeTab[0];
xmlXPathFreeObject(obj);
return ret; return ret;
} }
@ -966,7 +954,7 @@ virXPathNodeSet(const char *xpath,
xmlXPathContextPtr ctxt, xmlXPathContextPtr ctxt,
xmlNodePtr **list) xmlNodePtr **list)
{ {
xmlXPathObjectPtr obj; g_autoptr(xmlXPathObject) obj = NULL;
int ret; int ret;
if ((ctxt == NULL) || (xpath == NULL)) { if ((ctxt == NULL) || (xpath == NULL)) {
@ -985,21 +973,17 @@ virXPathNodeSet(const char *xpath,
if (obj->type != XPATH_NODESET) { if (obj->type != XPATH_NODESET) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Incorrect xpath '%s'"), xpath); _("Incorrect xpath '%s'"), xpath);
xmlXPathFreeObject(obj);
return -1; return -1;
} }
if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr < 0)) { if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr < 0))
xmlXPathFreeObject(obj);
return 0; return 0;
}
ret = obj->nodesetval->nodeNr; ret = obj->nodesetval->nodeNr;
if (list != NULL && ret) { if (list != NULL && ret) {
*list = g_new0(xmlNodePtr, ret); *list = g_new0(xmlNodePtr, ret);
memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr)); memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr));
} }
xmlXPathFreeObject(obj);
return ret; return ret;
} }