Re: [PATCH 2/4] xen-block: Fix inverted error check in xen_cdrom_realize()

Stefano Stabellini <[email protected]>
Newsgroups org.xenproject.lists.xen-devel,org.nongnu.qemu-devel
Message-ID <alpine.DEB.2.22.394.2607171816130.16921@ubuntu-linux-20-04-desktop>
On Fri, 17 Jul 2026, Mitsuru Kariya wrote:
> xen_cdrom_realize() checks the return value of blk_attach_dev() with
> 
>     if (!rc) {
>         error_setg_errno(errp, -rc, "failed to create drive");
>         return;
>     }
> 
> but blk_attach_dev() returns 0 on success and a negative errno on
> failure.  The condition is inverted: a successful attach (rc == 0) is
> reported as an error and fails realize, while a real failure (rc < 0)
> is silently ignored.
> 
> Here blk_attach_dev() is called on a BlockBackend just created by
> blk_new(), so blk->dev is NULL and its only failure path (-EBUSY)
> cannot be taken.  Replace the bogus check with an assertion, matching
> the other empty-drive realize paths (scsi-disk, ide, fdc, swim).
> 
> Signed-off-by: Mitsuru Kariya <[email protected]>


Reviewed-by: Stefano Stabellini <[email protected]>

> ---
>  hw/block/xen-block.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/hw/block/xen-block.c b/hw/block/xen-block.c
> index 474c12fe4a..abf9df2eee 100644
> --- a/hw/block/xen-block.c
> +++ b/hw/block/xen-block.c
> @@ -762,10 +762,7 @@ static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
>          conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
>  
>          rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
> -        if (!rc) {
> -            error_setg_errno(errp, -rc, "failed to create drive");
> -            return;
> -        }
> +        assert(rc == 0);
>      }
>  
>      blockdev->info = VDISK_READONLY | VDISK_CDROM;
> -- 
> 2.43.0
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.