diff mbox

[V10,15/17] block: dump to buffer for bdrv_image_info_dump()

Message ID 1363961953-13561-16-git-send-email-xiawenc@linux.vnet.ibm.com
State New
Headers show

Commit Message

Wayne Xia March 22, 2013, 2:19 p.m. UTC
This allow hmp use this function, just like qemu-img.
It also returns a pointer now to make it easy to use.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 block/qapi.c         |   67 +++++++++++++++++++++++++++++++++++--------------
 include/block/qapi.h |    2 +-
 qemu-img.c           |    6 +++-
 3 files changed, 54 insertions(+), 21 deletions(-)

Comments

Eric Blake April 1, 2013, 7:17 p.m. UTC | #1
On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   This allow hmp use this function, just like qemu-img.
> It also returns a pointer now to make it easy to use.
> 

>  
> -void bdrv_image_info_dump(ImageInfo *info)
> +GCC_FMT_ATTR(3, 4)
> +static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...)

Yuck.  I'm too worried that you are likely to cause truncation when you
exceed the bounds of the fixed-width buffer.  And you can't argue that
this is here to avoid malloc pressure, since...

>  
> +#define IMAGE_INFO_BUF_SIZE (2048)
>  static void dump_human_image_info_list(ImageInfoList *list)
>  {
>      ImageInfoList *elem;
>      bool delim = false;
> +    char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE);

...you are doing a malloc for the original buffer in the first place.
I'd much rather see use of g_string_append_printf or some similar glib
interface that manages a dynamically-sized output buffer to begin with,
than to attempt to force the output to fit in a fixed-width malloc'd buffer.
Wayne Xia April 2, 2013, 2:42 a.m. UTC | #2
于 2013-4-2 3:17, Eric Blake 写道:
> On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>>    This allow hmp use this function, just like qemu-img.
>> It also returns a pointer now to make it easy to use.
>>
>
>>
>> -void bdrv_image_info_dump(ImageInfo *info)
>> +GCC_FMT_ATTR(3, 4)
>> +static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...)
>
> Yuck.  I'm too worried that you are likely to cause truncation when you
> exceed the bounds of the fixed-width buffer.  And you can't argue that
> this is here to avoid malloc pressure, since...
>
>>
>> +#define IMAGE_INFO_BUF_SIZE (2048)
>>   static void dump_human_image_info_list(ImageInfoList *list)
>>   {
>>       ImageInfoList *elem;
>>       bool delim = false;
>> +    char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE);
>
> ...you are doing a malloc for the original buffer in the first place.
> I'd much rather see use of g_string_append_printf or some similar glib
> interface that manages a dynamically-sized output buffer to begin with,
> than to attempt to force the output to fit in a fixed-width malloc'd buffer.
>
   I have considered it before, but here g_malloc0 always allocate
a fixed half page size which brings less fragment than dynamic
allocation according to string length, just as my comments in code:

+/* Use buf instead of asprintf to reduce memory fragility. */
+char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info)

  Personally I'd like to avoid asprintf since there would be many
times of reallocation for a single info dumping, not worthy I think,
and there will not be much trouble if string truncate is tipped.
diff mbox

Patch

diff --git a/block/qapi.c b/block/qapi.c
index 477659a..c7932eb 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -326,9 +326,30 @@  BlockInfoList *qmp_query_block(Error **errp)
     return NULL;
 }
 
-void bdrv_image_info_dump(ImageInfo *info)
+GCC_FMT_ATTR(3, 4)
+static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...)
+{
+    int l;
+    if (*p_size < 1) {
+        return;
+    }
+
+    va_list ap;
+    va_start(ap, fmt);
+    l = vsnprintf(*p_buf, *p_size, fmt, ap);
+    va_end(ap);
+    if (l > 0) {
+        *p_buf += l;
+        *p_size -= l;
+    }
+}
+
+/* Use buf instead of asprintf to reduce memory fragility. */
+char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info)
 {
     char size_buf[128], dsize_buf[128];
+    char *buf1 = buf;
+
     if (!info->has_actual_size) {
         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
     } else {
@@ -336,43 +357,49 @@  void bdrv_image_info_dump(ImageInfo *info)
                                 info->actual_size);
     }
     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
-    printf("image: %s\n"
-           "file format: %s\n"
-           "virtual size: %s (%" PRId64 " bytes)\n"
-           "disk size: %s\n",
-           info->filename, info->format, size_buf,
-           info->virtual_size,
-           dsize_buf);
+    snprintf_tail(&buf1, &buf_size,
+                  "image: %s\n"
+                  "file format: %s\n"
+                  "virtual size: %s (%" PRId64 " bytes)\n"
+                  "disk size: %s\n",
+                  info->filename, info->format, size_buf,
+                  info->virtual_size,
+                  dsize_buf);
 
     if (info->has_encrypted && info->encrypted) {
-        printf("encrypted: yes\n");
+        snprintf_tail(&buf1, &buf_size, "encrypted: yes\n");
     }
 
     if (info->has_cluster_size) {
-        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
+        snprintf_tail(&buf1, &buf_size, "cluster_size: %" PRId64 "\n",
+                 info->cluster_size);
     }
 
     if (info->has_dirty_flag && info->dirty_flag) {
-        printf("cleanly shut down: no\n");
+        snprintf_tail(&buf1, &buf_size, "cleanly shut down: no\n");
     }
 
     if (info->has_backing_filename) {
-        printf("backing file: %s", info->backing_filename);
+        snprintf_tail(&buf1, &buf_size, "backing file: %s",
+                      info->backing_filename);
         if (info->has_full_backing_filename) {
-            printf(" (actual path: %s)", info->full_backing_filename);
+            snprintf_tail(&buf1, &buf_size, " (actual path: %s)",
+                          info->full_backing_filename);
         }
-        putchar('\n');
+        snprintf_tail(&buf1, &buf_size, "\n");
         if (info->has_backing_filename_format) {
-            printf("backing file format: %s\n", info->backing_filename_format);
+            snprintf_tail(&buf1, &buf_size, "backing file format: %s\n",
+                          info->backing_filename_format);
         }
     }
 
     if (info->has_snapshots) {
         SnapshotInfoList *elem;
-        char buf[256];
+        char buf_sn[256];
 
-        printf("Snapshot list:\n");
-        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        snprintf_tail(&buf1, &buf_size, "Snapshot list:\n");
+        snprintf_tail(&buf1, &buf_size, "%s\n",
+                      bdrv_snapshot_dump(buf_sn, sizeof(buf_sn), NULL));
 
         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
          * we convert to the block layer's native QEMUSnapshotInfo for now.
@@ -388,7 +415,9 @@  void bdrv_image_info_dump(ImageInfo *info)
 
             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
-            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
+            snprintf_tail(&buf1, &buf_size, "%s\n",
+                          bdrv_snapshot_dump(buf_sn, sizeof(buf_sn), &sn));
         }
     }
+    return buf;
 }
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 5b2762c..4c4677c 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -38,5 +38,5 @@  int bdrv_query_image_info(BlockDriverState *bs,
 int bdrv_query_info(BlockDriverState *bs,
                     BlockInfo **p_info,
                     Error **errp);
-void bdrv_image_info_dump(ImageInfo *info);
+char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info);
 #endif
diff --git a/qemu-img.c b/qemu-img.c
index 5b229a9..d3fce63 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1606,10 +1606,12 @@  static void dump_json_image_info(ImageInfo *info)
     QDECREF(str);
 }
 
+#define IMAGE_INFO_BUF_SIZE (2048)
 static void dump_human_image_info_list(ImageInfoList *list)
 {
     ImageInfoList *elem;
     bool delim = false;
+    char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE);
 
     for (elem = list; elem; elem = elem->next) {
         if (delim) {
@@ -1617,8 +1619,10 @@  static void dump_human_image_info_list(ImageInfoList *list)
         }
         delim = true;
 
-        bdrv_image_info_dump(elem->value);
+        printf("%s",
+               bdrv_image_info_dump(buf, IMAGE_INFO_BUF_SIZE, elem->value));
     }
+    g_free(buf);
 }
 
 static gboolean str_equal_func(gconstpointer a, gconstpointer b)