Skip to content

Commit 2127a9b

Browse files
committed
Correctly handle error from check_extension_int()
Kvm::check_extension_int() may return negative value as error code, so Kvm::get_max_vcpus() and Kvm::get_max_vcpu_id() should handle the error cases. Signed-off-by: Liu Jiang <[email protected]>
1 parent 140c8fe commit 2127a9b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/ioctls/system.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,12 @@ impl Kvm {
213213
/// assert!(kvm.get_max_vcpus() > 0);
214214
/// ```
215215
pub fn get_max_vcpus(&self) -> usize {
216-
match self.check_extension_int(Cap::MaxVcpus) {
217-
0 => self.get_nr_vcpus(),
218-
x => x as usize,
216+
let v = self.check_extension_int(Cap::MaxVcpus);
217+
218+
if v <= 0 {
219+
self.get_nr_vcpus()
220+
} else {
221+
v as usize
219222
}
220223
}
221224

@@ -233,9 +236,12 @@ impl Kvm {
233236
/// assert!(kvm.get_max_vcpu_id() > 0);
234237
/// ```
235238
pub fn get_max_vcpu_id(&self) -> usize {
236-
match self.check_extension_int(Cap::MaxVcpuId) {
237-
0 => self.get_max_vcpus(),
238-
x => x as usize,
239+
let v = self.check_extension_int(Cap::MaxVcpuId);
240+
241+
if v <= 0 {
242+
self.get_max_vcpus()
243+
} else {
244+
v as usize
239245
}
240246
}
241247

0 commit comments

Comments
 (0)