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

Michal Privoznik mprivozn at redhat.com
Tue Oct 11 09:46:25 UTC 2016


On 11.10.2016 04:15, 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 new-line.
> ---
>  src/util/virxml.c | 18 ++++++++++++++++++
>  src/util/virxml.h |  4 ++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/src/util/virxml.c b/src/util/virxml.c
> index 03bd784..450487e 100644
> --- a/src/util/virxml.c
> +++ b/src/util/virxml.c
> @@ -890,6 +890,24 @@ virXMLChildElementCount(xmlNodePtr node)
>      return ret;
>  }
>  
> +/**
> + * virXMLCheckString: checks if string contains at least one of
> + * forbidden characters
> + *
> + * Returns: 0 if string don't contains any of given characters, -1 otherwise
> + */
> +int virXMLCheckString(const char *nodeName, char *str,
> +                      const char *forbiddenChars)
> +{
> +    char *c;
> +    c = strpbrk(str, forbiddenChars);
> +    if (c) {
> +        virReportError(VIR_ERR_XML_DETAIL,
> +            _("invalid char in %s: %c"), nodeName, *c);
> +        return -1;
> +    }
> +    return 0;
> +}
>  
>  /**
>   * virXMLNodeToString: convert an XML node ptr to an XML string
> diff --git a/src/util/virxml.h b/src/util/virxml.h
> index 7a0a1da..676bf5e 100644
> --- a/src/util/virxml.h
> +++ b/src/util/virxml.h
> @@ -75,6 +75,10 @@ char *          virXMLPropString(xmlNodePtr node,
>                                   const char *name);
>  long     virXMLChildElementCount(xmlNodePtr node);
>  
> +int            virXMLCheckString(const char *nodeName,
> +                                 char *str,
> +                                 const char *forbiddenChars);
> +
>  /* Internal function; prefer the macros below.  */
>  xmlDocPtr      virXMLParseHelper(int domcode,
>                                   const char *filename,
> 

Looking good, however you forgot to add the symbol to
src/libvirt_private.syms. Without it, other parts of the code might not
be able to use this function if linker decides to optimize the symbol out.

Michal




More information about the libvir-list mailing list