From d1866f0e09661f7dad40340c5c81cbf77f74bfce Mon Sep 17 00:00:00 2001 From: Patrick Ventuzelo <9038181+pventuzelo@users.noreply.github.com> Date: Tue, 17 Dec 2019 05:51:07 +0100 Subject: [PATCH] [lightbeam] replace asserts by Errors in module.rs / translate_only (#713) * replace assert by Errors * add better errors message module.rs --- crates/lightbeam/src/module.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/lightbeam/src/module.rs b/crates/lightbeam/src/module.rs index 9e93cec5ba..02cb9c09ec 100644 --- a/crates/lightbeam/src/module.rs +++ b/crates/lightbeam/src/module.rs @@ -7,7 +7,7 @@ use cranelift_codegen::{ isa, }; use memoffset::offset_of; -use more_asserts::assert_le; + use std::{convert::TryInto, mem}; use thiserror::Error; use wasmparser::{FuncType, MemoryType, ModuleReader, SectionCode, Type}; @@ -563,15 +563,19 @@ pub fn translate_only(data: &[u8]) -> Result { let memories = section.get_memory_section_reader()?; let mem = translate_sections::memory(memories)?; - assert_le!( - mem.len(), - 1, - "Multiple memory sections not yet unimplemented" - ); + if mem.len() > 1 { + return Err(Error::Input( + "Multiple memory sections not yet implemented".to_string(), + )); + } if !mem.is_empty() { let mem = mem[0]; - assert_eq!(Some(mem.limits.initial), mem.limits.maximum); + if Some(mem.limits.initial) != mem.limits.maximum { + return Err(Error::Input( + "Custom memory limits not supported in lightbeam".to_string(), + )); + } output.memory = Some(mem); } @@ -642,7 +646,11 @@ pub fn translate_only(data: &[u8]) -> Result { translate_sections::data(data)?; } - assert!(reader.eof()); + if !reader.eof() { + return Err(Error::Input( + "Unexpected data found after the end of the module".to_string(), + )); + } Ok(output) }