From d09b8364d9f89c9d5f36dc983c4d4a36bb7388b9 Mon Sep 17 00:00:00 2001
From: Pan Nengyuan <pannengyuan@huawei.com>
Date: Thu, 16 Jan 2020 17:29:29 +0800
Subject: [PATCH] block: fix memleaks in bdrv_refresh_filename

If we call the qmp 'query-block' while qemu is working on 'block-commit',
it will cause memleaks. The memory leak stack is as follow:

Indirect leak of 12360 byte(s) in 3 object(s) allocated from:
    #0 0x7f80f0b6d970 in __interceptor_calloc (/lib64/libasan.so.5+0xef970)
    #1 0x7f80ee86049d in g_malloc0 (/lib64/libglib-2.0.so.0+0x5249d)
    #2 0x55ea95b5bb67 in qdict_new /mnt/sdb/qemu/qobject/qdict.c
    #3 0x55ea956cd043 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #4 0x55ea956cc950 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #5 0x55ea956cc950 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #6 0x55ea956cc950 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #7 0x55ea958818ea in bdrv_block_device_info /mnt/sdb/qemu/block/qapi.c
    #8 0x55ea958879de in bdrv_query_info /mnt/sdb/qemu/block/qapi.c
    #9 0x55ea9588b58f in qmp_query_block /mnt/sdb/qemu/block/qapi.c
    #10 0x55ea95567392 in qmp_marshal_query_block qapi/qapi-commands-block-core.c

Indirect leak of 4120 byte(s) in 1 object(s) allocated from:
    #0 0x7f80f0b6d970 in __interceptor_calloc (/lib64/libasan.so.5+0xef970)
    #1 0x7f80ee86049d in g_malloc0 (/lib64/libglib-2.0.so.0+0x5249d)
    #2 0x55ea95b5bb67 in qdict_new /mnt/sdb/qemu/qobject/qdict.c
    #3 0x55ea956cd043 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #4 0x55ea956cc950 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #5 0x55ea956cc950 in bdrv_refresh_filename /mnt/sdb/qemu/block.c
    #6 0x55ea9569f301 in bdrv_backing_attach /mnt/sdb/qemu/block.c
    #7 0x55ea956a99dd in bdrv_replace_child_noperm /mnt/sdb/qemu/block.c
    #8 0x55ea956b9b53 in bdrv_replace_node /mnt/sdb/qemu/block.c
    #9 0x55ea956b9e49 in bdrv_append /mnt/sdb/qemu/block.c
    #10 0x55ea958c3472 in commit_start /mnt/sdb/qemu/block/commit.c
    #11 0x55ea94b68ab0 in qmp_block_commit /mnt/sdb/qemu/blockdev.c

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
---
 block.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/block.c b/block.c
index 9ae5c0e..52bad05 100644
--- a/block.c
+++ b/block.c
@@ -6048,6 +6048,7 @@ void bdrv_refresh_filename(BlockDriverState *bs)
                 child->bs->exact_filename);
         pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename);
 
+        qobject_unref(bs->full_open_options);
         bs->full_open_options = qobject_ref(child->bs->full_open_options);
 
         return;
-- 
1.8.3.1

