qcow2: Fix qcow2_get_cluster_offset()
authorMax Reitz <mreitz@redhat.com>
Mon, 20 Jun 2016 14:26:23 +0000 (16:26 +0200)
committerMax Reitz <mreitz@redhat.com>
Wed, 13 Jul 2016 11:41:38 +0000 (13:41 +0200)
commitc834cba90521576224c30b15ebb4d6aeab7b42c4
tree1b43ac7b6bcbf994e04065fcb3287ae574760c7a
parenta367467995d0528fe591d87ca2e437c7b7d7951b
qcow2: Fix qcow2_get_cluster_offset()

Recently, qcow2_get_cluster_offset() has been changed to work with bytes
instead of sectors. This invalidated some assertions and introduced a
possible integer multiplication overflow.

This could be reproduced using e.g.

$ qemu-img create -f qcow2 -o cluster_size=1M blub.qcow2 8G
Formatting 'foo.qcow2', fmt=qcow2 size=8589934592 encryption=off
cluster_size=1048576 lazy_refcounts=off refcount_bits=16
$ qemu-io -c map blub.qcow2
qemu-io: qemu/block/qcow2-cluster.c:504: qcow2_get_cluster_offset:
Assertion `bytes_needed <= INT_MAX' failed.
[1]    20775 abort (core dumped)  qemu-io -c map foo.qcow2

This patch removes the now wrong assertion, adding comments and more
assertions to prove its correctness (and fixing the overflow which would
become apparent with the original assertion removed).

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160620142623.24471-3-mreitz@redhat.com
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
block/qcow2-cluster.c