Skip to content

Commit 93cf05e

Browse files
committed
Remove expect from hyperlight-host
Signed-off-by: Simon Davies <[email protected]>
1 parent ca4d775 commit 93cf05e

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

src/hyperlight_host/src/hypervisor/surrogate_process_manager.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::path::{Path, PathBuf};
2323
use crossbeam_channel::{unbounded, Receiver, Sender};
2424
use hyperlight_common::mem::PAGE_SIZE_USIZE;
2525
use rust_embed::RustEmbed;
26-
use tracing::{info, instrument, Span};
26+
use tracing::{error, info, instrument, Span};
2727
use windows::core::{s, PCSTR};
2828
use windows::Win32::Foundation::HANDLE;
2929
use windows::Win32::Security::SECURITY_ATTRIBUTES;
@@ -251,13 +251,16 @@ impl Drop for SurrogateProcessManager {
251251
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
252252
fn drop(&mut self) {
253253
let handle: HANDLE = self.job_handle.into();
254-
unsafe {
254+
if unsafe {
255255
// Terminating the job object will terminate all the surrogate
256256
// processes.
257257

258258
TerminateJobObject(handle, 0)
259259
}
260-
.expect("surrogate job objects were not all terminated error:");
260+
.is_err()
261+
{
262+
error!("surrogate job objects were not all terminated");
263+
}
261264
}
262265
}
263266

src/hyperlight_host/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
#![cfg_attr(not(any(test, debug_assertions)), warn(clippy::expect_used))]
18+
1719
use std::sync::Once;
1820

1921
/// This crate contains an SDK that is used to execute specially-

src/hyperlight_host/src/mem/pe/base_relocations.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ pub(super) fn get_base_relocations(
132132
// Process each block of relocations until we have processed the expected amount of relocation data
133133
while size_processed < table_size {
134134
// Read the header for the block, which is the same format as a DataDirectory so I'm reusing its parse function
135-
let block_header =
136-
goblin::pe::data_directories::DataDirectory::parse(payload, &mut next_block_offset)
137-
.expect("oops");
135+
let block_header = goblin::pe::data_directories::DataDirectory::parse(
136+
payload,
137+
&mut next_block_offset,
138+
)?;
138139
// All relocation blocks in a page contain offsets against this address
139140
let page_virtual_address = block_header.virtual_address;
140141

src/hyperlight_host/src/mem/pe/pe_info.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ impl PEInfo {
6767
log_then_return!("unsupported PE file, not an executable image")
6868
}
6969

70-
let optional_header = pe
71-
.header
72-
.optional_header
73-
.expect("unsupported PE file, missing optional header entry");
70+
let optional_header = match pe.header.optional_header {
71+
Some(optional_header) => optional_header,
72+
None => log_then_return!("unsupported PE file, no optional header found"),
73+
};
7474

7575
// check that the PE file was built with the option /DYNAMICBASE
7676

@@ -117,10 +117,10 @@ impl PEInfo {
117117
// we are going to take care of the data section
118118
if name == ".data" {
119119
// Make sure we fail if we enter this block more than once
120-
assert_eq!(
121-
data_section_additional_bytes, 0,
122-
"Hyperlight currently only supports one .data section"
123-
);
120+
121+
if data_section_additional_bytes > 0 {
122+
log_then_return!("Hyperlight currently only supports one .data section")
123+
}
124124

125125
data_section_raw_pointer = section.pointer_to_raw_data;
126126
data_section_additional_bytes = virtual_size - raw_size;
@@ -251,8 +251,7 @@ impl PEInfo {
251251
}
252252

253253
cur.set_position(patch.offset as u64);
254-
cur.write_all(&patch.relocated_virtual_address.to_le_bytes())
255-
.expect("failed to write patch to pe file contents");
254+
cur.write_all(&patch.relocated_virtual_address.to_le_bytes())?;
256255
applied += 1;
257256
}
258257

@@ -279,8 +278,7 @@ impl PEInfo {
279278
}
280279

281280
let relocations =
282-
base_relocations::get_base_relocations(&self.payload, &self.reloc_section)
283-
.expect("error parsing base relocations");
281+
base_relocations::get_base_relocations(&self.payload, &self.reloc_section)?;
284282
let mut patches = Vec::with_capacity(relocations.len());
285283

286284
for reloc in relocations {

0 commit comments

Comments
 (0)