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 Jun 5, 2021
1 parent 577d4eb commit 2fd74d8
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 @@ -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
}
}

Expand All @@ -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
}
}

Expand Down

0 comments on commit 2fd74d8

Please sign in to comment.