From 2fd74d865ddd2dfd05657ef76de7c48438850e3d Mon Sep 17 00:00:00 2001 From: Liu Jiang Date: Sat, 5 Jun 2021 16:07:43 +0800 Subject: [PATCH] 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 --- src/ioctls/system.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ioctls/system.rs b/src/ioctls/system.rs index 07949466..84e1353b 100644 --- a/src/ioctls/system.rs +++ b/src/ioctls/system.rs @@ -221,9 +221,12 @@ impl Kvm { /// ``` /// pub fn get_max_vcpus(&self) -> usize { - match self.check_extension_int(Cap::MaxVcpus) { - 0 => self.get_nr_vcpus(), - x => x as usize, + let v = self.check_extension_int(Cap::MaxVcpus); + + if v <= 0 { + self.get_nr_vcpus() + } else { + v as usize } } @@ -242,9 +245,12 @@ impl Kvm { /// ``` /// pub fn get_max_vcpu_id(&self) -> usize { - match self.check_extension_int(Cap::MaxVcpuId) { - 0 => self.get_max_vcpus(), - x => x as usize, + let v = self.check_extension_int(Cap::MaxVcpuId); + + if v <= 0 { + self.get_max_vcpus() + } else { + v as usize } }