diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index e444e53e136a5cb98ff99386686a397cd8632f6e..f88118cdc5626933dd48740b227665513b69dc8b 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -39,6 +39,9 @@ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size) new_l1_size = s->l1_size; if (min_size <= new_l1_size) return 0; + if (new_l1_size == 0) { + new_l1_size = 1; + } while (min_size > new_l1_size) { new_l1_size = (new_l1_size * 3 + 1) / 2; } diff --git a/block/qcow2.c b/block/qcow2.c index a9e768268a6df9aeaaa4a6448dcd4f8c03dba4b1..3954cf13984c3a0c9df0143132fd8369f33c7e36 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -166,8 +166,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) goto fail; - if (header.size <= 1 || - header.cluster_bits < MIN_CLUSTER_BITS || + if (header.cluster_bits < MIN_CLUSTER_BITS || header.cluster_bits > MAX_CLUSTER_BITS) goto fail; if (header.crypt_method > QCOW_CRYPT_AES) @@ -200,13 +199,15 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) if (s->l1_size < s->l1_vm_state_index) goto fail; s->l1_table_offset = header.l1_table_offset; - s->l1_table = qemu_mallocz( - align_offset(s->l1_size * sizeof(uint64_t), 512)); - if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != - s->l1_size * sizeof(uint64_t)) - goto fail; - for(i = 0;i < s->l1_size; i++) { - be64_to_cpus(&s->l1_table[i]); + if (s->l1_size > 0) { + s->l1_table = qemu_mallocz( + align_offset(s->l1_size * sizeof(uint64_t), 512)); + if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != + s->l1_size * sizeof(uint64_t)) + goto fail; + for(i = 0;i < s->l1_size; i++) { + be64_to_cpus(&s->l1_table[i]); + } } /* alloc L2 cache */ s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));