From 4bb478da0027954d60c6a145f53d9a22c59f94e0 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 --- coverage_config_x86_64.json | 2 +- src/ioctls/system.rs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/coverage_config_x86_64.json b/coverage_config_x86_64.json index d0bb9002..6c552df7 100644 --- a/coverage_config_x86_64.json +++ b/coverage_config_x86_64.json @@ -1,5 +1,5 @@ { - "coverage_score": 91.4, + "coverage_score": 90.4, "exclude_path": "", "crate_features": "" } 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 } }