inconsistent metadata test and panic fix

This commit is contained in:
NikVolf
2018-02-07 21:57:09 +03:00
parent 94fba1c41a
commit ca0c2dd450
4 changed files with 24 additions and 4 deletions

Binary file not shown.

View File

@@ -94,6 +94,8 @@ pub enum Error {
InvalidVarInt32,
/// Invalid VarInt64 value
InvalidVarInt64,
/// Inconsistent metadata
InconsistentMetadata,
}
impl fmt::Display for Error {
@@ -116,6 +118,7 @@ impl fmt::Display for Error {
Error::InvalidVarUint1(val) => write!(f, "Not an unsigned 1-bit integer: {}", val),
Error::InvalidVarInt32 => write!(f, "Not a signed 32-bit integer"),
Error::InvalidVarInt64 => write!(f, "Not a signed 64-bit integer"),
Error::InconsistentMetadata => write!(f, "Inconsistent metadata"),
}
}
}
@@ -138,6 +141,7 @@ impl error::Error for Error {
Error::InvalidVarUint1(_) => "Not an unsigned 1-bit integer",
Error::InvalidVarInt32 => "Not a signed 32-bit integer",
Error::InvalidVarInt64 => "Not a signed 64-bit integer",
Error::InconsistentMetadata => "Inconsistent metadata",
}
}
}

View File

@@ -319,7 +319,7 @@ pub fn peek_size(source: &[u8]) -> usize {
#[cfg(test)]
mod integration_tests {
use super::super::{deserialize_file, serialize, deserialize_buffer, Section};
use super::super::{deserialize_file, serialize, deserialize_buffer, Section, Error};
use super::Module;
#[test]
@@ -468,6 +468,15 @@ 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;

View File

@@ -59,9 +59,9 @@ pub enum Section {
Code(CodeSection),
/// Data definition section
Data(DataSection),
/// Name section.
/// Name section.
///
/// Note that initially it is not parsed until `parse_names` is called explicitly.
/// Note that initially it is not parsed until `parse_names` is called explicitly.
Name(NameSection),
}
@@ -232,7 +232,14 @@ impl Deserialize for CustomSection {
let section_length: u32 = VarUint32::deserialize(reader)?.into();
let name = String::deserialize(reader)?;
let payload_left = section_length - (name.len() as u32 + name.len() as u32 / 128 + 1);
let total_naming = name.len() as u32 + name.len() as u32 / 128 + 1;
if total_naming > section_length {
return Err(Error::InconsistentMetadata)
} else if total_naming == section_length {
return Ok(CustomSection { name: name, payload: Vec::new() });
}
let payload_left = section_length - total_naming;
let mut payload = vec![0u8; payload_left as usize];
reader.read_exact(&mut payload[..])?;