From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:44869) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ry6iq-00065K-JS for qemu-devel@nongnu.org; Thu, 16 Feb 2012 14:10:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ry6io-0005cy-Nd for qemu-devel@nongnu.org; Thu, 16 Feb 2012 14:10:28 -0500 Received: from mx1.redhat.com ([209.132.183.28]:65137) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ry6io-0005bh-C7 for qemu-devel@nongnu.org; Thu, 16 Feb 2012 14:10:26 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q1GJAOm5023127 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 16 Feb 2012 14:10:25 -0500 Received: from redhat.com (reserved-203-247.tlv.redhat.com [10.35.203.247] (may be forged)) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id q1GIFKZZ018883 for ; Thu, 16 Feb 2012 13:15:21 -0500 Date: Thu, 16 Feb 2012 20:15:29 +0200 From: "Michael S. Tsirkin" Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: [Qemu-devel] [PATCH 2/2] pci: rewrite devaddr parsing List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org 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); + 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; } -- 1.7.9.111.gf3fb0