qcow: avoid truncation if usize is 32 bits

The u64 offsets could be truncated when running on a 32 bit machine.
Do the math in 64 bit, limit to usize::MAX, then truncate.

BUG=837453
TEST=run crosvm and read/write files

Change-Id: If44ec94cf730ca7c1e580eeddd202e54e2de1081
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1031301
Reviewed-by: Sonny Rao <sonnyrao@chromium.org>
This commit is contained in:
Dylan Reid 2018-04-26 17:57:28 -07:00 committed by chrome-bot
parent 71aedde5af
commit 832fc3cde5

View file

@ -302,14 +302,14 @@ impl QcowFile {
if address.checked_add(count as u64).is_none() || address > self.virtual_size() {
return 0;
}
min(count, self.virtual_size() as usize - address as usize)
min(count as u64, self.virtual_size() - address) as usize
}
// Limits the range so that it doesn't overflow the end of a cluster.
fn limit_range_cluster(&self, address: u64, count: usize) -> usize {
let offset: u64 = address & self.cluster_mask;
let limit = self.cluster_size - offset;
min(count, limit as usize)
min(count as u64, limit) as usize
}
// Gets the maximum virtual size of this image.