From 28199fdac8953c60cfe2167ac3e9d17e0d9de4dc Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Fri, 1 Dec 2017 09:05:33 +0300 Subject: [PATCH] Add table validation. --- src/validation/mod.rs | 47 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/validation/mod.rs b/src/validation/mod.rs index 8415f3b..47344a5 100644 --- a/src/validation/mod.rs +++ b/src/validation/mod.rs @@ -1,4 +1,4 @@ -use elements::{Module, ResizableLimits, MemoryType}; +use elements::{Module, ResizableLimits, MemoryType, TableType}; pub struct Error(pub String); @@ -11,6 +11,14 @@ pub fn validate_module(module: &Module) -> Result<(), Error> { .collect::>()? } + if let Some(table_section) = module.table_section() { + table_section + .entries() + .iter() + .map(TableType::validate) + .collect::>()? + } + Ok(()) } @@ -35,6 +43,12 @@ impl MemoryType { } } +impl TableType { + fn validate(&self) -> Result<(), Error> { + self.limits().validate() + } +} + #[cfg(test)] mod tests { use super::validate_module; @@ -69,7 +83,7 @@ mod tests { .build(); assert!(validate_module(&m).is_ok()); - // mod is always valid without max. + // mem is always valid without max. let m = module() .memory() .with_min(10) @@ -77,4 +91,33 @@ mod tests { .build(); assert!(validate_module(&m).is_ok()); } + + #[test] + fn table_limits() { + // min > max + let m = module() + .table() + .with_min(10) + .with_max(Some(9)) + .build() + .build(); + assert!(validate_module(&m).is_err()); + + // min = max + let m = module() + .table() + .with_min(10) + .with_max(Some(10)) + .build() + .build(); + assert!(validate_module(&m).is_ok()); + + // table is always valid without max. + let m = module() + .table() + .with_min(10) + .build() + .build(); + assert!(validate_module(&m).is_ok()); + } }