From 17d0bc01bfcce0ad4fb5105d4502595224569ff0 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Tue, 8 Aug 2017 13:22:51 +0100 Subject: [PATCH] virtio-blk: handle blk_getlength() errors If blk_getlength() fails in virtio_blk_update_config() consider the disk image length to be 0 bytes. Signed-off-by: Stefan Hajnoczi Reviewed-by: Fam Zheng Message-id: 20170808122251.29815-1-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- hw/block/virtio-blk.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index b750bd8b53..a16ac75090 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -730,6 +730,7 @@ static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) BlockConf *conf = &s->conf.conf; struct virtio_blk_config blkcfg; uint64_t capacity; + int64_t length; int blk_size = conf->logical_block_size; blk_get_geometry(s->blk, &capacity); @@ -752,7 +753,8 @@ static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) * divided by 512 - instead it is the amount of blk_size blocks * per track (cylinder). */ - if (blk_getlength(s->blk) / conf->heads / conf->secs % blk_size) { + length = blk_getlength(s->blk); + if (length > 0 && length / conf->heads / conf->secs % blk_size) { blkcfg.geometry.sectors = conf->secs & ~s->sector_mask; } else { blkcfg.geometry.sectors = conf->secs;