[libvirt] [PATCH 1/3] util: Add function to check if string contains some illegal chars

Michal Privoznik mprivozn at redhat.com
Thu Oct 20 11:18:10 UTC 2016


On 20.10.2016 04:57, Sławek Kapłoński wrote:
> This new function can be used to check if e.g. name of XML
> node don't contains forbidden chars like "/" or "\n".
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virxml.c        | 28 ++++++++++++++++++++++++++++
>  src/util/virxml.h        |  3 +++
>  3 files changed, 32 insertions(+)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 55b6a24..f91f179 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -2678,6 +2678,7 @@ virUUIDParse;
>  # util/virxml.h
>  virXMLChildElementCount;
>  virXMLExtractNamespaceXML;
> +virXMLNodeHasIllegalChars;
>  virXMLNodeSanitizeNamespaces;
>  virXMLNodeToString;
>  virXMLParseHelper;
> diff --git a/src/util/virxml.c b/src/util/virxml.c
> index 03bd784..b0d0a94 100644
> --- a/src/util/virxml.c
> +++ b/src/util/virxml.c
> @@ -462,6 +462,34 @@ virXPathLongLong(const char *xpath,
>      return ret;
>  }
>  
> +
> +/**
> + * virXMLNodeHasIllegalChars: checks if string contains at least one of
> + * forbidden characters
> + *
> + * @node_name: Name of checked node
> + * @node: string to check
> + * @chars: illegal chars to check
> + *
> + * If node contains any of illegal chars VIR_ERR_XML_DETAIL error will be
> + * reported.
> + *
> + * Returns: 0 if string don't contains any of given characters, -1 otherwise
> + */
> +int
> +virXMLNodeHasIllegalChars(const char *node_name,
> +                          const char *node,
> +                          const char *chars)
> +{
> +    if (strpbrk(node, chars)) {
> +        virReportError(VIR_ERR_XML_DETAIL,
> +                       _("invalid char in %s: 0x%02x"), node_name, *chars);

Almost. So for instance when I call this function like this:

virXMLNodeHasIllegalChars("name", "some string\n", "\r\t\n");

I will see the following error message:

"invalid char in name: \r"

I'd expect to see "invalid char in name: \n". But that's okay. I can fix
it before pushing. Also, virXMLNode.* may suggest that it takes
xmlNodePtr which is not the case. I will rename this before pushing.

ACK

Michal




More information about the libvir-list mailing list