[libvirt] [PATCH 2/3] conf: tighten up XML integer parsing
Stefan Berger
stefanb at linux.vnet.ibm.com
Thu Apr 19 01:03:44 UTC 2012
On 04/18/2012 08:14 PM, Eric Blake wrote:
> https://bugzilla.redhat.com/show_bug.cgi?id=617711 reported that
> even with my recent patched to allow<memory unit='G'>1</memory>,
> people can still get away with trying<memory>1G</memory> and
> silently get<memory unit='KiB'>1</memory> instead. While
> virt-xml-validate catches the error, our C parser was not.
>
> I always love it when I can reduce lines of code while fixing bugs.
>
> * src/conf/domain_conf.c (virDomainDefParseXML): Avoid strtoll.
> * src/conf/storage_conf.c (virStorageDefParsePerms): Likewise.
> * src/util/xml.c (virXPathLongBase, virXPathULongBase)
> (virXPathULongLong, virXPathLongLong): Likewise.
> ---
> src/conf/domain_conf.c | 12 +++++-------
> src/conf/storage_conf.c | 6 +++---
> src/util/xml.c | 36 ++++--------------------------------
> 3 files changed, 12 insertions(+), 42 deletions(-)
>
> index 2330fa1..64cc0cd 100644
> --- a/src/conf/storage_conf.c
> +++ b/src/conf/storage_conf.c
> @@ -570,14 +570,14 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
> if (!mode) {
> perms->mode = defaultmode;
> } else {
> - char *end = NULL;
> - perms->mode = strtol(mode,&end, 8);
> - if (*end || (perms->mode& ~0777)) {
> + int tmp;
Nit: empty line after var decl?
> + if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
[...]
@@ -465,15 +444,8 @@ virXPathLongLong(const char *xpath,
> ctxt->node = relnode;
> if ((obj != NULL)&& (obj->type == XPATH_STRING)&&
> (obj->stringval != NULL)&& (obj->stringval[0] != 0)) {
> - char *conv = NULL;
> - unsigned long long val;
> -
> - val = strtoll((const char *) obj->stringval,&conv, 10);
> - if (conv == (const char *) obj->stringval) {
> + if (virStrToLong_ll((char *) obj->stringval, NULL, 10, value)< 0)
> ret = -2;
> - } else {
> - *value = val;
> - }
> } else if ((obj != NULL)&& (obj->type == XPATH_NUMBER)&&
> (!(isnan(obj->floatval)))) {
> *value = (long long) obj->floatval;
Again there was pattern to it :-)
ACK
More information about the libvir-list
mailing list