summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2019-06-09 16:42:59 -0700
committerCommit Bot <commit-bot@chromium.org>2019-06-12 01:31:46 +0000
commit21c941786ea0cb72114f3e9c7c940471664862b5 (patch)
treef0bec49a5e6a5c381693d0b66dcb77fb92b37b10
parentcae80e321acdccb1591124f6bf657758f1e75d1d (diff)
downloadcrosvm-21c941786ea0cb72114f3e9c7c940471664862b5.tar
crosvm-21c941786ea0cb72114f3e9c7c940471664862b5.tar.gz
crosvm-21c941786ea0cb72114f3e9c7c940471664862b5.tar.bz2
crosvm-21c941786ea0cb72114f3e9c7c940471664862b5.tar.lz
crosvm-21c941786ea0cb72114f3e9c7c940471664862b5.tar.xz
crosvm-21c941786ea0cb72114f3e9c7c940471664862b5.tar.zst
crosvm-21c941786ea0cb72114f3e9c7c940471664862b5.zip
qcow: Calculate the max refcounts as a u64
u32's get multiplied together and can overflow. A usize was being
returned, make everything a u64 to make sure it fits.

Change-Id: I87071d294f4e62247c9ae72244db059a7b528b62
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1651459
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
-rw-r--r--qcow/src/qcow.rs13
1 files changed, 7 insertions, 6 deletions
diff --git a/qcow/src/qcow.rs b/qcow/src/qcow.rs
index 4c152d0..eccd23d 100644
--- a/qcow/src/qcow.rs
+++ b/qcow/src/qcow.rs
@@ -295,11 +295,12 @@ 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 << refcount_order as u64) / 8;
+    let for_data = div_round_up_u64(num_clusters as u64 * refcount_bytes, cluster_size as u64);
+    let for_refcounts = div_round_up_u64(for_data * refcount_bytes, cluster_size as u64);
+    for_data + for_refcounts
 }
 
 /// Represents a qcow2 file. This is a sparse file format maintained by the qemu project.
@@ -419,7 +420,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,