Skip to content

Commit 52c1c84

Browse files
authored
Merge pull request #40 from entropyxyz/add-empty-bytecode
added EmptyBytecode variant to RuntimeError
2 parents 008fd64 + 0c2a673 commit 52c1c84

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

runtime/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ pub use bindgen::{Error as ProgramError, Program, SignatureRequest};
2020
/// Runtime `Error` type
2121
#[derive(Debug, Error)]
2222
pub enum RuntimeError {
23-
/// Program bytecode is invalid.
23+
/// Program bytecode is of zero length (core-side runtime error; Programs should probably not return this)
24+
#[error("Bytecode length is zero")]
25+
EmptyBytecode,
26+
/// Program bytecode is not a valid WebAssembly component.
2427
#[error("Invalid bytecode")]
2528
InvalidBytecode,
2629
/// Runtime error during execution.
@@ -63,6 +66,10 @@ impl Runtime {
6366
program: &[u8],
6467
signature_request: &SignatureRequest,
6568
) -> Result<(), RuntimeError> {
69+
if program.len() == 0 {
70+
return Err(RuntimeError::EmptyBytecode);
71+
}
72+
6673
let component = Component::from_binary(&self.engine, program)
6774
.map_err(|_| RuntimeError::InvalidBytecode)?;
6875

runtime/tests/runtime.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ fn test_barebones_component_fails_with_data_length_less_than_10() {
3333
let res = runtime.evaluate(BAREBONES_COMPONENT_WASM, &signature_request);
3434
assert!(res.is_err());
3535
}
36+
37+
#[test]
38+
fn test_empty_bytecode_fails() {
39+
let mut runtime = Runtime::new();
40+
41+
let signature_request = SignatureRequest {
42+
message: vec![],
43+
auxilary_data: None,
44+
};
45+
46+
let res = runtime.evaluate(&[], &signature_request);
47+
assert_eq!(res.unwrap_err().to_string(), "Bytecode length is zero");
48+
}

0 commit comments

Comments
 (0)