From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:57023) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ry6vr-0001Zk-10 for qemu-devel@nongnu.org; Thu, 16 Feb 2012 14:23:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ry6vm-0008Cj-L6 for qemu-devel@nongnu.org; Thu, 16 Feb 2012 14:23:54 -0500 Received: from fe01x03-cgp.akado.ru ([77.232.31.164]:52622 helo=akado.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ry6vm-0008Aw-Bz for qemu-devel@nongnu.org; Thu, 16 Feb 2012 14:23:50 -0500 Date: Thu, 16 Feb 2012 23:23:40 +0400 (MSK) From: malc In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: Re: [Qemu-devel] [PATCH 2/2] pci: rewrite devaddr parsing List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Michael S. Tsirkin" Cc: qemu-devel@nongnu.org On Thu, 16 Feb 2012, Michael S. Tsirkin wrote: > Use scanf instead of manual string scanning. > > Signed-off-by: Michael S. Tsirkin > --- > 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) > * [[:]:]., 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 [[:]:] */ > + 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:av1474@comtv.ru