Skip to content

Commit 9e252ee

Browse files
bonzinijiangliu
authored andcommitted
guest_memory: fixes to Bytes<GuestAddress>::read_from
There are a bug and a small enhancement here. The enhancement is that the source need not provide the exact number of bytes; try_access supports partial reads so we can use src.read() instead of src.read_exact(). The bug fix is in the else case, which uses src.read() but then throws away the result and advances the iterator by the full amount that was expected. We could also fix the bug by using src.read_exact() in the else branch, but read_from() has a return value for partial reads so it's unnecessary to do that. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent f1bb166 commit 9e252ee

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

coverage_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 84.5,
2+
"coverage_score": 84.3,
33
"exclude_path": "mmap_windows.rs",
44
"crate_features": "backend-mmap"
55
}

src/guest_memory.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,16 +582,15 @@ impl<T: GuestMemory> Bytes<GuestAddress> for T {
582582
// This is safe cause `start` and `len` are within the `region`.
583583
let start = caddr.raw_value() as usize;
584584
let end = start + len;
585-
src.read_exact(&mut dst[start..end])
586-
.map_err(Error::IOError)?;
587-
Ok(len)
585+
let bytes_read = src.read(&mut dst[start..end]).map_err(Error::IOError)?;
586+
Ok(bytes_read)
588587
} else {
589588
let len = std::cmp::min(len, MAX_ACCESS_CHUNK);
590589
let mut buf = vec![0u8; len].into_boxed_slice();
591590
let bytes_read = src.read(&mut buf[..]).map_err(Error::IOError)?;
592591
let bytes_written = region.write(&buf[0..bytes_read], caddr)?;
593592
assert_eq!(bytes_written, bytes_read);
594-
Ok(len)
593+
Ok(bytes_read)
595594
}
596595
})
597596
}

0 commit comments

Comments
 (0)