From: Max Reitz Date: Sun, 4 May 2014 03:31:40 +0000 (+0200) Subject: qcow2: Fix alloc_clusters_noref() overflow detection X-Git-Tag: v2.1.0-rc0~132^2~16 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=65f33bc0020112e7be7b8966495cd5efa2d0ab15;p=thirdparty%2Fqemu.git qcow2: Fix alloc_clusters_noref() overflow detection If the very first allocation has a length of 0, the free_cluster_index is still 0 after the for loop, which means that subtracting one from it will underflow and signal an invalid range of clusters by returning -EFBIG. However, there is no such range, as its length is 0. Fix this by preventing underflows on free_cluster_index during the check. Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf Signed-off-by: Stefan Hajnoczi --- diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index e79895d11dc..9507aef8471 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -656,7 +656,9 @@ retry: /* Make sure that all offsets in the "allocated" range are representable * in an int64_t */ - if (s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits)) { + if (s->free_cluster_index > 0 && + s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits)) + { return -EFBIG; }