diff --git a/qcow/src/qcow.rs b/qcow/src/qcow.rs index 4a25749b1..178fb70cb 100755 --- a/qcow/src/qcow.rs +++ b/qcow/src/qcow.rs @@ -322,11 +322,15 @@ impl QcowHeader { } } -fn max_refcount_clusters(refcount_order: u32, cluster_size: u32, num_clusters: u32) -> usize { - let refcount_bytes = (0x01u32 << refcount_order) / 8; - let for_data = div_round_up_u32(num_clusters * refcount_bytes, cluster_size); - let for_refcounts = div_round_up_u32(for_data * refcount_bytes, cluster_size); - for_data as usize + for_refcounts as usize +fn max_refcount_clusters(refcount_order: u32, cluster_size: u32, num_clusters: u32) -> u64 { + // Use u64 as the product of the u32 inputs can overflow. + let refcount_bytes = (0x01 << u64::from(refcount_order)) / 8; + let for_data = div_round_up_u64( + u64::from(num_clusters) * refcount_bytes, + u64::from(cluster_size), + ); + let for_refcounts = div_round_up_u64(for_data * refcount_bytes, u64::from(cluster_size)); + for_data + for_refcounts } /// Represents a qcow2 file. This is a sparse file format maintained by the qemu project. @@ -446,7 +450,7 @@ impl QcowFile { header.refcount_order, cluster_size as u32, (num_clusters + l1_clusters + num_l2_clusters + header_clusters) as u32, - ) as u64; + ); let refcount_block_entries = cluster_size / refcount_bytes; let refcounts = RefCount::new( &mut raw_file,