qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: qemu-devel@nongnu.org, armbru@redhat.com,
	peter.maydell@linaro.org, lersek@redhat.com
Subject: [Qemu-devel] [PATCH v2 1/2] accomodate OpenFirmware device paths in sufficient storage
Date: Wed, 25 Jul 2012 02:19:54 +0200	[thread overview]
Message-ID: <1343175595-7944-2-git-send-email-lersek@redhat.com> (raw)
In-Reply-To: <87r4s25vs8.fsf@blackfin.pond.sub.org>


Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 hw/qdev.c |   54 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/hw/qdev.c b/hw/qdev.c
index af54467..59cc0c2 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -25,6 +25,8 @@
    inherit from a particular bus (e.g. PCI or I2C) rather than
    this API directly.  */
 
+#include <stdarg.h>
+
 #include "net.h"
 #include "qdev.h"
 #include "sysemu.h"
@@ -495,36 +497,66 @@ static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
     return NULL;
 }
 
+static int snprintf_helper(char *p, int size, int len, const char *format, ...)
+{
+    char *target;
+    int free_sz;
+    va_list args;
+    int ret;
+
+    if (len < size) {
+        target = p + len;
+        free_sz = size - len;
+    }
+    else {
+        target = NULL;
+        free_sz = 0;
+    }
+
+    va_start(args, format);
+    ret = vsnprintf(target, free_sz, format, args);
+    va_end(args);
+
+    assert(ret >= 0);
+    return ret;
+}
+
+
 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
 {
-    int l = 0;
+    int len = 0;
 
     if (dev && dev->parent_bus) {
         char *d;
-        l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
+        len = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
         d = bus_get_fw_dev_path(dev->parent_bus, dev);
         if (d) {
-            l += snprintf(p + l, size - l, "%s", d);
+            len += snprintf_helper(p, size, len, "%s", d);
             g_free(d);
         } else {
-            l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
+            len += snprintf_helper(p, size, len, "%s",
+                                   object_get_typename(OBJECT(dev)));
         }
     }
-    l += snprintf(p + l , size - l, "/");
+    len += snprintf_helper(p, size, len, "/");
 
-    return l;
+    return len;
 }
 
 char* qdev_get_fw_dev_path(DeviceState *dev)
 {
-    char path[128];
-    int l;
+    int len, len2;
+    char *path;
 
-    l = qdev_get_fw_dev_path_helper(dev, path, 128);
+    len = qdev_get_fw_dev_path_helper(dev, NULL, 0);
+    path = g_malloc(len + 1);
 
-    path[l-1] = '\0';
+    len2 = qdev_get_fw_dev_path_helper(dev, path, len + 1);
+    assert(len2 == len);
 
-    return strdup(path);
+    assert(len > 0);
+    path[len - 1] = '\0';
+    return path;
 }
 
 char *qdev_get_dev_path(DeviceState *dev)
-- 
1.7.1

  parent reply	other threads:[~2012-07-25  0:18 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-23 11:33 [Qemu-devel] [PATCH] check for available room when formatting OpenFirmware device path Laszlo Ersek
2012-07-23 12:34 ` Peter Maydell
2012-07-23 12:39   ` Peter Maydell
2012-07-23 12:46 ` Markus Armbruster
2012-07-23 13:08   ` Laszlo Ersek
2012-07-23 15:01     ` Markus Armbruster
2012-07-23 15:29       ` Laszlo Ersek
2012-07-23 15:42         ` Markus Armbruster
2012-07-25  0:19           ` [Qemu-devel] [PATCH v2 0/2] assorted device path formatting cleanups Laszlo Ersek
2012-07-25  0:19           ` Laszlo Ersek [this message]
2012-07-25  7:34             ` [Qemu-devel] [PATCH v2 1/2] accomodate OpenFirmware device paths in sufficient storage Peter Maydell
2012-07-25  8:11             ` Markus Armbruster
2012-07-25  0:19           ` [Qemu-devel] [PATCH v2 2/2] get_fw_dev_path() impls should allocate memory with glib functions Laszlo Ersek

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=1343175595-7944-2-git-send-email-lersek@redhat.com \
    --to=lersek@redhat.com \
    --cc=armbru@redhat.com \
    --cc=peter.maydell@linaro.org \
    --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).