qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, mreitz@redhat.com, kwolf@redhat.com,
	vsementsov@virtuozzo.com,
	Peter Maydell <peter.maydell@linaro.org>
Subject: [PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child
Date: Mon,  3 May 2021 14:33:57 +0300	[thread overview]
Message-ID: <20210503113402.185852-2-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20210503113402.185852-1-vsementsov@virtuozzo.com>

bdrv_attach_child_common() doesn't require tran_finalize() on failure
(it does tran_add() only on success path). Still tran_new() must be
paired with tran_finalize() anyway, at least to free empty Transaction
object itself.

So, refactor the function for clean finalization code, same on all
paths.

While being here, also leave a comment on unobvious background zeroing
of child pointer on failure path.

Reported-by: Coverity (CID 1452773)
Reported-by: Peter Maydell <peter.maydell@linaro.org>
Fixes: 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 block.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/block.c b/block.c
index 874c22c43e..728aa34b2f 100644
--- a/block.c
+++ b/block.c
@@ -2918,12 +2918,18 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                    child_role, perm, shared_perm, opaque,
                                    &child, tran, errp);
     if (ret < 0) {
-        bdrv_unref(child_bs);
-        return NULL;
+        goto out;
     }
 
     ret = bdrv_refresh_perms(child_bs, errp);
+    if (ret < 0) {
+        goto out;
+    }
+
+out:
     tran_finalize(tran, ret);
+    /* child is unset on failure by bdrv_attach_child_common_abort() */
+    assert((ret < 0) == !child);
 
     bdrv_unref(child_bs);
     return child;
-- 
2.29.2



  reply	other threads:[~2021-05-03 11:39 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-03 11:33 [PATCH 0/6] block permission updated follow-up Vladimir Sementsov-Ogievskiy
2021-05-03 11:33 ` Vladimir Sementsov-Ogievskiy [this message]
2021-05-03 15:51   ` [PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child Alberto Garcia
2021-05-04  6:56     ` Vladimir Sementsov-Ogievskiy
2021-05-03 11:33 ` [PATCH 2/6] block: bdrv_reopen_multiple(): fix leak of tran object Vladimir Sementsov-Ogievskiy
2021-05-03 15:52   ` Alberto Garcia
2021-05-04  6:57     ` Vladimir Sementsov-Ogievskiy
2021-05-03 11:33 ` [PATCH 3/6] block-backend: improve blk_root_get_parent_desc() Vladimir Sementsov-Ogievskiy
2021-05-03 15:54   ` Alberto Garcia
2021-05-03 11:34 ` [PATCH 4/6] block: improve bdrv_child_get_parent_desc() Vladimir Sementsov-Ogievskiy
2021-05-03 11:48   ` Vladimir Sementsov-Ogievskiy
2021-05-03 11:34 ` [PATCH 5/6] block: simplify bdrv_child_user_desc() Vladimir Sementsov-Ogievskiy
2021-05-03 16:05   ` Alberto Garcia
2021-05-04  6:57     ` Vladimir Sementsov-Ogievskiy
2021-05-03 11:34 ` [PATCH 6/6] block: improve permission conflict error message Vladimir Sementsov-Ogievskiy
2021-05-03 12:28 ` [PATCH 7/6] block: document child argument of bdrv_attach_child_common() Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210503113402.185852-2-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).