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 <mreitz@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Max Reitz 2014-05-04 05:31:40 +02:00 committed by Stefan Hajnoczi
parent 43cbeffb19
commit 65f33bc002
1 changed files with 3 additions and 1 deletions

View File

@ -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;
}