qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 2/2] pci: rewrite devaddr parsing


From: malc
Subject: Re: [Qemu-devel] [PATCH 2/2] pci: rewrite devaddr parsing
Date: Thu, 16 Feb 2012 23:23:40 +0400 (MSK)
User-agent: Alpine 2.00 (LNX 1167 2008-08-23)

On Thu, 16 Feb 2012, Michael S. Tsirkin wrote:

> Use scanf instead of manual string scanning.
> 
> Signed-off-by: Michael S. Tsirkin <address@hidden>
> ---
>  hw/pci.c |   81 
> +++++++++++++++++++++++++++++---------------------------------
>  1 files changed, 38 insertions(+), 43 deletions(-)
> 
> diff --git a/hw/pci.c b/hw/pci.c
> index 5827c0e..a8c0b69 100644
> --- a/hw/pci.c
> +++ b/hw/pci.c
> @@ -479,61 +479,56 @@ static void pci_set_default_subsystem_id(PCIDevice 
> *pci_dev)
>   *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
>   */
>  static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
> -                      unsigned int *slotp, unsigned int *funcp)
> -{
> -    const char *p;
> -    char *e;
> -    unsigned long val;
> -    unsigned long dom = 0, bus = 0;
> -    unsigned int slot = 0;
> -    unsigned int func = 0;
> -
> -    p = addr;
> -    val = strtoul(p, &e, 16);
> -    if (e == p)
> -     return -1;
> -    if (*e == ':') {
> -     bus = val;
> -     p = e + 1;
> -     val = strtoul(p, &e, 16);
> -     if (e == p)
> -         return -1;
> -     if (*e == ':') {
> -         dom = bus;
> -         bus = val;
> -         p = e + 1;
> -         val = strtoul(p, &e, 16);
> -         if (e == p)
> -             return -1;
> -     }
> -    }
> -
> -    slot = val;
> -
> -    if (funcp != NULL) {
> -        if (*e != '.')
> -            return -1;
> +                             unsigned int *slotp, unsigned int *funcp)
> +{
> +    unsigned dom, bus, slot, func;
> +    int n = -1;
> +
> +    /* Parse [[<domain>:]<bus>:]<slot> */
> +    sscanf(addr, "%x:%x:%x%n", &dom, &bus, &slot, &n);

sscanf can fail.

> +    if (n == -1) {
> +        dom = 0;
> +        sscanf(addr, "%x:%x%n", &bus, &slot, &n);
> +        if (n == -1) {
> +            bus = 0;
> +            sscanf(addr, "%x%n", &slot, &n);
> +            if (n == -1) {
> +                return -1;
> +            }
> +        }
> +    }
>  
> -        p = e + 1;
> -        val = strtoul(p, &e, 16);
> -        if (e == p)
> +    /* Parse the optional .func */
> +    addr += n;
> +    if (!*addr) {
> +        func = 0;
> +    } else {
> +        sscanf(addr, ".%x%n", &func, &n);
> +        if (n == -1) {
>              return -1;
> +        }
> +        addr += n;
> +    }
>  
> -        func = val;
> +    /* Anything left? Malformed input. */
> +    if (*addr) {
> +        return -1;
>      }
>  
> -    /* if funcp == NULL func is 0 */
> -    if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
> -     return -1;
> +    if (funcp == NULL) {
> +        func = 0;
> +    }
>  
> -    if (*e)
> +    if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
>       return -1;
> +    }
>  
>      *domp = dom;
>      *busp = bus;
>      *slotp = slot;
> -    if (funcp != NULL)
> +    if (funcp != NULL) {
>          *funcp = func;
> +    }
>      return 0;
>  }
>  
> 

-- 
mailto:address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]