[libvirt PATCH v5 4/8] virxml: Add virXMLPropUInt

Peter Krempa pkrempa at redhat.com
Fri Apr 16 07:41:44 UTC 2021


On Thu, Apr 08, 2021 at 13:19:54 +0200, Tim Wiederhake wrote:
> Convenience function to return the value of an unsigned integer XML attribute.
> 
> Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virxml.c        | 61 ++++++++++++++++++++++++++++++++++++++++
>  src/util/virxml.h        |  6 ++++
>  3 files changed, 68 insertions(+)

[...]

Observe same generic comments I had on 3/8.

> diff --git a/src/util/virxml.c b/src/util/virxml.c
> index bd6b75150e..fba02900f5 100644
> --- a/src/util/virxml.c
> +++ b/src/util/virxml.c
> @@ -713,6 +713,67 @@ virXMLPropInt(xmlNodePtr node, const char *name, int base,
>  }
>  
>  
> +/**
> + * virXMLPropUInt:
> + * @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 unsigned 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.
> + */
> +int
> +virXMLPropUInt(xmlNodePtr node, const char* name, int base,
> +               virXMLPropFlags flags, unsigned 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;
> +
> +        virReportError(VIR_ERR_XML_ERROR,
> +                       _("Missing required attribute '%s' in element '%s'"),
> +                       name, node->name);
> +        return -1;
> +    }
> +
> +    if (flags & VIR_XML_PROP_WRAPNEGATIVE) {
> +        ret = virStrToLong_ui(tmp, NULL, base, result);

I find this case to be a very bad anti-feature ...

> +    } else {
> +        ret = virStrToLong_uip(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;
> +}
> +
> +
>  /**
>   * virXPathBoolean:
>   * @xpath: the XPath string to evaluate
> diff --git a/src/util/virxml.h b/src/util/virxml.h
> index aba863ae11..82d5d16375 100644
> --- a/src/util/virxml.h
> +++ b/src/util/virxml.h
> @@ -37,6 +37,7 @@ typedef enum {
>      VIR_XML_PROP_OPTIONAL       = 0,        /* Attribute may be absent */
>      VIR_XML_PROP_REQUIRED       = 1 << 0,   /* Attribute may not be absent */
>      VIR_XML_PROP_NONZERO        = 1 << 1,   /* Attribute may not be zero */
> +    VIR_XML_PROP_WRAPNEGATIVE   = 1 << 2,   /* Wrap around negative values */

So I'm not very happy to see it being made easy to be abused in the
future.

Under same conditions as 3/8:

Reviewed-by: Peter Krempa <pkrempa at redhat.com>




More information about the libvir-list mailing list