virXMLPropTristateBool/virXMLPropTristateSwitch/virXMLPropEnum can be implemented using the same internal code. Extract it into a new function called virXMLPropEnumInternal, which will also simplify adding versions of these functions with a custom default value. This way we'll be able to always initialize @result so that unused value bugs can be prevented. Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx> --- src/util/virxml.c | 108 ++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 7cc73ab9a0..9929064993 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -557,6 +557,40 @@ virXMLNodeContentString(xmlNodePtr node) return ret; } +static int +virXMLPropEnumInternal(xmlNodePtr node, + const char* name, + int (*strToInt)(const char*), + virXMLPropFlags flags, + unsigned int *result) + +{ + g_autofree char *tmp = NULL; + int ret; + + if (!(tmp = virXMLPropString(node, name))) { + if (!(flags & VIR_XML_PROP_REQUIRED)) + return 0; + + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"), + name, node->name); + return -1; + } + + ret = strToInt(tmp); + if (ret < 0 || + ((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s': '%s'."), + name, node->name, NULLSTR(tmp)); + return -1; + } + + *result = ret; + return 1; +} + /** * virXMLPropTristateBool: @@ -577,28 +611,10 @@ virXMLPropTristateBool(xmlNodePtr node, virXMLPropFlags flags, virTristateBool *result) { - g_autofree char *tmp = NULL; - int val; - - if (!(tmp = virXMLPropString(node, name))) { - if (!(flags & VIR_XML_PROP_REQUIRED)) - return 0; - - virReportError(VIR_ERR_XML_ERROR, - _("Missing required attribute '%s' in element '%s'"), - name, node->name); - return -1; - } - - if ((val = virTristateBoolTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'yes' or 'no'"), - name, node->name, tmp); - return -1; - } + flags |= VIR_XML_PROP_NONZERO; - *result = val; - return 1; + return virXMLPropEnumInternal(node, name, virTristateBoolTypeFromString, + flags, result); } @@ -621,28 +637,10 @@ virXMLPropTristateSwitch(xmlNodePtr node, virXMLPropFlags flags, virTristateSwitch *result) { - g_autofree char *tmp = NULL; - int val; - - if (!(tmp = virXMLPropString(node, name))) { - if (!(flags & VIR_XML_PROP_REQUIRED)) - return 0; - - virReportError(VIR_ERR_XML_ERROR, - _("Missing required attribute '%s' in element '%s'"), - name, node->name); - return -1; - } - - if ((val = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'on' or 'off'"), - name, node->name, tmp); - return -1; - } + flags |= VIR_XML_PROP_NONZERO; - *result = val; - return 1; + return virXMLPropEnumInternal(node, name, virTristateSwitchTypeFromString, + flags, result); } @@ -833,32 +831,10 @@ virXMLPropEnum(xmlNodePtr node, virXMLPropFlags flags, unsigned int *result) { - g_autofree char *tmp = NULL; - int ret; - - if (!(tmp = virXMLPropString(node, name))) { - if (!(flags & VIR_XML_PROP_REQUIRED)) - return 0; - - virReportError(VIR_ERR_XML_ERROR, - _("Missing required attribute '%s' in element '%s'"), - name, node->name); - return -1; - } - - ret = strToInt(tmp); - if (ret < 0 || - ((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) { - virReportError(VIR_ERR_XML_ERROR, - _("Invalid value for attribute '%s' in element '%s': '%s'."), - name, node->name, NULLSTR(tmp)); - return -1; - } - - *result = ret; - return 1; + return virXMLPropEnumInternal(node, name, strToInt, flags, result); } + /** * virXPathBoolean: * @xpath: the XPath string to evaluate -- 2.30.2