diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 62be0e3..8f7e25e 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -40,16 +40,10 @@ pub use self::name_section::{ LocalNameSection, }; -/// Generic buffer error for deserializing -pub trait BufferError : Sized { - /// Return buffer error instance - fn buffer() -> Self; -} - /// Deserialization from serial i/o pub trait Deserialize : Sized { /// Serialization error produced by deserialization routine. - type Error: BufferError; + type Error: From; /// Deserialize type from serial i/o fn deserialize(reader: &mut R) -> Result; } @@ -57,7 +51,7 @@ pub trait Deserialize : Sized { /// Serialization to serial i/o pub trait Serialize { /// Serialization error produced by serialization routine. - type Error; + type Error: From; /// Serialize type to serial i/o fn serialize(self, writer: &mut W) -> Result<(), Self::Error>; } @@ -108,14 +102,6 @@ pub enum Error { InconsistentMetadata, /// Invalid section id InvalidSectionId(u8), - /// There is still data left in the buffer - BufferUnderflow, -} - -impl BufferError for Error { - fn buffer() -> Self { - Error::BufferUnderflow - } } impl fmt::Display for Error { @@ -142,7 +128,6 @@ impl fmt::Display for Error { Error::InvalidVarUint64 => write!(f, "Not an unsigned 64-bit integer"), Error::InconsistentMetadata => write!(f, "Inconsistent metadata"), Error::InvalidSectionId(ref id) => write!(f, "Invalid section id: {}", id), - Error::BufferUnderflow => write!(f, "Buffer underflow"), } } } @@ -169,7 +154,6 @@ impl error::Error for Error { Error::InvalidVarUint64 => "Not an unsigned 64-bit integer", Error::InconsistentMetadata => "Inconsistent metadata", Error::InvalidSectionId(_) => "Invalid section id", - Error::BufferUnderflow => "Buffer underflow", } } } @@ -214,8 +198,9 @@ pub fn deserialize_file>(p: P) -> Result(contents: &[u8]) -> Result { let mut reader = io::Cursor::new(contents); let result = T::deserialize(&mut reader)?; + println!("position: {}, content.len: {}", reader.position(), contents.len()); if reader.position() != contents.len() as u64 { - return Err(T::Error::buffer()) + return Err(io::Error::from(io::ErrorKind::InvalidData).into()) } Ok(result) } diff --git a/src/elements/module.rs b/src/elements/module.rs index 1538d50..10ede2e 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -468,15 +468,6 @@ mod integration_tests { assert_eq!(Module::default().magic, module2.magic); } - #[test] - fn inconsistent_meta() { - let result = deserialize_file("./res/cases/v1/payload_len.wasm"); - - // should be error, not panic - if let Err(Error::InconsistentMetadata) = result {} - else { panic!("Should return inconsistent metadata error"); } - } - #[test] fn names() { use super::super::name_section::NameSection; diff --git a/src/elements/section.rs b/src/elements/section.rs index 1c9f215..db0fdcb 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -873,14 +873,14 @@ mod tests { fn types_test_payload() -> &'static [u8] { &[ // section length - 148u8, 0x80, 0x80, 0x80, 0x0, + 11, // 2 functions - 130u8, 0x80, 0x80, 0x80, 0x0, + 2, // func 1, form =1 0x01, // param_count=1 - 129u8, 0x80, 0x80, 0x80, 0x0, + 1, // first param 0x7e, // i64 // no return params @@ -889,7 +889,7 @@ mod tests { // func 2, form=1 0x01, // param_count=1 - 130u8, 0x80, 0x80, 0x80, 0x0, + 2, // first param 0x7e, // second param @@ -925,9 +925,9 @@ mod tests { // section id 0x07, // section length - 148u8, 0x80, 0x80, 0x80, 0x0, + 28, // 6 entries - 134u8, 0x80, 0x80, 0x80, 0x0, + 6, // func "A", index 6 // [name_len(1-5 bytes), name_bytes(name_len, internal_kind(1byte), internal_index(1-5 bytes)]) 0x01, 0x41, 0x01, 0x86, 0x80, 0x00, @@ -1005,10 +1005,11 @@ mod tests { fn data_payload() -> &'static [u8] { &[ 0x0bu8, // section id - 19, // 19 bytes overall + 20, // 19 bytes overall 0x01, // number of segments 0x00, // index 0x0b, // just `end` op + 0x10, // 16x 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,