From c1059a3a3cb80bc087ad7e33fced4f8a7a9c0150 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Thu, 13 Apr 2017 17:43:34 +0200 Subject: [PATCH] block: Do not unref bs->file on error in BD's open The block layer takes care of removing the bs->file child if the block driver's bdrv_open()/bdrv_file_open() implementation fails. The block driver therefore does not need to do so, and indeed should not unless it sets bs->file to NULL afterwards -- because if this is not done, the bdrv_unref_child() in bdrv_open_inherit() will dereference the freed memory block at bs->file afterwards, which is not good. We can now decide whether to add a "bs->file = NULL;" after each of the offending bdrv_unref_child() invocations, or just drop them altogether. The latter is simpler, so let's do that. Cc: qemu-stable Signed-off-by: Max Reitz Reviewed-by: Eric Blake Reviewed-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf (cherry picked from commit de234897b60e034ba94b307fc289e2dc692c9251) Signed-off-by: Michael Roth --- block/blkdebug.c | 4 +--- block/blkreplay.c | 3 --- block/blkverify.c | 3 --- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index 67e8024e36..cc4a146e84 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -389,14 +389,12 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, } else if (align) { error_setg(errp, "Invalid alignment"); ret = -EINVAL; - goto fail_unref; + goto out; } ret = 0; goto out; -fail_unref: - bdrv_unref_child(bs, bs->file); out: if (ret < 0) { g_free(s->config_file); diff --git a/block/blkreplay.c b/block/blkreplay.c index e1102119fb..6aa5fd4156 100755 --- a/block/blkreplay.c +++ b/block/blkreplay.c @@ -37,9 +37,6 @@ static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags, ret = 0; fail: - if (ret < 0) { - bdrv_unref_child(bs, bs->file); - } return ret; } diff --git a/block/blkverify.c b/block/blkverify.c index 9a1e21c6ad..af23281669 100644 --- a/block/blkverify.c +++ b/block/blkverify.c @@ -142,9 +142,6 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, ret = 0; fail: - if (ret < 0) { - bdrv_unref_child(bs, bs->file); - } qemu_opts_del(opts); return ret; } -- 2.39.2