Skip to content

Commit

Permalink
Correctly handle error from check_extension_int()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jiangliu committed Dec 4, 2021
1 parent 9e1e308 commit a04ad9b
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/ioctls/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ impl Kvm {
/// assert!(kvm.get_max_vcpus() > 0);
/// ```
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
}
}

Expand All @@ -233,9 +236,12 @@ impl Kvm {
/// assert!(kvm.get_max_vcpu_id() > 0);
/// ```
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
}
}

Expand Down

0 comments on commit a04ad9b

Please sign in to comment.