qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 2/2] pci: rewrite devaddr parsing
Date: Thu, 16 Feb 2012 20:15:29 +0200	[thread overview]
Message-ID: <f9fbea93081f7b7aa7e4f1814cf9f6dfbd6f907d.1329416075.git.mst@redhat.com> (raw)
In-Reply-To: <cover.1329416075.git.mst@redhat.com>

Use scanf instead of manual string scanning.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 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);
+    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

  parent reply	other threads:[~2012-02-16 19:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-16 18:15 [Qemu-devel] [PATCH 0/2] pci devaddr parsing cleanups Michael S. Tsirkin
2012-02-16 18:15 ` [Qemu-devel] [PATCH 1/2] pci: don't export an internal function Michael S. Tsirkin
2012-02-16 18:15 ` Michael S. Tsirkin [this message]
2012-02-16 19:23   ` [Qemu-devel] [PATCH 2/2] pci: rewrite devaddr parsing malc
2012-02-16 20:39     ` Eric Blake
2012-02-16 21:35       ` malc
2012-02-17 10:08       ` Michael S. Tsirkin
2012-02-17 12:35         ` Markus Armbruster
2012-02-19 16:10           ` Michael S. Tsirkin
2012-02-19 20:03           ` Michael S. Tsirkin
2012-02-17  9:42     ` Michael S. Tsirkin
2012-02-17  9:50       ` malc
2012-02-17 10:13         ` Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f9fbea93081f7b7aa7e4f1814cf9f6dfbd6f907d.1329416075.git.mst@redhat.com \
    --to=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).