
On Thu, Apr 08, 2021 at 13:19:53 +0200, Tim Wiederhake wrote:
Convenience function to return the value of an integer XML attribute.
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 57 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++ 3 files changed, 64 insertions(+)
[...]
diff --git a/src/util/virxml.c b/src/util/virxml.c index f62c5c39c4..bd6b75150e 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -656,6 +656,63 @@ virXMLPropTristateSwitch(xmlNodePtr node, const char* name, }
+/** + * virXMLPropInt: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @base: Number base, see strtol + * @flags: Bitwise or of virXMLPropFlags + * @result: The returned value + * + * Convenience function to return value of an integer attribute. + * + * Returns 1 in case of success in which case @result is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure.
Comment doesn't say what happens to @result on 0 or -1, but reading it implies that @result is left untouched ...
+ */ +int +virXMLPropInt(xmlNodePtr node, const char *name, int base, + virXMLPropFlags flags, int *result) +{ + g_autofree char *tmp = NULL; + int ret; + + if (!node || !name || !result) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid argument to %s"), + __FUNCTION__); + return -1; + } + + if (!(tmp = virXMLPropString(node, name))) { + if ((flags & VIR_XML_PROP_REQUIRED) != VIR_XML_PROP_REQUIRED) + return 0;
Same general comments regarding checks and header formatting as in previous instances.
+ + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"), + name, node->name); + return -1; + } + + ret = virStrToLong_i(tmp, NULL, base, result); + + if (ret < 0) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s': '%s'. Expected integer value"), + name, node->name, tmp); + return -1; + } + + if ((flags & VIR_XML_PROP_NONZERO) && (*result == 0)) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s': Zero is not permitted"), + name, node->name);
'return -1;' missing. Also in this case @result is modified despite returning -1 which breaks expectations from the comment.
+ } + + return 1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate
Reviewed-by: Peter Krempa <pkrempa@redhat.com>