Patchwork [v2,1/2] accomodate OpenFirmware device paths in sufficient storage

login
register
mail settings
Submitter Laszlo Ersek
Date July 25, 2012, 12:19 a.m.
Message ID <1343175595-7944-2-git-send-email-lersek@redhat.com>
Download mbox | patch
Permalink /patch/173085/
State New
Headers show

Comments

Laszlo Ersek - July 25, 2012, 12:19 a.m.
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 hw/qdev.c |   54 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 43 insertions(+), 11 deletions(-)
Peter Maydell - July 25, 2012, 7:34 a.m.
On 25 July 2012 01:19, Laszlo Ersek <lersek@redhat.com> wrote:
>
> Signed-off-by: Laszlo Ersek <lersek@redhat.com>
> ---
>  hw/qdev.c |   54 +++++++++++++++++++++++++++++++++++++++++++-----------
>  1 files changed, 43 insertions(+), 11 deletions(-)

Yuck. Just redo qdev_get_fw_dev_path along the lines I
suggested please. This is way too complicated.

-- PMM

> 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
>
>
Markus Armbruster - July 25, 2012, 8:11 a.m.
Reviewed-by: Markus Armbruster <armbru@redhat.com>

Patch

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)