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 11, 2021
1 parent 462e32c commit ed66084
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 91.4,
"coverage_score": 90.4,
"exclude_path": "",
"crate_features": ""
}
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 ed66084

Please sign in to comment.