OSDN Git Service

block: fix leaks in bdrv_open_driver()
authorManos Pitsidianakis <el13635@mail.ntua.gr>
Fri, 14 Jul 2017 14:35:48 +0000 (17:35 +0300)
committerKevin Wolf <kwolf@redhat.com>
Tue, 1 Aug 2017 16:09:33 +0000 (18:09 +0200)
commit180ca19ae02be70f9b158bfd7dec1ff123b9cf8c
treea3272008113fdfc2eaa3eb723d6e481fcc2909f3
parent998cbd6a44cd96044f56713274bdf6a94cd721c0
block: fix leaks in bdrv_open_driver()

bdrv_open_driver() is called in two places, bdrv_new_open_driver() and
bdrv_open_common(). In the latter, failure cleanup in is in its caller,
bdrv_open_inherit(), which unrefs the bs->file of the failed driver open
if it exists.

Let's move the bs->file cleanup to bdrv_open_driver() to take care of
all callers and do not set bs->drv to NULL unless the driver's open
function failed. When bs is destroyed by removing its last reference, it
calls bdrv_close() which checks bs->drv to perform the needed cleanups
and also call the driver's close function. Since it cleans up options
and opaque we must take care not leave dangling pointers.

The error paths in bdrv_open_driver() are now two:
If open fails, drv->bdrv_close() should not be called. Unref the child
if it exists, free what we allocated and set bs->drv to NULL. Return the
error and let callers free their stuff.

If open succeeds but we fail after, return the error and let callers
unref and delete their bs, while cleaning up their allocations.

Signed-off-by: Manos Pitsidianakis <el13635@mail.ntua.gr>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
block.c