Patchwork fix qdev_print_devinfo()

login
register
mail settings
Submitter Gerd Hoffmann
Date Aug. 3, 2009, 9:26 a.m.
Message ID <1249291608-14766-1-git-send-email-kraxel@redhat.com>
Download mbox | patch
Permalink /patch/30487/
State Superseded
Headers show

Comments

Gerd Hoffmann - Aug. 3, 2009, 9:26 a.m.
snprintf returns number of bytes needed for the output, not the number
of bytes actually written.  Thus the math is wrong ...

Spotted by Markus Armbruster.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/qdev.c |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
Markus Armbruster - Aug. 3, 2009, 10:52 a.m.
Gerd Hoffmann <kraxel@redhat.com> writes:

> snprintf returns number of bytes needed for the output, not the number
> of bytes actually written.  Thus the math is wrong ...
>
> Spotted by Markus Armbruster.
>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/qdev.c |   26 +++++++++++++++++---------
>  1 files changed, 17 insertions(+), 9 deletions(-)
>
> diff --git a/hw/qdev.c b/hw/qdev.c
> index 9488dba..fc95115 100644
> --- a/hw/qdev.c
> +++ b/hw/qdev.c
> @@ -108,15 +108,23 @@ DeviceState *qdev_create(BusState *bus, const char *name)
>  static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
>  {
>      int pos = 0;
> +    int ret;
>  
> -    pos += snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
> -                    info->name, info->bus_info->name);
> -    if (info->alias)
> -        pos += snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
> -    if (info->desc)
> -        pos += snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
> -    if (info->no_user)
> -        pos += snprintf(dest+pos, len-pos, ", no-user");
> +    ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
> +                   info->name, info->bus_info->name);
> +    pos += MIN(len-pos,ret);
> +    if (info->alias) {
> +        ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
> +        pos += MIN(len-pos,ret);
> +    }
> +    if (info->desc) {
> +        ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
> +        pos += MIN(len-pos,ret);
> +    }
> +    if (info->no_user) {
> +        ret = snprintf(dest+pos, len-pos, ", no-user");
> +        pos += MIN(len-pos,ret);
> +    }
>      return pos;
>  }

Looks good.

For a general purpose formatting function, I'd return how much buffer is
needed to avoid truncation, just like snprintf(), to let the caller
resize its buffer.  But qdev_print_devinfo()'s callers don't care.

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index 9488dba..fc95115 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -108,15 +108,23 @@  DeviceState *qdev_create(BusState *bus, const char *name)
 static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
 {
     int pos = 0;
+    int ret;
 
-    pos += snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
-                    info->name, info->bus_info->name);
-    if (info->alias)
-        pos += snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
-    if (info->desc)
-        pos += snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
-    if (info->no_user)
-        pos += snprintf(dest+pos, len-pos, ", no-user");
+    ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
+                   info->name, info->bus_info->name);
+    pos += MIN(len-pos,ret);
+    if (info->alias) {
+        ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
+        pos += MIN(len-pos,ret);
+    }
+    if (info->desc) {
+        ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
+        pos += MIN(len-pos,ret);
+    }
+    if (info->no_user) {
+        ret = snprintf(dest+pos, len-pos, ", no-user");
+        pos += MIN(len-pos,ret);
+    }
     return pos;
 }